Skip to content

Commit aef1d8d

Browse files
committed
De-emphasise single-phase init
1 parent 96905bd commit aef1d8d

2 files changed

Lines changed: 62 additions & 44 deletions

File tree

Doc/c-api/module.rst

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,15 @@ which export an initialization function), or compiled-in modules
137137
(where the initialization function is added using :c:func:`PyImport_AppendInittab`).
138138
See :ref:`building` or :ref:`extending-with-embedding` for details.
139139
140-
The initialization function can either pass a module definition instance
141-
to :c:func:`PyModule_Create`, and return the resulting module object,
142-
or request "multi-phase initialization" by returning the definition struct itself.
140+
To perform ':ref:`multi-phase initialization <multi-phase-initialization>`'
141+
(:pep:`489`), the initialization function must return a pointer to the
142+
:c:type:`module definition struct <PyModuleDef>`.
143+
This allows Python to determine which capabilities the module supports *before*
144+
it is executed, as creation and initialization are split,
145+
similarly to the :py:meth:`!__new__` and :py:meth:`!__init__` on classes.
146+
147+
The legacy method (prior to Python 3.5) to specify an extension module is
148+
':ref:`single-phase initialization <single-phase-initialization>`'.
143149
144150
.. c:type:: PyModuleDef
145151
@@ -189,7 +195,7 @@ or request "multi-phase initialization" by returning the definition struct itsel
189195
190196
An array of slot definitions for multi-phase initialization, terminated by
191197
a ``{0, NULL}`` entry.
192-
When using single-phase initialization, *m_slots* must be ``NULL``.
198+
When using legacy single-phase initialization, *m_slots* must be ``NULL``.
193199
194200
.. versionchanged:: 3.5
195201
@@ -249,52 +255,22 @@ or request "multi-phase initialization" by returning the definition struct itsel
249255
.. versionchanged:: 3.9
250256
No longer called before the module state is allocated.
251257
252-
Single-phase initialization
253-
...........................
254-
255-
The module initialization function may create and return the module object
256-
directly. This is referred to as "single-phase initialization", and uses one
257-
of the following two module creation functions:
258-
259-
.. c:function:: PyObject* PyModule_Create(PyModuleDef *def)
260-
261-
Create a new module object, given the definition in *def*. This behaves
262-
like :c:func:`PyModule_Create2` with *module_api_version* set to
263-
:c:macro:`PYTHON_API_VERSION`.
264-
265-
266-
.. c:function:: PyObject* PyModule_Create2(PyModuleDef *def, int module_api_version)
267-
268-
Create a new module object, given the definition in *def*, assuming the
269-
API version *module_api_version*. If that version does not match the version
270-
of the running interpreter, a :exc:`RuntimeWarning` is emitted.
271-
272-
Return ``NULL`` with an exception set on error.
273-
274-
.. note::
275-
276-
Most uses of this function should be using :c:func:`PyModule_Create`
277-
instead; only use this if you are sure you need it.
278-
279-
Before it is returned from in the initialization function, the resulting module
280-
object is typically populated using functions like :c:func:`PyModule_AddObjectRef`.
281-
282258
.. _multi-phase-initialization:
283259
284260
Multi-phase initialization
285261
..........................
286262
287-
An alternate way to specify extensions is to request "multi-phase initialization".
263+
The preferred method to specify extensions is to request "multi-phase initialization".
288264
Extension modules created this way behave more like Python modules: the
289265
initialization is split between the *creation phase*, when the module object
290266
is created, and the *execution phase*, when it is populated.
291267
The distinction is similar to the :py:meth:`!__new__` and :py:meth:`!__init__` methods
292268
of classes.
293269
294-
Unlike modules created using single-phase initialization, these modules are not
295-
singletons: if the *sys.modules* entry is removed and the module is re-imported,
296-
a new module object is created, and the old module is subject to normal garbage
297-
collection -- as with Python modules.
270+
Unlike modules created using the legacy single-phase initialization mechanism,
271+
these modules are not singletons: if the *sys.modules* entry is removed and
272+
the module is re-imported, a new module object is created, and the old module
273+
is subject to normal garbage collection -- as with Python modules.
298274
By default, multiple modules created from the same definition should be
299275
independent: changes to one should not affect the others.
300276
This means that all state should be specific to the module object (using e.g.
@@ -448,6 +424,44 @@ The available slot types are:
448424
449425
See :PEP:`489` for more details on multi-phase initialization.
450426
427+
.. _single-phase-initialization:
428+
429+
Single-phase initialization
430+
...........................
431+
432+
.. attention::
433+
Single-phase initialization is a legacy mechanism to initialize extension
434+
modules, with known drawbacks and design flaws. Extension module authors
435+
are encouraged to use multi-phase initialization instead.
436+
437+
The module initialization function may create and return the module object
438+
directly. This is referred to as "single-phase initialization", and uses one
439+
of the following two module creation functions, returning the resulting
440+
module object:
441+
442+
.. c:function:: PyObject* PyModule_Create(PyModuleDef *def)
443+
444+
Create a new module object, given the definition in *def*. This behaves
445+
like :c:func:`PyModule_Create2` with *module_api_version* set to
446+
:c:macro:`PYTHON_API_VERSION`.
447+
448+
449+
.. c:function:: PyObject* PyModule_Create2(PyModuleDef *def, int module_api_version)
450+
451+
Create a new module object, given the definition in *def*, assuming the
452+
API version *module_api_version*. If that version does not match the version
453+
of the running interpreter, a :exc:`RuntimeWarning` is emitted.
454+
455+
Return ``NULL`` with an exception set on error.
456+
457+
.. note::
458+
459+
Most uses of this function should be using :c:func:`PyModule_Create`
460+
instead; only use this if you are sure you need it.
461+
462+
Before it is returned from in the initialization function, the resulting module
463+
object is typically populated using functions like :c:func:`PyModule_AddObjectRef`.
464+
451465
Low-level module creation functions
452466
...................................
453467
@@ -677,8 +691,8 @@ state:
677691
.. versionadded:: 3.13
678692
679693
680-
Module lookup
681-
^^^^^^^^^^^^^
694+
Module lookup (single-phase initialization)
695+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
682696
683697
Single-phase initialization creates singleton modules that can be looked up
684698
in the context of the current interpreter. This allows the module object to be

Doc/howto/free-threading-extensions.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Extensions that use multi-phase initialization (i.e.,
5050
module definition. If your extension supports older versions of CPython,
5151
you should guard the slot with a :c:data:`PY_VERSION_HEX` check.
5252

53-
::
53+
.. code-block:: c
5454
5555
static struct PyModuleDef_Slot module_slots[] = {
5656
...
@@ -70,13 +70,17 @@ you should guard the slot with a :c:data:`PY_VERSION_HEX` check.
7070
Single-Phase Initialization
7171
...........................
7272

73-
Extensions that use single-phase initialization (i.e.,
73+
Extensions that use legacy single-phase initialization (i.e.,
7474
:c:func:`PyModule_Create`) should call :c:func:`PyUnstable_Module_SetGIL` to
7575
indicate that they support running with the GIL disabled. The function is
7676
only defined in the free-threaded build, so you should guard the call with
7777
``#ifdef Py_GIL_DISABLED`` to avoid compilation errors in the regular build.
7878

79-
::
79+
Note that this function is part of the :pep:`unstable C API <689>`, meaning
80+
that it may change without warning between minor releases. Where possible,
81+
prefer using multi-phase initialization with the ``Py_mod_gil`` slot.
82+
83+
.. code-block:: c
8084
8185
static struct PyModuleDef moduledef = {
8286
PyModuleDef_HEAD_INIT,

0 commit comments

Comments
 (0)