@@ -203,31 +203,42 @@ function usually raises :c:data:`PyExc_TypeError`. If you have an argument whos
203
203
value must be in a particular range or must satisfy other conditions,
204
204
:c:data: `PyExc_ValueError ` is appropriate.
205
205
206
- You can also define a new exception that is unique to your module. For this, you
207
- usually declare a static object variable at the beginning of your file::
206
+ You can also define a new exception that is unique to your module.
207
+ For this, you can declare a static global object variable at the beginning
208
+ of the file::
208
209
209
210
static PyObject *SpamError;
210
211
211
- and initialize it in your module's initialization function ( :c:func: ` !PyInit_spam `)
212
- with an exception object ::
212
+ and initialize it with an exception object in the module's
213
+ :c:data: ` Py_mod_exec ` function ( :c:func: ` !spam_module_exec `) ::
213
214
214
- PyMODINIT_FUNC
215
- PyInit_spam(void )
215
+ static int
216
+ spam_module_exec(PyObject *m )
216
217
{
217
- PyObject *m;
218
-
219
- m = PyModule_Create(&spammodule);
220
- if (m == NULL)
221
- return NULL;
222
-
223
218
SpamError = PyErr_NewException("spam.error", NULL, NULL);
224
- if (PyModule_AddObjectRef(m, "error", SpamError) < 0) {
225
- Py_CLEAR(SpamError);
226
- Py_DECREF(m);
227
- return NULL;
219
+ if (PyModule_AddObjectRef(m, "SpamError", SpamError) < 0) {
220
+ return -1;
228
221
}
229
222
230
- return m;
223
+ return 0;
224
+ }
225
+
226
+ static PyModuleDef_Slot spam_module_slots[] = {
227
+ {Py_mod_exec, spam_module_exec},
228
+ {0, NULL}
229
+ };
230
+
231
+ static struct PyModuleDef spam_module = {
232
+ .m_base = PyModuleDef_HEAD_INIT,
233
+ .m_name = "spam",
234
+ .m_size = 0, // non-negative
235
+ .m_slots = spam_module_slots,
236
+ };
237
+
238
+ PyMODINIT_FUNC
239
+ PyInit_spam(void)
240
+ {
241
+ return PyModuleDef_Init(&spam_module);
231
242
}
232
243
233
244
Note that the Python name for the exception object is :exc: `!spam.error `. The
@@ -318,7 +329,7 @@ The Module's Method Table and Initialization Function
318
329
I promised to show how :c:func: `!spam_system ` is called from Python programs.
319
330
First, we need to list its name and address in a "method table"::
320
331
321
- static PyMethodDef SpamMethods [] = {
332
+ static PyMethodDef spam_methods [] = {
322
333
...
323
334
{"system", spam_system, METH_VARARGS,
324
335
"Execute a shell command."},
@@ -343,13 +354,10 @@ function.
343
354
344
355
The method table must be referenced in the module definition structure::
345
356
346
- static struct PyModuleDef spammodule = {
347
- PyModuleDef_HEAD_INIT,
348
- "spam", /* name of module */
349
- spam_doc, /* module documentation, may be NULL */
350
- -1, /* size of per-interpreter state of the module,
351
- or -1 if the module keeps state in global variables. */
352
- SpamMethods
357
+ static struct PyModuleDef spam_module = {
358
+ ...
359
+ .m_methods = spam_methods,
360
+ ...
353
361
};
354
362
355
363
This structure, in turn, must be passed to the interpreter in the module's
@@ -360,23 +368,17 @@ only non-\ ``static`` item defined in the module file::
360
368
PyMODINIT_FUNC
361
369
PyInit_spam(void)
362
370
{
363
- return PyModule_Create(&spammodule );
371
+ return PyModuleDef_Init(&spam_module );
364
372
}
365
373
366
374
Note that :c:macro: `PyMODINIT_FUNC ` declares the function as ``PyObject * `` return type,
367
375
declares any special linkage declarations required by the platform, and for C++
368
376
declares the function as ``extern "C" ``.
369
377
370
- When the Python program imports module :mod: `!spam ` for the first time,
371
- :c:func: `!PyInit_spam ` is called. (See below for comments about embedding Python.)
372
- It calls :c:func: `PyModule_Create `, which returns a module object, and
373
- inserts built-in function objects into the newly created module based upon the
374
- table (an array of :c:type: `PyMethodDef ` structures) found in the module definition.
375
- :c:func: `PyModule_Create ` returns a pointer to the module object
376
- that it creates. It may abort with a fatal error for
377
- certain errors, or return ``NULL `` if the module could not be initialized
378
- satisfactorily. The init function must return the module object to its caller,
379
- so that it then gets inserted into ``sys.modules ``.
378
+ :c:func: `!PyInit_spam ` is called when each interpreter imports its module
379
+ :mod: `!spam ` for the first time. (See below for comments about embedding Python.)
380
+ A pointer to the module definition must be returned via :c:func: `PyModuleDef_Init `,
381
+ so that the import machinery can create the module and store it in ``sys.modules ``.
380
382
381
383
When embedding Python, the :c:func: `!PyInit_spam ` function is not called
382
384
automatically unless there's an entry in the :c:data: `PyImport_Inittab ` table.
@@ -433,23 +435,19 @@ optionally followed by an import of the module::
433
435
434
436
.. note ::
435
437
436
- Removing entries from ``sys.modules `` or importing compiled modules into
437
- multiple interpreters within a process (or following a :c:func: `fork ` without an
438
- intervening :c:func: `exec `) can create problems for some extension modules.
439
- Extension module authors should exercise caution when initializing internal data
440
- structures.
438
+ If you declare a global variable or a local static one, the module may
439
+ experience unintended side-effects on re-initialisation, for example when
440
+ removing entries from ``sys.modules `` or importing compiled modules into
441
+ multiple interpreters within a process
442
+ (or following a :c:func: `fork ` without an intervening :c:func: `exec `).
443
+ If module state is not yet fully :ref: `isolated <isolating-extensions-howto >`,
444
+ authors should consider marking the module as having no support for subinterpreters
445
+ (via :c:macro: `Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED `).
441
446
442
447
A more substantial example module is included in the Python source distribution
443
- as :file: `Modules/xxmodule .c `. This file may be used as a template or simply
448
+ as :file: `Modules/xxlimited .c `. This file may be used as a template or simply
444
449
read as an example.
445
450
446
- .. note ::
447
-
448
- Unlike our ``spam `` example, ``xxmodule `` uses *multi-phase initialization *
449
- (new in Python 3.5), where a PyModuleDef structure is returned from
450
- ``PyInit_spam ``, and creation of the module is left to the import machinery.
451
- For details on multi-phase initialization, see :PEP: `489 `.
452
-
453
451
454
452
.. _compilation :
455
453
790
788
{NULL, NULL, 0, NULL} /* sentinel */
791
789
};
792
790
793
- static struct PyModuleDef keywdargmodule = {
794
- PyModuleDef_HEAD_INIT,
795
- "keywdarg",
796
- NULL,
797
- -1,
798
- keywdarg_methods
791
+ static struct PyModuleDef keywdarg_module = {
792
+ .m_base = PyModuleDef_HEAD_INIT,
793
+ .m_name = "keywdarg",
794
+ .m_size = 0,
795
+ .m_methods = keywdarg_methods,
799
796
};
800
797
801
798
PyMODINIT_FUNC
802
799
PyInit_keywdarg(void)
803
800
{
804
- return PyModule_Create(&keywdargmodule );
801
+ return PyModuleDef_Init(&keywdarg_module );
805
802
}
806
803
807
804
@@ -1072,8 +1069,9 @@ why his :meth:`!__del__` methods would fail...
1072
1069
1073
1070
The second case of problems with a borrowed reference is a variant involving
1074
1071
threads. Normally, multiple threads in the Python interpreter can't get in each
1075
- other's way, because there is a global lock protecting Python's entire object
1076
- space. However, it is possible to temporarily release this lock using the macro
1072
+ other's way, because there is a :term: `global lock <global interpreter lock> `
1073
+ protecting Python's entire object space.
1074
+ However, it is possible to temporarily release this lock using the macro
1077
1075
:c:macro: `Py_BEGIN_ALLOW_THREADS `, and to re-acquire it using
1078
1076
:c:macro: `Py_END_ALLOW_THREADS `. This is common around blocking I/O calls, to
1079
1077
let other threads use the processor while waiting for the I/O to complete.
@@ -1259,32 +1257,26 @@ two more lines must be added::
1259
1257
#include "spammodule.h"
1260
1258
1261
1259
The ``#define `` is used to tell the header file that it is being included in the
1262
- exporting module, not a client module. Finally, the module's initialization
1263
- function must take care of initializing the C API pointer array::
1260
+ exporting module, not a client module. Finally, the module's :c:data: ` mod_exec
1261
+ <Py_mod_exec> ` function must take care of initializing the C API pointer array::
1264
1262
1265
- PyMODINIT_FUNC
1266
- PyInit_spam(void )
1263
+ static int
1264
+ spam_module_exec(PyObject * m )
1267
1265
{
1268
- PyObject *m;
1269
1266
static void *PySpam_API[PySpam_API_pointers];
1270
1267
PyObject *c_api_object;
1271
1268
1272
- m = PyModule_Create(&spammodule);
1273
- if (m == NULL)
1274
- return NULL;
1275
-
1276
1269
/* Initialize the C API pointer array */
1277
1270
PySpam_API[PySpam_System_NUM] = (void *)PySpam_System;
1278
1271
1279
1272
/* Create a Capsule containing the API pointer array's address */
1280
1273
c_api_object = PyCapsule_New((void *)PySpam_API, "spam._C_API", NULL);
1281
1274
1282
1275
if (PyModule_Add(m, "_C_API", c_api_object) < 0) {
1283
- Py_DECREF(m);
1284
- return NULL;
1276
+ return -1;
1285
1277
}
1286
1278
1287
- return m ;
1279
+ return 0 ;
1288
1280
}
1289
1281
1290
1282
Note that ``PySpam_API `` is declared ``static ``; otherwise the pointer
@@ -1343,20 +1335,16 @@ like this::
1343
1335
1344
1336
All that a client module must do in order to have access to the function
1345
1337
:c:func: `!PySpam_System ` is to call the function (or rather macro)
1346
- :c:func: `!import_spam ` in its initialization function::
1338
+ :c:func: `!import_spam ` in its :c:data: ` mod_exec <Py_mod_exec> ` function::
1347
1339
1348
- PyMODINIT_FUNC
1349
- PyInit_client(void )
1340
+ static int
1341
+ client_module_exec(PyObject *m )
1350
1342
{
1351
- PyObject *m;
1352
-
1353
- m = PyModule_Create(&clientmodule);
1354
- if (m == NULL)
1355
- return NULL;
1356
- if (import_spam() < 0)
1357
- return NULL;
1343
+ if (import_spam() < 0) {
1344
+ return -1;
1345
+ }
1358
1346
/* additional initialization can happen here */
1359
- return m ;
1347
+ return 0 ;
1360
1348
}
1361
1349
1362
1350
The main disadvantage of this approach is that the file :file: `spammodule.h ` is
0 commit comments