Skip to content

Commit d35a6b4

Browse files
committed
updates
1 parent 9e6bff5 commit d35a6b4

File tree

11 files changed

+53
-71
lines changed

11 files changed

+53
-71
lines changed

Doc/c-api/allocation.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,6 @@ Allocating Objects on the Heap
153153
154154
.. seealso::
155155
156-
:c:func:`PyModule_Create`
156+
:ref:`moduleobjects`
157157
To allocate and create extension modules.
158158

Doc/c-api/intro.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ complete listing.
129129
static struct PyModuleDef spam_module = {
130130
.m_base = PyModuleDef_HEAD_INIT,
131131
.m_name = "spam",
132-
.m_size = 0,
133132
...
134133
};
135134

Doc/extending/building.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ instance. See :ref:`initializing-modules` for details.
2323
.. highlight:: python
2424
2525
For modules with ASCII-only names, the function must be named
26-
``PyInit_<modulename>``, with ``<modulename>`` replaced by the name of the
27-
module. When using :ref:`multi-phase-initialization`, non-ASCII module names
26+
:samp:`PyInit_{<name>}`, with ``<name>`` replaced by the name of the module.
27+
When using :ref:`multi-phase-initialization`, non-ASCII module names
2828
are allowed. In this case, the initialization function name is
29-
``PyInitU_<modulename>``, with ``<modulename>`` encoded using Python's
29+
:samp:`PyInitU_{<name>}`, with ``<name>`` encoded using Python's
3030
*punycode* encoding with hyphens replaced by underscores. In Python::
3131

3232
def initfunc_name(name):

Doc/extending/embedding.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ Python extension. For example::
251251
{NULL, NULL, 0, NULL}
252252
};
253253

254-
struct PyModuleDef emb_module = {
254+
static struct PyModuleDef emb_module = {
255255
.m_base = PyModuleDef_HEAD_INIT,
256256
.m_name = "emb",
257257
.m_size = 0,

Doc/extending/extending.rst

Lines changed: 32 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -203,24 +203,32 @@ function usually raises :c:data:`PyExc_TypeError`. If you have an argument whos
203203
value must be in a particular range or must satisfy other conditions,
204204
:c:data:`PyExc_ValueError` is appropriate.
205205

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 usually declare an object variable at in the module state::
208208

209-
static PyObject *SpamError;
209+
typedef struct {
210+
// ...
211+
PyObject *SpamError;
212+
// ...
213+
} spam_state;
210214

211215
and initialize it in the module's :c:data:`Py_mod_exec` function
212-
(:c:func:`!spam_module_exec`)
213-
214-
with an exception object::
216+
(:c:func:`!spam_module_exec`) with an exception object::
215217

216218
static int
217219
spam_module_exec(PyObject *m)
218220
{
219-
SpamError = PyErr_NewException("spam.error", NULL, NULL);
220-
if (PyModule_AddObjectRef(m, "SpamError", SpamError) < 0) {
221+
spam_state *state = PyModule_GetState(m);
222+
if (state == NULL) {
223+
return -1;
224+
}
225+
state->SpamError = PyErr_NewException("spam.error", NULL, NULL);
226+
if (state->SpamError == NULL) {
227+
return -1;
228+
}
229+
if (PyModule_AddType(m, (PyTypeObject *)state->SpamError) < 0) {
221230
return -1;
222231
}
223-
224232
return 0;
225233
}
226234

@@ -232,7 +240,7 @@ with an exception object::
232240
static struct PyModuleDef spam_module = {
233241
.m_base = PyModuleDef_HEAD_INIT,
234242
.m_name = "spam",
235-
.m_size = 0, // non-negative
243+
.m_size = sizeof(spam_state),
236244
.m_slots = spam_module_slots,
237245
};
238246

@@ -436,18 +444,20 @@ optionally followed by an import of the module::
436444

437445
.. note::
438446

439-
If you declare a global variable or a local static one, the module can
440-
cause the same problems as the legacy single-phase initialization when
447+
If you declare a global variable or a local static one, the module may
448+
experience unintended side-effects on re-initialisation, for example when
441449
removing entries from ``sys.modules`` or importing compiled modules into
442-
multiple interpreters within a process (or following a :c:func:`fork` without an
443-
intervening :c:func:`exec`). In this case, at least the module should
444-
stop supporting subinterpreters through a :c:type:`PyModuleDef_Slot`
445-
(:c:data:`Py_mod_multiple_interpreters`).
450+
multiple interpreters within a process
451+
(or following a :c:func:`fork` without an intervening :c:func:`exec`).
452+
If module state is not yet fully :ref:`isolated <isolating-extensions-howto>`,
453+
authors should consider marking the module as having no support for subinterpreters
454+
(via :c:macro:`Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED`).
446455

447456
A more substantial example module is included in the Python source distribution
448457
as :file:`Modules/xxlimited.c`. This file may be used as a template or simply
449458
read as an example.
450459

460+
451461
.. _compilation:
452462

453463
Compilation and Linkage
@@ -1068,8 +1078,9 @@ why his :meth:`!__del__` methods would fail...
10681078

10691079
The second case of problems with a borrowed reference is a variant involving
10701080
threads. Normally, multiple threads in the Python interpreter can't get in each
1071-
other's way, because there is a global lock protecting Python's entire object
1072-
space. However, it is possible to temporarily release this lock using the macro
1081+
other's way, because there is a :term:`global lock <global interpreter lock>`
1082+
protecting Python's entire object space.
1083+
However, it is possible to temporarily release this lock using the macro
10731084
:c:macro:`Py_BEGIN_ALLOW_THREADS`, and to re-acquire it using
10741085
:c:macro:`Py_END_ALLOW_THREADS`. This is common around blocking I/O calls, to
10751086
let other threads use the processor while waiting for the I/O to complete.
@@ -1255,8 +1266,8 @@ two more lines must be added::
12551266
#include "spammodule.h"
12561267

12571268
The ``#define`` is used to tell the header file that it is being included in the
1258-
exporting module, not a client module. Finally, the module's initialization
1259-
function must take care of initializing the C API pointer array::
1269+
exporting module, not a client module. Finally, the module's :c:data:`mod_exec
1270+
<Py_mod_exec>` function must take care of initializing the C API pointer array::
12601271

12611272
static int
12621273
spam_module_exec(PyObject *m)
@@ -1277,24 +1288,6 @@ function must take care of initializing the C API pointer array::
12771288
return 0;
12781289
}
12791290

1280-
static PyModuleDef_Slot spam_module_slots[] = {
1281-
{Py_mod_exec, spam_module_exec},
1282-
{0, NULL}
1283-
};
1284-
1285-
static struct PyModuleDef spam_module = {
1286-
.m_base = PyModuleDef_HEAD_INIT,
1287-
.m_name = "spam",
1288-
.m_size = 0,
1289-
.m_slots = spam_module_slots,
1290-
};
1291-
1292-
PyMODINIT_FUNC
1293-
PyInit_spam(void)
1294-
{
1295-
return PyModuleDef_Init(&spam_module);
1296-
}
1297-
12981291
Note that ``PySpam_API`` is declared ``static``; otherwise the pointer
12991292
array would disappear when :c:func:`!PyInit_spam` terminates!
13001293

@@ -1351,7 +1344,7 @@ like this::
13511344

13521345
All that a client module must do in order to have access to the function
13531346
:c:func:`!PySpam_System` is to call the function (or rather macro)
1354-
:c:func:`!import_spam` in its initialization function::
1347+
:c:func:`!import_spam` in its :c:data:`mod_exec <Py_mod_exec>` function::
13551348

13561349
static int
13571350
client_module_exec(PyObject *m)
@@ -1362,24 +1355,6 @@ All that a client module must do in order to have access to the function
13621355
return 0;
13631356
}
13641357

1365-
static PyModuleDef_Slot client_module_slots[] = {
1366-
{Py_mod_exec, client_module_exec},
1367-
{0, NULL}
1368-
};
1369-
1370-
static struct PyModuleDef client_module = {
1371-
.m_base = PyModuleDef_HEAD_INIT,
1372-
.m_name = "client",
1373-
.m_size = 0,
1374-
.m_slots = client_module_slots,
1375-
};
1376-
1377-
PyMODINIT_FUNC
1378-
PyInit_client(void)
1379-
{
1380-
return PyModuleDef_Init(&client_module);
1381-
}
1382-
13831358
The main disadvantage of this approach is that the file :file:`spammodule.h` is
13841359
rather complicated. However, the basic structure is the same for each function
13851360
that is exported, so it has to be learned only once.

Doc/extending/newtypes_tutorial.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ from the previous chapter. This file defines three things:
5555
#. How the :class:`!Custom` **type** behaves: this is the ``CustomType`` struct,
5656
which defines a set of flags and function pointers that the interpreter
5757
inspects when specific operations are requested.
58-
#. How to initialize the :mod:`!custom` module: this is the ``PyInit_custom``
59-
function and the associated ``custommodule`` struct.
58+
#. How to define and execute the :mod:`!custom` module: this is the
59+
``PyInit_custom`` function and the associated ``custom_module`` struct for
60+
defining the module, and the ``custom_module_exec`` function to set up
61+
a fresh module object.
6062

6163
The first bit is::
6264

@@ -173,8 +175,9 @@ implementation provided by the API function :c:func:`PyType_GenericNew`. ::
173175
Everything else in the file should be familiar, except for some code in
174176
:c:func:`!custom_module_exec`::
175177

176-
if (PyType_Ready(&CustomType) < 0)
178+
if (PyType_Ready(&CustomType) < 0) {
177179
return -1;
180+
}
178181

179182
This initializes the :class:`!Custom` type, filling in a number of members
180183
to the appropriate default values, including :c:member:`~PyObject.ob_type` that we initially

Doc/includes/newtypes/custom.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ static PyTypeObject CustomType = {
1919
static int
2020
custom_module_exec(PyObject *m)
2121
{
22-
if (PyType_Ready(&CustomType) < 0)
22+
if (PyType_Ready(&CustomType) < 0) {
2323
return -1;
24+
}
2425

2526
if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) {
2627
return -1;

Doc/includes/newtypes/custom2.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ static PyTypeObject CustomType = {
109109
static int
110110
custom_module_exec(PyObject *m)
111111
{
112-
if (PyType_Ready(&CustomType) < 0)
112+
if (PyType_Ready(&CustomType) < 0) {
113113
return -1;
114+
}
114115

115116
if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) {
116117
return -1;

Doc/includes/newtypes/custom3.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,9 @@ static PyTypeObject CustomType = {
154154
static int
155155
custom_module_exec(PyObject *m)
156156
{
157-
if (PyType_Ready(&CustomType) < 0)
157+
if (PyType_Ready(&CustomType) < 0) {
158158
return -1;
159+
}
159160

160161
if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) {
161162
return -1;

Doc/includes/newtypes/custom4.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,9 @@ static PyTypeObject CustomType = {
173173
static int
174174
custom_module_exec(PyObject *m)
175175
{
176-
if (PyType_Ready(&CustomType) < 0)
176+
if (PyType_Ready(&CustomType) < 0) {
177177
return -1;
178+
}
178179

179180
if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) {
180181
return -1;

Doc/includes/newtypes/sublist.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ static int
4545
sublist_module_exec(PyObject *m)
4646
{
4747
SubListType.tp_base = &PyList_Type;
48-
if (PyType_Ready(&SubListType) < 0)
48+
if (PyType_Ready(&SubListType) < 0) {
4949
return -1;
50+
}
5051

5152
if (PyModule_AddObjectRef(m, "SubList", (PyObject *) &SubListType) < 0) {
5253
return -1;

0 commit comments

Comments
 (0)