@@ -191,21 +191,22 @@ usually declare a static object variable at the beginning of your file::
191191
192192 static PyObject *SpamError;
193193
194- and initialize it in your module's initialization function (:cfunc: `initspam `)
194+ and initialize it in your module's initialization function (:cfunc: `PyInit_spam `)
195195with an exception object (leaving out the error checking for now)::
196196
197197 PyMODINIT_FUNC
198- initspam (void)
198+ PyInit_spam (void)
199199 {
200200 PyObject *m;
201201
202- m = Py_InitModule("spam", SpamMethods );
202+ m = PyModule_Create(&spammodule );
203203 if (m == NULL)
204- return;
204+ return NULL ;
205205
206206 SpamError = PyErr_NewException("spam.error", NULL, NULL);
207207 Py_INCREF(SpamError);
208208 PyModule_AddObject(m, "error", SpamError);
209+ return m;
209210 }
210211
211212Note that the Python name for the exception object is :exc: `spam.error `. The
@@ -303,49 +304,64 @@ accept a third ``PyObject *`` parameter which will be a dictionary of keywords.
303304Use :cfunc: `PyArg_ParseTupleAndKeywords ` to parse the arguments to such a
304305function.
305306
306- The method table must be passed to the interpreter in the module's
307+ The method table must be referenced in the module definition structure::
308+
309+ struct PyModuleDef spammodule = {
310+ PyModuleDef_HEAD_INIT,
311+ "spam", /* name of module */
312+ spam_doc, /* module documentation, may be NULL */
313+ -1, /* size of per-interpreter state of the module,
314+ or -1 if the module keeps state in global variables. */
315+ SpamMethods
316+ };
317+
318+ This structure, in turn, must be passed to the interpreter in the module's
307319initialization function. The initialization function must be named
308- :cfunc: `initname `, where *name * is the name of the module, and should be the
320+ :cfunc: `PyInit_name `, where *name * is the name of the module, and should be the
309321only non-\ ``static `` item defined in the module file::
310322
311323 PyMODINIT_FUNC
312- initspam (void)
324+ PyInit_spam (void)
313325 {
314- (void) Py_InitModule("spam", SpamMethods );
326+ return PyModule_Create(&spammodule );
315327 }
316328
317329Note that PyMODINIT_FUNC declares the function as ``void `` return type,
318330declares any special linkage declarations required by the platform, and for C++
319331declares the function as ``extern "C" ``.
320332
321333When the Python program imports module :mod: `spam ` for the first time,
322- :cfunc: `initspam ` is called. (See below for comments about embedding Python.)
323- It calls :cfunc: `Py_InitModule `, which creates a "module object" (which is
324- inserted in the dictionary ``sys.modules `` under the key ``"spam" ``), and
334+ :cfunc: `PyInit_spam ` is called. (See below for comments about embedding Python.)
335+ It calls :cfunc: `PyModule_Create `, which returns a module object, and
325336inserts built-in function objects into the newly created module based upon the
326- table (an array of :ctype: `PyMethodDef ` structures) that was passed as its
327- second argument. :cfunc: `Py_InitModule ` returns a pointer to the module object
328- that it creates (which is unused here) . It may abort with a fatal error for
337+ table (an array of :ctype: `PyMethodDef ` structures) found in the module definition.
338+ :cfunc: `PyModule_Create ` returns a pointer to the module object
339+ that it creates. It may abort with a fatal error for
329340certain errors, or return *NULL * if the module could not be initialized
330- satisfactorily.
341+ satisfactorily. The init function must return the module object to its caller,
342+ so that it then gets inserted into ``sys.modules ``.
331343
332- When embedding Python, the :cfunc: `initspam ` function is not called
344+ When embedding Python, the :cfunc: `PyInit_spam ` function is not called
333345automatically unless there's an entry in the :cdata: `_PyImport_Inittab ` table.
334- The easiest way to handle this is to statically initialize your
335- statically-linked modules by directly calling :cfunc: `initspam ` after the call
336- to :cfunc: `Py_Initialize `::
346+ To add the module to the initialization table, use :cfunc: `PyImport_AppendInittab `,
347+ optionally followed by an import of the module::
337348
338349 int
339350 main(int argc, char *argv[])
340351 {
352+ /* Add a builtin module, before Py_Initialize */
353+ PyImport_AppendInittab("spam", PyInit_spam);
354+
341355 /* Pass argv[0] to the Python interpreter */
342356 Py_SetProgramName(argv[0]);
343357
344358 /* Initialize the Python interpreter. Required. */
345359 Py_Initialize();
346360
347- /* Add a static module */
348- initspam();
361+ /* Optionally import the module; alternatively,
362+ import can be deferred until the embedded script
363+ imports it. */
364+ PyImport_ImportModule("spam");
349365
350366An example may be found in the file :file: `Demo/embed/demo.c ` in the Python
351367source distribution.
@@ -1154,15 +1170,15 @@ exporting module, not a client module. Finally, the module's initialization
11541170function must take care of initializing the C API pointer array::
11551171
11561172 PyMODINIT_FUNC
1157- initspam (void)
1173+ PyInit_spam (void)
11581174 {
11591175 PyObject *m;
11601176 static void *PySpam_API[PySpam_API_pointers];
11611177 PyObject *c_api_object;
11621178
1163- m = Py_InitModule("spam", SpamMethods );
1179+ m = PyModule_Create(&spammodule );
11641180 if (m == NULL)
1165- return;
1181+ return NULL ;
11661182
11671183 /* Initialize the C API pointer array */
11681184 PySpam_API[PySpam_System_NUM] = (void *)PySpam_System;
@@ -1172,10 +1188,11 @@ function must take care of initializing the C API pointer array::
11721188
11731189 if (c_api_object != NULL)
11741190 PyModule_AddObject(m, "_C_API", c_api_object);
1191+ return m;
11751192 }
11761193
11771194Note that ``PySpam_API `` is declared ``static ``; otherwise the pointer
1178- array would disappear when :func: `initspam ` terminates!
1195+ array would disappear when :func: `PyInit_spam ` terminates!
11791196
11801197The bulk of the work is in the header file :file: `spammodule.h `, which looks
11811198like this::
0 commit comments