Skip to content

Commit 67219f1

Browse files
committed
Another pass
1 parent 91d9a50 commit 67219f1

File tree

2 files changed

+108
-110
lines changed

2 files changed

+108
-110
lines changed

Doc/c-api/extension-modules.rst

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,45 @@ and must be named after the module name plus an extension listed in
2626

2727
.. _extension-export-hook:
2828

29+
Extension export hook
30+
.....................
31+
32+
.. versionadded:: next
33+
34+
Support for the :samp:`PyModExport_{<name>}` export hook was added in Python
35+
3.15. The older way of defining modules is still available: consult either
36+
the :ref:`extension-pyinit` section or earlier versions of this
37+
documentation if you plan to support earlier Python versions.
38+
2939
The export hook must be an exported function with the following signature:
3040

3141
.. c:function:: PyModuleDef_Slot *PyModExport_modulename(void)
3242
33-
Its name should be :samp:`PyModExport_{<name>}`, with ``<name>`` replaced by
34-
the name of the module.
35-
See :ref:`extension-export-hook-name` for full details.
43+
For modules with ASCII-only names, the :ref:`export hook <extension-export-hook>`
44+
must be named :samp:`PyModExport_{<name>}`,
45+
with ``<name>`` replaced by the module's name.
3646

37-
.. versionadded:: next
47+
For non-ASCII module names, the export hook must instead be named
48+
:samp:`PyModExportU_{<name>}` (note the ``U``), with ``<name>`` encoded using
49+
Python's *punycode* encoding with hyphens replaced by underscores. In Python:
50+
51+
.. code-block:: python
3852
39-
The :samp:`PyModExport_{<name>}` export hook was added in Python 3.15.
40-
The older way of defining modules is still available: consult either the
41-
:ref:`extension-pyinit` section or earlier versions of this documentation
42-
if you plan to support earlier Python versions.
53+
def hook_name(name):
54+
try:
55+
suffix = b'_' + name.encode('ascii')
56+
except UnicodeEncodeError:
57+
suffix = b'U_' + name.encode('punycode').replace(b'-', b'_')
58+
return b'PyModExport' + suffix
4359
4460
The export hook returns an array of :c:type:`PyModuleDef_Slot` entries,
4561
terminated by an entry with a slot ID of ``0``.
4662
These slots describe how the module should be created and initialized.
4763

4864
This array must remain valid and constant until interpreter shutdown.
49-
Typically, it should use ``static`` storage; for dynamic behavior you should
50-
use the :c:macro:`Py_mod_create` and :c:macro:`Py_mod_exec` slots.
65+
Typically, it should use ``static`` storage.
66+
Prefer using the :c:macro:`Py_mod_create` and :c:macro:`Py_mod_exec` slots
67+
for any dynamic behavior.
5168

5269
The export hook may return ``NULL`` with an exception set to signal failure.
5370

@@ -99,27 +116,6 @@ rather than crash, in common cases of ABI mismatch.
99116
See the `Multiple modules in one library <https://peps.python.org/pep-0489/#multiple-modules-in-one-library>`__
100117
section in :pep:`489` for details.
101118

102-
.. _extension-export-hook-name:
103-
104-
Export hook name
105-
................
106-
107-
For modules with ASCII-only names, the export hook must be named
108-
:samp:`PyModExport_{<name>}`, with ``<name>`` replaced by the module's name.
109-
110-
For non-ASCII module names, the export hook must be named
111-
:samp:`PyModExportU_{<name>}` (note the ``U``), with ``<name>`` encoded using
112-
Python's *punycode* encoding with hyphens replaced by underscores. In Python:
113-
114-
.. code-block:: python
115-
116-
def hook_name(name):
117-
try:
118-
suffix = b'_' + name.encode('ascii')
119-
except UnicodeEncodeError:
120-
suffix = b'U_' + name.encode('punycode').replace(b'-', b'_')
121-
return b'PyModExport' + suffix
122-
123119

124120
.. _multi-phase-initialization:
125121

@@ -208,7 +204,9 @@ an older-style :dfn:`initialization function` with the signature:
208204
Its name should be :samp:`PyInit_{<name>}`, with ``<name>`` replaced by the
209205
name of the module.
210206
For non-ASCII module names, use :samp:`PyInitU_{<name>}` instead, with
211-
``<name>`` encoded :ref:`as for the export hook <extension-export-hook-name>`.
207+
``<name>`` encoded in the same way as for the
208+
:ref:`export hook <extension-export-hook>` (that is, using Punycode
209+
with underscores).
212210

213211
If a module exports both :samp:`PyInit_{<name>}` and
214212
:samp:`PyModExport_{<name>}`, the :samp:`PyInit_{<name>}` function
@@ -332,11 +330,6 @@ in the following ways:
332330
A single-phase ``PyInit_modulename`` function should create “its” module
333331
object as soon as possible, before any other module objects can be created.
334332

335-
* Attempts to import a single-phase initialization module reentrantly
336-
from its own initialization function are likely to cause infinite recursion.
337-
(The extension author may prevent this by manually inserting a partially
338-
initialized module object in :py:attr:`sys.modules`.)
339-
340333
* Non-ASCII module names (``PyInitU_modulename``) are not supported.
341334

342335
* Single-phase modules support module lookup functions like

Doc/c-api/module.rst

Lines changed: 78 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
.. _moduleobjects:
44

55
Module Objects
6-
--------------
6+
==============
77

88
.. index:: pair: object; module
99

10-
1110
.. c:var:: PyTypeObject PyModule_Type
1211
1312
.. index:: single: ModuleType (in module types)
@@ -139,8 +138,9 @@ Module Objects
139138
Module definition
140139
-----------------
141140
142-
Modules defined using the C API are typically defined using an array of *slots*
143-
The slots provide a “description" of how a module should be created.
141+
Modules created using the C API are typically defined using an
142+
array of :dfn:`slots`.
143+
The slots provide a "description" of how a module should be created.
144144
145145
.. versionchanged:: next
146146
@@ -162,7 +162,7 @@ in an array of slots.
162162
163163
.. c:member:: int slot
164164
165-
A slot ID, chosen from the available values explained below.
165+
A slot ID, chosen from the available ``Py_mod_*`` values explained below.
166166
167167
An ID of 0 marks the end of a :c:type:`!PyModuleDef_Slot` array.
168168
@@ -175,11 +175,9 @@ in an array of slots.
175175
176176
.. versionadded:: 3.5
177177
178-
The available slot types are:
179-
180178
181-
Description slots
182-
.................
179+
Metadata slots
180+
..............
183181
184182
.. c:macro:: Py_mod_name
185183
@@ -213,9 +211,6 @@ Feature slots
213211
A pointer to a :c:struct:`PyABIInfo` structure that describes the ABI that
214212
the extension is using.
215213
216-
When the module is loaded, the :c:struct:`!PyABIInfo` in this slot is checked
217-
using :c:func:`PyABIInfo_Check`.
218-
219214
A suitable :c:struct:`!PyABIInfo` variable can be defined using the
220215
:c:macro:`PyABIInfo_VAR` macro, as in:
221216
@@ -228,6 +223,9 @@ Feature slots
228223
...
229224
};
230225
226+
When creating a module, Python checks the value of this slot
227+
using :c:func:`PyABIInfo_Check`.
228+
231229
.. versionadded:: 3.15
232230
233231
.. c:macro:: Py_mod_multiple_interpreters
@@ -255,9 +253,6 @@ Feature slots
255253
This slot determines whether or not importing this module
256254
in a subinterpreter will fail.
257255
258-
Multiple ``Py_mod_multiple_interpreters`` slots may not be specified
259-
in one module definition.
260-
261256
If ``Py_mod_multiple_interpreters`` is not specified, the import
262257
machinery defaults to ``Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED``.
263258
@@ -283,8 +278,6 @@ Feature slots
283278
this module will cause the GIL to be automatically enabled. See
284279
:ref:`whatsnew313-free-threaded-cpython` for more detail.
285280
286-
Multiple ``Py_mod_gil`` slots may not be specified in one module definition.
287-
288281
If ``Py_mod_gil`` is not specified, the import machinery defaults to
289282
``Py_MOD_GIL_USED``.
290283
@@ -306,8 +299,8 @@ Creation and initialization slots
306299
The function will be called with:
307300
308301
- *spec*: a ``ModuleSpec``-like object, meaning that any attributes defined
309-
for :py:class:`importlib.machinery.ModuleSpec`
310-
have matching semantics. However, any of the attributes may be missing.
302+
for :py:class:`importlib.machinery.ModuleSpec` have matching semantics.
303+
However, any of the attributes may be missing.
311304
- *def*: ``NULL``, or the module definition if the module is created from one.
312305
313306
The function should return a new module object, or set an error
@@ -324,21 +317,27 @@ Creation and initialization slots
324317
names through symlinks, all while sharing a single module definition.
325318
326319
There is no requirement for the returned object to be an instance of
327-
:c:type:`PyModule_Type`. Any type can be used, as long as it supports
328-
setting and getting import-related attributes.
329-
However, only ``PyModule_Type`` instances may be returned if the
330-
``PyModuleDef`` has non-``NULL`` ``m_traverse``, ``m_clear``,
331-
``m_free``; non-zero ``m_size``; or slots other than ``Py_mod_create``.
320+
:c:type:`PyModule_Type`.
321+
However, some slots may only be used with
322+
:c:type:`!PyModule_Type` instances; in particular:
323+
324+
- :c:macro:`Py_mod_exec`,
325+
- :ref:`module state slots <ext-module-state-slots>` (``Py_mod_state_*``),
326+
- :c:macro:`Py_mod_token`.
332327
333328
.. versionchanged:: next
334329
335-
The *slots* argument may be ``ModuleSpec``-like object, rather than
330+
The *slots* argument may be a ``ModuleSpec``-like object, rather than
336331
a true :py:class:`~importlib.machinery.ModuleSpec` instance.
337332
Note that previous versions of CPython did not enforce this.
338333
334+
The *def* argument may now be ``NULL``, since modules are not necessarily
335+
made from definitions.
336+
339337
.. c:macro:: Py_mod_exec
340338
341-
Specifies a function that is called to *execute* the module.
339+
Specifies a function that is called to :dfn:`execute`, or initialize,
340+
the module.
342341
This is equivalent to executing the code of a Python module: typically,
343342
this function adds classes and constants to the module.
344343
The signature of the function is:
@@ -380,13 +379,63 @@ Creation and initialization slots
380379
381380
Use :c:member:`PyModuleDef.m_methods` instead to support previous versions.
382381
382+
.. _ext-module-state:
383+
384+
Module state
385+
------------
386+
387+
Extension modules can have *module state* -- a
388+
piece of memory that is allocated on module creation,
389+
and freed when the module object is deallocated.
390+
The module state is specified using :ref:`dedicated slots <ext-module-state-slots>`.
391+
392+
A typical use of module state is storing an exception type -- or indeed *any*
393+
type object defined by the module --
394+
395+
Unlike the module's Python attributes, Python code cannot replace or delete
396+
data stored in module state.
397+
398+
Keeping per-module information in attributes and module state, rather than in
399+
static globals, makes module objects *isolated* and safer for use in
400+
multiple sub-interpreters.
401+
It also helps Python do an orderly clean-up when it shuts down.
402+
403+
Extensions that keep references to Python objects as part of module state must
404+
implement :c:macro:`Py_mod_state_traverse` and :c:macro:`Py_mod_state_clear`
405+
functions to avoid reference leaks.
406+
407+
To retrieve the state from a given module, use the following functions:
408+
409+
.. c:function:: void* PyModule_GetState(PyObject *module)
410+
411+
Return the "state" of the module, that is, a pointer to the block of memory
412+
allocated at module creation time, or ``NULL``. See
413+
:c:macro:`Py_mod_state_size`.
414+
415+
On error, return ``NULL`` with an exception set.
416+
Use :c:func:`PyErr_Occurred` to tell this case apart from missing
417+
module state.
418+
419+
420+
.. c:function:: int PyModule_GetStateSize(PyObject *, Py_ssize_t *result)
421+
422+
Set *\*result* to the size of the module's state, as specified using
423+
:c:macro:`Py_mod_state_size` (or :c:member:`PyModuleDef.m_size`),
424+
and return 0.
425+
426+
On error, set *\*result* to -1, and return -1 with an exception set.
427+
428+
.. versionadded:: next
429+
430+
383431
384432
.. _ext-module-state-slots:
385433
386434
Slots for defining module state
387435
...............................
388436
389-
See :ref:`ext-module-state` for an explanation of module state.
437+
The following :c:member:`PyModuleDef_Slot.slot` IDs are available for
438+
defining the module state.
390439
391440
.. c:macro:: Py_mod_state_size
392441
@@ -400,7 +449,8 @@ See :ref:`ext-module-state` for an explanation of module state.
400449
module does not support sub-interpreters, because it has global state.
401450
Negative *size* is only allowed when using
402451
:ref:`legacy single-phase initialization <single-phase-initialization>`
403-
or when :ref:`creating modules dynamically <module-from-slots>`.
452+
or when creating modules dynamically using :c:func:`PyModule_Create`
453+
or :c:func:`PyModule_Create2`.
404454
405455
See :PEP:`3121` for more details.
406456
@@ -505,51 +555,6 @@ See :ref:`ext-module-state` for an explanation of module state.
505555
506556
.. versionadded:: next
507557
508-
.. _ext-module-state:
509-
510-
Module state
511-
------------
512-
513-
Extension modules can have *module state* -- a
514-
piece of memory that is allocated on module creation,
515-
and freed when the module object is deallocated.
516-
The module state is specified using :ref:`dedicated slots <ext-module-state-slots>`.
517-
518-
Unlike the module's Python attributes, Python code cannot replace or delete
519-
data stored in module state.
520-
521-
Keeping per-module information in attributes and module state, rather than in
522-
static globals, makes module objects *isolated* and safer for use in
523-
multiple sub-interpreters.
524-
It also helps Python do an orderly clean-up when it shuts down.
525-
526-
Extensions that keep references to Python objects as part of module state must
527-
implement :c:macro:`Py_mod_state_traverse` and :c:macro:`Py_mod_state_clear`
528-
functions to avoid reference leaks.
529-
530-
To retrieve the state from a given module, use the following functions:
531-
532-
.. c:function:: void* PyModule_GetState(PyObject *module)
533-
534-
Return the "state" of the module, that is, a pointer to the block of memory
535-
allocated at module creation time, or ``NULL``. See
536-
:c:macro:`Py_mod_state_size`.
537-
538-
On error, return ``NULL`` with an exception set.
539-
Use :c:func:`PyErr_Occurred` to tell this case apart from a missing
540-
:c:type:`!PyModuleDef`.
541-
542-
543-
.. c:function:: int PyModule_GetStateSize(PyObject *, Py_ssize_t *result)
544-
545-
Set *\*result* to the size of the module's state, as specified using
546-
:c:macro:`Py_mod_state_size` (or :c:member:`PyModuleDef.m_size`),
547-
and return 0.
548-
549-
On error, set *\*result* to -1, and return -1 with an exception set.
550-
551-
.. versionadded:: next
552-
553558
554559
.. _ext-module-token:
555560

0 commit comments

Comments
 (0)