Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedding example does not add created module #48842

Closed
blakemadden mannequin opened this issue Dec 8, 2008 · 9 comments
Closed

Embedding example does not add created module #48842

blakemadden mannequin opened this issue Dec 8, 2008 · 9 comments
Labels
docs Documentation in the Doc dir

Comments

@blakemadden
Copy link
Mannequin

blakemadden mannequin commented Dec 8, 2008

BPO 4592
Nosy @loewis, @brettcannon, @birkenfeld, @amauryfa
Files
  • python.cpp
  • embedding_example.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2008-12-09.23:49:19.342>
    created_at = <Date 2008-12-08.15:00:50.938>
    labels = ['docs']
    title = 'Embedding example does not add created module'
    updated_at = <Date 2008-12-09.23:49:19.341>
    user = 'https://bugs.python.org/blakemadden'

    bugs.python.org fields:

    activity = <Date 2008-12-09.23:49:19.341>
    actor = 'georg.brandl'
    assignee = 'none'
    closed = True
    closed_date = <Date 2008-12-09.23:49:19.342>
    closer = 'georg.brandl'
    components = ['Documentation']
    creation = <Date 2008-12-08.15:00:50.938>
    creator = 'blakemadden'
    dependencies = []
    files = ['12281', '12310']
    hgrepos = []
    issue_num = 4592
    keywords = ['patch', 'needs review']
    message_count = 9.0
    messages = ['77307', '77322', '77326', '77445', '77449', '77453', '77464', '77471', '77474']
    nosy_count = 5.0
    nosy_names = ['loewis', 'brett.cannon', 'georg.brandl', 'amaury.forgeotdarc', 'blakemadden']
    pr_nums = []
    priority = 'critical'
    resolution = 'fixed'
    stage = 'needs patch'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue4592'
    versions = ['Python 3.0']

    @blakemadden
    Copy link
    Mannequin Author

    blakemadden mannequin commented Dec 8, 2008

    Following an example of how to extend and embed that I found in patch
    file r67655 (the website is out of date [bpo-4586]), PyModule_Create
    appears to not work (or example is not complete). When I run r67655
    (attached also) I get an error saying that "emb" is not a module.

    @blakemadden blakemadden mannequin added the extension-modules C modules in the Modules dir label Dec 8, 2008
    @blakemadden
    Copy link
    Mannequin Author

    blakemadden mannequin commented Dec 8, 2008

    It seems that a call to "PyDict_SetItemString" is what is missing from
    r67655:

    PyObject* m = PyModule_Create(&EmbModule);
    PyDict_SetItemString(PyImport_GetModuleDict(), EmbModule.m_name, m);

    I am just guessing here though, don't know if this is correct way to do
    things.

    @blakemadden blakemadden mannequin added docs Documentation in the Doc dir and removed extension-modules C modules in the Modules dir labels Dec 8, 2008
    @blakemadden blakemadden mannequin assigned birkenfeld Dec 8, 2008
    @blakemadden blakemadden mannequin changed the title PyModule_Create doesn't work (or example is missing something) Example patch is missing something Dec 8, 2008
    @amauryfa
    Copy link
    Member

    amauryfa commented Dec 8, 2008

    In python2.6, Py_InitModule4() calls PyImport_AddModule().
    Why doesn't python3.0's PyModule_Create() do the same?

    Index: Objects/moduleobject.c
    ===================================================================

    --- Objects/moduleobject.c      (revision 67577)
    +++ Objects/moduleobject.c      (working copy)
    @@ -103,8 +103,9 @@
                            _Py_PackageContext = NULL;
                    }
            }
    -       if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
    +       if ((m = (PyModuleObject*)PyImport_AddModule(name)) == NULL)
                    return NULL;
    +       Py_INCREF(m);
            if (module->m_size > 0) {
                    m->md_state = PyMem_MALLOC(module->m_size);

    @brettcannon
    Copy link
    Member

    Because the init function for extension modules are supposed to return
    the module now and the import machinery adds the module itself.

    @blakemadden
    Copy link
    Mannequin Author

    blakemadden mannequin commented Dec 9, 2008

    So, does that mean that Python is fine and there is a problem in the
    example? How do I get that example to work, there seems to be something
    missing in it. Thanks.

    @brettcannon
    Copy link
    Member

    The example is broken.

    And the example under discussion is
    http://docs.python.org/dev/3.0/extending/embedding.html#extending-embedded-python
    .

    I have changed the title to be more descriptive and assigned to Martin
    to find out what the proper way is to handle this case.

    @brettcannon brettcannon assigned loewis and unassigned birkenfeld Dec 9, 2008
    @brettcannon brettcannon changed the title Example patch is missing something Embedding example does not add created module Dec 9, 2008
    @loewis
    Copy link
    Mannequin

    loewis mannequin commented Dec 9, 2008

    The proper way of extending embedded Python can be seen in
    Demo/embed/demo.c.

    I can't contribute to the documentation, so I'm unassigning myself.

    @loewis loewis mannequin removed their assignment Dec 9, 2008
    @amauryfa
    Copy link
    Member

    amauryfa commented Dec 9, 2008

    See attached documentation patch.

    Instead of a direct call to PyModule_Create(), the main function must 
    use
        PyImport_AppendInittab("emb", &PyInit_emb);

    Note that the same line also works for all 2.x versions.
    I'll try to add an item to howto/cporting.rst.

    @birkenfeld
    Copy link
    Member

    Applied in r67682.

    Please don't add things directly to cporting.rst, rather amend the
    PortingExtensionModulesToPy3k wiki page.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    docs Documentation in the Doc dir
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants