@@ -191,21 +191,22 @@ usually declare a static object variable at the beginning of your file::
191
191
192
192
static PyObject *SpamError;
193
193
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`)
195
195
with an exception object (leaving out the error checking for now)::
196
196
197
197
PyMODINIT_FUNC
198
-
initspam(void)
198
+
PyInit_spam(void)
199
199
{
200
200
PyObject *m;
201
201
202
-
m = Py_InitModule("spam", SpamMethods);
202
+
m = PyModule_Create(&spammodule);
203
203
if (m == NULL)
204
-
return;
204
+
return NULL;
205
205
206
206
SpamError = PyErr_NewException("spam.error", NULL, NULL);
207
207
Py_INCREF(SpamError);
208
208
PyModule_AddObject(m, "error", SpamError);
209
+
return m;
209
210
}
210
211
211
212
Note 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.
303
304
Use :cfunc:`PyArg_ParseTupleAndKeywords` to parse the arguments to such a
304
305
function.
305
306
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
307
319
initialization 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
309
321
only non-\ ``static`` item defined in the module file::
310
322
311
323
PyMODINIT_FUNC
312
-
initspam(void)
324
+
PyInit_spam(void)
313
325
{
314
-
(void) Py_InitModule("spam", SpamMethods);
326
+
return PyModule_Create(&spammodule);
315
327
}
316
328
317
329
Note that PyMODINIT_FUNC declares the function as ``void`` return type,
318
330
declares any special linkage declarations required by the platform, and for C++
319
331
declares the function as ``extern "C"``.
320
332
321
333
When 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
325
336
inserts 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
329
340
certain 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``.
331
343
332
-
When embedding Python, the :cfunc:`initspam` function is not called
344
+
When embedding Python, the :cfunc:`PyInit_spam` function is not called
333
345
automatically 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::
337
348
338
349
int
339
350
main(int argc, char *argv[])
340
351
{
352
+
/* Add a builtin module, before Py_Initialize */
353
+
PyImport_AppendInittab("spam", PyInit_spam);
354
+
341
355
/* Pass argv[0] to the Python interpreter */
342
356
Py_SetProgramName(argv[0]);
343
357
344
358
/* Initialize the Python interpreter. Required. */
345
359
Py_Initialize();
346
360
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");
349
365
350
366
An example may be found in the file :file:`Demo/embed/demo.c` in the Python
351
367
source distribution.
@@ -1154,15 +1170,15 @@ exporting module, not a client module. Finally, the module's initialization
1154
1170
function must take care of initializing the C API pointer array::
1155
1171
1156
1172
PyMODINIT_FUNC
1157
-
initspam(void)
1173
+
PyInit_spam(void)
1158
1174
{
1159
1175
PyObject *m;
1160
1176
static void *PySpam_API[PySpam_API_pointers];
1161
1177
PyObject *c_api_object;
1162
1178
1163
-
m = Py_InitModule("spam", SpamMethods);
1179
+
m = PyModule_Create(&spammodule);
1164
1180
if (m == NULL)
1165
-
return;
1181
+
return NULL;
1166
1182
1167
1183
/* Initialize the C API pointer array */
1168
1184
PySpam_API[PySpam_System_NUM] = (void *)PySpam_System;
@@ -1172,10 +1188,11 @@ function must take care of initializing the C API pointer array::
1172
1188
1173
1189
if (c_api_object != NULL)
1174
1190
PyModule_AddObject(m, "_C_API", c_api_object);
1191
+
return m;
1175
1192
}
1176
1193
1177
1194
Note 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!
1179
1196
1180
1197
The bulk of the work is in the header file :file:`spammodule.h`, which looks
1181
1198
like this::
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4