Skip to content

Commit 20c9902

Browse files
bpo-39161: Document multi-phase init modules under Py_NewInterpreter() (GH-17896)
\+ this also adds a stronger warning against sharing objects between (sub-)interpreters. https://bugs.python.org/issue39161 (cherry picked from commit 6c5d661) Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent 45e5750 commit 20c9902

1 file changed

Lines changed: 35 additions & 17 deletions

File tree

Doc/c-api/init.rst

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,15 +1222,31 @@ function. You can create and destroy them using the following functions:
12221222
single: Py_FinalizeEx()
12231223
single: Py_Initialize()
12241224
1225-
Extension modules are shared between (sub-)interpreters as follows: the first
1226-
time a particular extension is imported, it is initialized normally, and a
1227-
(shallow) copy of its module's dictionary is squirreled away. When the same
1228-
extension is imported by another (sub-)interpreter, a new module is initialized
1229-
and filled with the contents of this copy; the extension's ``init`` function is
1230-
not called. Note that this is different from what happens when an extension is
1231-
imported after the interpreter has been completely re-initialized by calling
1232-
:c:func:`Py_FinalizeEx` and :c:func:`Py_Initialize`; in that case, the extension's
1233-
``initmodule`` function *is* called again.
1225+
Extension modules are shared between (sub-)interpreters as follows:
1226+
1227+
* For modules using multi-phase initialization,
1228+
e.g. :c:func:`PyModule_FromDefAndSpec`, a separate module object is
1229+
created and initialized for each interpreter.
1230+
Only C-level static and global variables are shared between these
1231+
module objects.
1232+
1233+
* For modules using single-phase initialization,
1234+
e.g. :c:func:`PyModule_Create`, the first time a particular extension
1235+
is imported, it is initialized normally, and a (shallow) copy of its
1236+
module's dictionary is squirreled away.
1237+
When the same extension is imported by another (sub-)interpreter, a new
1238+
module is initialized and filled with the contents of this copy; the
1239+
extension's ``init`` function is not called.
1240+
Objects in the module's dictionary thus end up shared across
1241+
(sub-)interpreters, which might cause unwanted behavior (see
1242+
`Bugs and caveats`_ below).
1243+
1244+
Note that this is different from what happens when an extension is
1245+
imported after the interpreter has been completely re-initialized by
1246+
calling :c:func:`Py_FinalizeEx` and :c:func:`Py_Initialize`; in that
1247+
case, the extension's ``initmodule`` function *is* called again.
1248+
As with multi-phase initialization, this means that only C-level static
1249+
and global variables are shared between these modules.
12341250
12351251
.. index:: single: close() (in module os)
12361252
@@ -1256,14 +1272,16 @@ process, the insulation between them isn't perfect --- for example, using
12561272
low-level file operations like :func:`os.close` they can
12571273
(accidentally or maliciously) affect each other's open files. Because of the
12581274
way extensions are shared between (sub-)interpreters, some extensions may not
1259-
work properly; this is especially likely when the extension makes use of
1260-
(static) global variables, or when the extension manipulates its module's
1261-
dictionary after its initialization. It is possible to insert objects created
1262-
in one sub-interpreter into a namespace of another sub-interpreter; this should
1263-
be done with great care to avoid sharing user-defined functions, methods,
1264-
instances or classes between sub-interpreters, since import operations executed
1265-
by such objects may affect the wrong (sub-)interpreter's dictionary of loaded
1266-
modules.
1275+
work properly; this is especially likely when using single-phase initialization
1276+
or (static) global variables.
1277+
It is possible to insert objects created in one sub-interpreter into
1278+
a namespace of another (sub-)interpreter; this should be avoided if possible.
1279+
1280+
Special care should be taken to avoid sharing user-defined functions,
1281+
methods, instances or classes between sub-interpreters, since import
1282+
operations executed by such objects may affect the wrong (sub-)interpreter's
1283+
dictionary of loaded modules. It is equally important to avoid sharing
1284+
objects from which the above are reachable.
12671285
12681286
Also note that combining this functionality with :c:func:`PyGILState_\*` APIs
12691287
is delicate, because these APIs assume a bijection between Python thread states

0 commit comments

Comments
 (0)