Skip to content

Commit 3fc9447

Browse files
committed
Rewrite the whole section
1 parent 7f96223 commit 3fc9447

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

Doc/c-api/module.rst

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -288,27 +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 class 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 also 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, multiple modules created from the same definition should
310+
be *isolated*: changes to one should not 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 the 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>`.
306-
Typically, extensions ensure this in one of these ways:
307-
308-
- :ref:`isolating module instances <isolating-extensions-howto>`,
309-
- :ref:`raising an error on repeated initialization <isolating-extensions-optout>`, or
310-
- limiting a module to the main interpreter using
311-
:c:data:`Py_mod_multiple_interpreters`.
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.
312325
313326
To request multi-phase initialization, the initialization function
314327
(PyInit_modulename) returns a :c:type:`PyModuleDef` instance with non-empty

0 commit comments

Comments
 (0)