Skip to content

Commit 0142b5d

Browse files
committed
pythongh-140550: PEP 793 reference documentation
Since the PEP calls for soft-deprecation of the existing initialization function, this reorganizes the relevant docs to put the new way of doing things first, and de-emphasize the old. Some bits, like the tutorial, are left out of this patch. (See the issue for a list.)
1 parent ffd6473 commit 0142b5d

File tree

6 files changed

+686
-265
lines changed

6 files changed

+686
-265
lines changed

Doc/c-api/extension-modules.rst

Lines changed: 176 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Defining extension modules
88
A C extension for CPython is a shared library (for example, a ``.so`` file
99
on Linux, ``.pyd`` DLL on Windows), which is loadable into the Python process
1010
(for example, it is compiled with compatible compiler settings), and which
11-
exports an :ref:`initialization function <extension-export-hook>`.
11+
exports an :dfn:`export hook` function (or an
12+
old-style :ref:`initialization function <extension-pyinit>`).
1213

1314
To be importable by default (that is, by
1415
:py:class:`importlib.machinery.ExtensionFileLoader`),
@@ -23,25 +24,131 @@ and must be named after the module name plus an extension listed in
2324
One suitable tool is Setuptools, whose documentation can be found at
2425
https://setuptools.pypa.io/en/latest/setuptools.html.
2526

26-
Normally, the initialization function returns a module definition initialized
27-
using :c:func:`PyModuleDef_Init`.
28-
This allows splitting the creation process into several phases:
27+
.. _extension-export-hook:
28+
29+
The export hook must be an exported function with the following signature:
30+
31+
.. c:function:: PyModuleDef_Slot *PyModExport_modulename(void)
32+
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.
36+
37+
.. versionadded:: next
38+
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.
43+
44+
The export hook returns an array of :c:type:`PyModuleDef_Slot` entries,
45+
terminated by an entry with a slot ID of ``0``.
46+
These slots describe how the module should be created and initialized.
47+
48+
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.
51+
52+
The export hook may return ``NULL`` with an exception set to signal failure.
53+
54+
It is recommended to define the export hook function using a helper macro:
55+
56+
.. c:macro:: PyMODEXPORT_FUNC
57+
58+
Declare an extension module export hook.
59+
This macro:
2960

61+
* specifies the :c:expr:`PyModuleDef_Slot*` return type,
62+
* adds any special linkage declarations required by the platform, and
63+
* for C++, declares the function as ``extern "C"``.
64+
65+
For example, a module called ``spam`` would be defined like this::
66+
67+
PyABIInfo_VAR(abi_info);
68+
69+
static PyModuleDef_Slot spam_slots[] = {
70+
{Py_mod_abi, &abi_info},
71+
{Py_mod_name, "spam"},
72+
{Py_mod_init, spam_init_function},
73+
...
74+
{0, NULL},
75+
};
76+
77+
PyMODEXPORT_FUNC
78+
PyModExport_spam(void)
79+
{
80+
return spam_slots;
81+
}
82+
83+
The export hook is typically the only non-\ ``static``
84+
item defined in the module's C source.
85+
86+
The hook should be kept short -- ideally, one line as above.
87+
If you do need to use Python C API in this function, it is recommended to call
88+
:c:expr:`PyABIInfo_Check(&abi_info, "modulename")` first to raise an exception,
89+
rather than crash, in common cases of ABI mismatch.
90+
91+
92+
.. note::
93+
94+
It is possible to export multiple modules from a single shared library by
95+
defining multiple export hooks.
96+
However, importing them requires a custom importer or suitably named
97+
copies/links of the extension file, because Python's import machinery only
98+
finds the function corresponding to the filename.
99+
See the `Multiple modules in one library <https://peps.python.org/pep-0489/#multiple-modules-in-one-library>`__
100+
section in :pep:`489` for details.
101+
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+
123+
124+
.. _multi-phase-initialization:
125+
126+
Multi-phase initialization
127+
..........................
128+
129+
The process of creating an extension module follows several phases:
130+
131+
- Python finds and calls the export hook to get information on how to
132+
create the module.
30133
- Before any substantial code is executed, Python can determine which
31134
capabilities the module supports, and it can adjust the environment or
32135
refuse loading an incompatible extension.
33-
- By default, Python itself creates the module object -- that is, it does
34-
the equivalent of :py:meth:`object.__new__` for classes.
35-
It also sets initial attributes like :attr:`~module.__package__` and
36-
:attr:`~module.__loader__`.
37-
- Afterwards, the module object is initialized using extension-specific
38-
code -- the equivalent of :py:meth:`~object.__init__` on classes.
136+
Slots like :c:data:`Py_mod_abi`, :c:data:`Py_mod_gil` and
137+
:c:data:`Py_mod_multiple_interpreters` influence this step.
138+
- By default, Python itself then creates the module object -- that is, it does
139+
the equivalent of calling :py:meth:`~object.__new__` when creating an object.
140+
This step can be overridden using the :c:data:`Py_mod_create` slot.
141+
- Python sets initial module attributes like :attr:`~module.__package__` and
142+
:attr:`~module.__loader__`, and inserts the module object into
143+
:py:attr:`sys.modules`.
144+
- Afterwards, the module object is initialized in an extension-specific way
145+
-- the equivalent of :py:meth:`~object.__init__` when creating an object,
146+
or of executing top-level code in a Python-language module.
147+
The behavior is specified using the :c:data:`Py_mod_exec` slot.
39148

40149
This is called *multi-phase initialization* to distinguish it from the legacy
41-
(but still supported) *single-phase initialization* scheme,
42-
where the initialization function returns a fully constructed module.
43-
See the :ref:`single-phase-initialization section below <single-phase-initialization>`
44-
for details.
150+
(but still supported) :ref:`single-phase initialization <single-phase-initialization>`,
151+
where an initialization function returns a fully constructed module.
45152

46153
.. versionchanged:: 3.5
47154

@@ -53,7 +160,7 @@ Multiple module instances
53160

54161
By default, extension modules are not singletons.
55162
For example, if the :py:attr:`sys.modules` entry is removed and the module
56-
is re-imported, a new module object is created, and typically populated with
163+
is re-imported, a new module object is created and, typically, populated with
57164
fresh method and type objects.
58165
The old module is subject to normal garbage collection.
59166
This mirrors the behavior of pure-Python modules.
@@ -83,36 +190,32 @@ A module may also be limited to the main interpreter using
83190
the :c:data:`Py_mod_multiple_interpreters` slot.
84191

85192

86-
.. _extension-export-hook:
193+
.. _extension-pyinit:
87194

88-
Initialization function
89-
.......................
195+
``PyInit`` function
196+
...................
90197

91-
The initialization function defined by an extension module has the
92-
following signature:
198+
.. deprecated:: next
199+
200+
This functionality is :term:`soft deprecated`.
201+
It will not get new features, but there are no plans to remove it.
202+
203+
Instead of :c:func:`PyModExport_modulename`, an extension module can define
204+
an older-style :dfn:`initialization function` with the signature:
93205

94206
.. c:function:: PyObject* PyInit_modulename(void)
95207
96208
Its name should be :samp:`PyInit_{<name>}`, with ``<name>`` replaced by the
97209
name of the module.
210+
For non-ASCII module names, use :samp:`PyInitU_{<name>}` instead, with
211+
``<name>`` encoded :ref:`as for the export hook <extension-export-hook-name>`.
98212

99-
For modules with ASCII-only names, the function must instead be named
100-
:samp:`PyInit_{<name>}`, with ``<name>`` replaced by the name of the module.
101-
When using :ref:`multi-phase-initialization`, non-ASCII module names
102-
are allowed. In this case, the initialization function name is
103-
:samp:`PyInitU_{<name>}`, with ``<name>`` encoded using Python's
104-
*punycode* encoding with hyphens replaced by underscores. In Python:
213+
If a module exports both :samp:`PyInit_{<name>}` and
214+
:samp:`PyModExport_{<name>}`, the :samp:`PyInit_{<name>}` function
215+
is ignored.
105216

106-
.. code-block:: python
107-
108-
def initfunc_name(name):
109-
try:
110-
suffix = b'_' + name.encode('ascii')
111-
except UnicodeEncodeError:
112-
suffix = b'U_' + name.encode('punycode').replace(b'-', b'_')
113-
return b'PyInit' + suffix
114-
115-
It is recommended to define the initialization function using a helper macro:
217+
Like with :c:macro:`PyMODEXPORT_FUNC`, it is recommended to define the
218+
initialization function using a helper macro:
116219

117220
.. c:macro:: PyMODINIT_FUNC
118221
@@ -123,51 +226,24 @@ It is recommended to define the initialization function using a helper macro:
123226
* adds any special linkage declarations required by the platform, and
124227
* for C++, declares the function as ``extern "C"``.
125228

126-
For example, a module called ``spam`` would be defined like this::
127-
128-
static struct PyModuleDef spam_module = {
129-
.m_base = PyModuleDef_HEAD_INIT,
130-
.m_name = "spam",
131-
...
132-
};
133-
134-
PyMODINIT_FUNC
135-
PyInit_spam(void)
136-
{
137-
return PyModuleDef_Init(&spam_module);
138-
}
139-
140-
It is possible to export multiple modules from a single shared library by
141-
defining multiple initialization functions. However, importing them requires
142-
using symbolic links or a custom importer, because by default only the
143-
function corresponding to the filename is found.
144-
See the `Multiple modules in one library <https://peps.python.org/pep-0489/#multiple-modules-in-one-library>`__
145-
section in :pep:`489` for details.
146-
147-
The initialization function is typically the only non-\ ``static``
148-
item defined in the module's C source.
149-
150229

151-
.. _multi-phase-initialization:
230+
Normally, the initialization function (``PyInit_modulename``) returns
231+
a :c:type:`PyModuleDef` instance with non-``NULL``
232+
:c:member:`~PyModuleDef.m_slots`. This allows Python to use
233+
:ref:`multi-phase initialization <multi-phase-initialization>`.
152234

153-
Multi-phase initialization
154-
..........................
155-
156-
Normally, the :ref:`initialization function <extension-export-hook>`
157-
(``PyInit_modulename``) returns a :c:type:`PyModuleDef` instance with
158-
non-``NULL`` :c:member:`~PyModuleDef.m_slots`.
159235
Before it is returned, the ``PyModuleDef`` instance must be initialized
160236
using the following function:
161237

162-
163238
.. c:function:: PyObject* PyModuleDef_Init(PyModuleDef *def)
164239
165240
Ensure a module definition is a properly initialized Python object that
166241
correctly reports its type and a reference count.
167242
168243
Return *def* cast to ``PyObject*``, or ``NULL`` if an error occurred.
169244
170-
Calling this function is required for :ref:`multi-phase-initialization`.
245+
Calling this function is required before returning a :c:type:`PyModuleDef`
246+
from a module initialization function.
171247
It should not be used in other contexts.
172248
173249
Note that Python assumes that ``PyModuleDef`` structures are statically
@@ -178,18 +254,37 @@ using the following function:
178254
.. versionadded:: 3.5
179255
180256
257+
For example, a module called ``spam`` would be defined like this::
258+
259+
static struct PyModuleDef spam_module = {
260+
.m_base = PyModuleDef_HEAD_INIT,
261+
.m_name = "spam",
262+
...
263+
};
264+
265+
PyMODINIT_FUNC
266+
PyInit_spam(void)
267+
{
268+
return PyModuleDef_Init(&spam_module);
269+
}
270+
271+
181272
.. _single-phase-initialization:
182273
183274
Legacy single-phase initialization
184-
..................................
275+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
276+
277+
.. deprecated:: next
185278
186-
.. attention::
187-
Single-phase initialization is a legacy mechanism to initialize extension
279+
Single-phase initialization is :term:`soft deprecated`.
280+
It is a legacy mechanism to initialize extension
188281
modules, with known drawbacks and design flaws. Extension module authors
189282
are encouraged to use multi-phase initialization instead.
190283
191-
In single-phase initialization, the
192-
:ref:`initialization function <extension-export-hook>` (``PyInit_modulename``)
284+
However, there are no plans to remove support for it.
285+
286+
In single-phase initialization, the old-style
287+
:ref:`initializaton function <extension-pyinit>` (``PyInit_modulename``)
193288
should create, populate and return a module object.
194289
This is typically done using :c:func:`PyModule_Create` and functions like
195290
:c:func:`PyModule_AddObjectRef`.
@@ -237,11 +332,18 @@ in the following ways:
237332
A single-phase ``PyInit_modulename`` function should create “its” module
238333
object as soon as possible, before any other module objects can be created.
239334

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 `sys.modules`.)
339+
240340
* Non-ASCII module names (``PyInitU_modulename``) are not supported.
241341

242342
* Single-phase modules support module lookup functions like
243343
:c:func:`PyState_FindModule`.
244344

345+
* The module's :c:member:`PyModuleDef.m_slots` must be NULL.
346+
245347
.. [#testsinglephase] ``_testsinglephase`` is an internal module used
246348
in CPython's self-test suite; your installation may or may not
247349
include it.

Doc/c-api/init.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1680,7 +1680,8 @@ function. You can create and destroy them using the following functions:
16801680
Only C-level static and global variables are shared between these
16811681
module objects.
16821682
1683-
* For modules using single-phase initialization,
1683+
* For modules using legacy
1684+
:ref:`single-phase initialization <single-phase-initialization>`,
16841685
e.g. :c:func:`PyModule_Create`, the first time a particular extension
16851686
is imported, it is initialized normally, and a (shallow) copy of its
16861687
module's dictionary is squirreled away.

0 commit comments

Comments
 (0)