Skip to content

Commit eb145fa

Browse files
encukouAA-Turner
andauthored
gh-134160: Improve multi-phase init note on isolation & subinterpreters (GH-134775)
Co-authored-by: Adam Turner <[email protected]>
1 parent cc344e8 commit eb145fa

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

Doc/c-api/module.rst

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -288,22 +288,40 @@ An alternate way to specify extensions is to request "multi-phase initialization
288288
Extension modules created this way behave more like Python modules: the
289289
initialization is split between the *creation phase*, when the module object
290290
is created, and the *execution phase*, when it is populated.
291-
The distinction is similar to the :py:meth:`!__new__` and :py:meth:`!__init__` methods
292-
of classes.
291+
The distinction is similar to the :py:meth:`~object.__new__` and
292+
:py:meth:`~object.__init__` methods of classes.
293293
294294
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.
298-
By default, multiple modules created from the same definition should be
299-
independent: changes to one should not affect the others.
300-
This means that all state should be specific to the module object (using e.g.
301-
using :c:func:`PyModule_GetState`), or its contents (such as the module's
302-
:attr:`~object.__dict__` or individual classes created with :c:func:`PyType_FromSpec`).
295+
singletons.
296+
For example, if the :py:attr:`sys.modules` entry is removed and the module
297+
is re-imported, a new module object is created, and typically populated with
298+
fresh method and type objects.
299+
The old module is subject to normal garbage collection.
300+
This mirrors the behavior of pure-Python modules.
301+
302+
Additional module instances may be created in
303+
:ref:`sub-interpreters <sub-interpreter-support>`
304+
or after after Python runtime reinitialization
305+
(:c:func:`Py_Finalize` and :c:func:`Py_Initialize`).
306+
In these cases, sharing Python objects between module instances would likely
307+
cause crashes or undefined behavior.
308+
309+
To avoid such issues, each instance of an extension module should
310+
be *isolated*: changes to one instance should not implicitly affect the others,
311+
and all state, including references to Python objects, should be specific to
312+
a particular module instance.
313+
See :ref:`isolating-extensions-howto` for more details and a practical guide.
314+
315+
A simpler way to avoid these issues is
316+
:ref:`raising an error on repeated initialization <isolating-extensions-optout>`.
303317
304318
All modules created using multi-phase initialization are expected to support
305-
:ref:`sub-interpreters <sub-interpreter-support>`. Making sure multiple modules
306-
are independent is typically enough to achieve this.
319+
:ref:`sub-interpreters <sub-interpreter-support>`, or otherwise explicitly
320+
signal a lack of support.
321+
This is usually achieved by isolation or blocking repeated initialization,
322+
as above.
323+
A module may also be limited to the main interpreter using
324+
the :c:data:`Py_mod_multiple_interpreters` slot.
307325
308326
To request multi-phase initialization, the initialization function
309327
(PyInit_modulename) returns a :c:type:`PyModuleDef` instance with non-empty

Doc/howto/isolating-extensions.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ possible, consider explicit locking.
168168
If it is necessary to use process-global state, the simplest way to
169169
avoid issues with multiple interpreters is to explicitly prevent a
170170
module from being loaded more than once per process—see
171-
`Opt-Out: Limiting to One Module Object per Process`_.
171+
:ref:`isolating-extensions-optout`.
172172

173173

174174
Managing Per-Module State
@@ -207,6 +207,8 @@ An example of a module with per-module state is currently available as
207207
example module initialization shown at the bottom of the file.
208208

209209

210+
.. _isolating-extensions-optout:
211+
210212
Opt-Out: Limiting to One Module Object per Process
211213
--------------------------------------------------
212214

0 commit comments

Comments
 (0)