classification
Title: Embedding example does not add created module
Type: Stage: needs patch
Components: Documentation Versions: Python 3.0
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, blakemadden, brett.cannon, georg.brandl, loewis
Priority: critical Keywords: needs review, patch

Created on 2008-12-08 15:00 by blakemadden, last changed 2008-12-09 23:49 by georg.brandl. This issue is now closed.

Files
File name Uploaded Description Edit
python.cpp blakemadden, 2008-12-08 15:00
embedding_example.patch amaury.forgeotdarc, 2008-12-09 23:24
Messages (9)
msg77307 - (view) Author: blake madden (blakemadden) Date: 2008-12-08 15:00
Following an example of how to extend and embed that I found in patch
file r67655 (the website is out of date [issue4586]), 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.
msg77322 - (view) Author: blake madden (blakemadden) Date: 2008-12-08 16:24
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.
msg77326 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-12-08 16:50
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);
msg77445 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2008-12-09 20:53
Because the init function for extension modules are supposed to return
the module now and the import machinery adds the module itself.
msg77449 - (view) Author: blake madden (blakemadden) Date: 2008-12-09 21:00
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.
msg77453 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2008-12-09 21:05
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.
msg77464 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2008-12-09 22:22
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.
msg77471 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-12-09 23:24
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.
msg77474 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-12-09 23:49
Applied in r67682.

Please don't add things directly to cporting.rst, rather amend the
PortingExtensionModulesToPy3k wiki page.
History
Date User Action Args
2008-12-09 23:49:19georg.brandlsetstatus: open -> closed
resolution: fixed
messages: + msg77474
2008-12-09 23:24:21amaury.forgeotdarcsetkeywords: + needs review, patch
files: + embedding_example.patch
messages: + msg77471
2008-12-09 22:22:41loewissetassignee: loewis ->
messages: + msg77464
2008-12-09 21:05:24brett.cannonsetpriority: critical
assignee: georg.brandl -> loewis
title: Example patch is missing something -> Embedding example does not add created module
messages: + msg77453
stage: needs patch
2008-12-09 21:00:25blakemaddensetmessages: + msg77449
2008-12-09 20:53:10brett.cannonsetnosy: + brett.cannon
messages: + msg77445
2008-12-08 16:50:00amaury.forgeotdarcsetnosy: + amaury.forgeotdarc, loewis
messages: + msg77326
2008-12-08 16:24:42blakemaddensettitle: PyModule_Create doesn't work (or example is missing something) -> Example patch is missing something
2008-12-08 16:24:01blakemaddensetassignee: georg.brandl
messages: + msg77322
components: + Documentation, - Extension Modules
nosy: + georg.brandl
2008-12-08 15:00:51blakemaddencreate