Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6c5d661

Browse files
encukoumiss-islington
authored andcommitted
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
1 parent f3e5e95 commit 6c5d661

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
@@ -1230,15 +1230,31 @@ function. You can create and destroy them using the following functions:
12301230
single: Py_FinalizeEx()
12311231
single: Py_Initialize()
12321232
1233-
Extension modules are shared between (sub-)interpreters as follows: the first
1234-
time a particular extension is imported, it is initialized normally, and a
1235-
(shallow) copy of its module's dictionary is squirreled away. When the same
1236-
extension is imported by another (sub-)interpreter, a new module is initialized
1237-
and filled with the contents of this copy; the extension's ``init`` function is
1238-
not called. Note that this is different from what happens when an extension is
1239-
imported after the interpreter has been completely re-initialized by calling
1240-
:c:func:`Py_FinalizeEx` and :c:func:`Py_Initialize`; in that case, the extension's
1241-
``initmodule`` function *is* called again.
1233+
Extension modules are shared between (sub-)interpreters as follows:
1234+
1235+
* For modules using multi-phase initialization,
1236+
e.g. :c:func:`PyModule_FromDefAndSpec`, a separate module object is
1237+
created and initialized for each interpreter.
1238+
Only C-level static and global variables are shared between these
1239+
module objects.
1240+
1241+
* For modules using single-phase initialization,
1242+
e.g. :c:func:`PyModule_Create`, the first time a particular extension
1243+
is imported, it is initialized normally, and a (shallow) copy of its
1244+
module's dictionary is squirreled away.
1245+
When the same extension is imported by another (sub-)interpreter, a new
1246+
module is initialized and filled with the contents of this copy; the
1247+
extension's ``init`` function is not called.
1248+
Objects in the module's dictionary thus end up shared across
1249+
(sub-)interpreters, which might cause unwanted behavior (see
1250+
`Bugs and caveats`_ below).
1251+
1252+
Note that this is different from what happens when an extension is
1253+
imported after the interpreter has been completely re-initialized by
1254+
calling :c:func:`Py_FinalizeEx` and :c:func:`Py_Initialize`; in that
1255+
case, the extension's ``initmodule`` function *is* called again.
1256+
As with multi-phase initialization, this means that only C-level static
1257+
and global variables are shared between these modules.
12421258
12431259
.. index:: single: close() (in module os)
12441260
@@ -1264,14 +1280,16 @@ process, the insulation between them isn't perfect --- for example, using
12641280
low-level file operations like :func:`os.close` they can
12651281
(accidentally or maliciously) affect each other's open files. Because of the
12661282
way extensions are shared between (sub-)interpreters, some extensions may not
1267-
work properly; this is especially likely when the extension makes use of
1268-
(static) global variables, or when the extension manipulates its module's
1269-
dictionary after its initialization. It is possible to insert objects created
1270-
in one sub-interpreter into a namespace of another sub-interpreter; this should
1271-
be done with great care to avoid sharing user-defined functions, methods,
1272-
instances or classes between sub-interpreters, since import operations executed
1273-
by such objects may affect the wrong (sub-)interpreter's dictionary of loaded
1274-
modules.
1283+
work properly; this is especially likely when using single-phase initialization
1284+
or (static) global variables.
1285+
It is possible to insert objects created in one sub-interpreter into
1286+
a namespace of another (sub-)interpreter; this should be avoided if possible.
1287+
1288+
Special care should be taken to avoid sharing user-defined functions,
1289+
methods, instances or classes between sub-interpreters, since import
1290+
operations executed by such objects may affect the wrong (sub-)interpreter's
1291+
dictionary of loaded modules. It is equally important to avoid sharing
1292+
objects from which the above are reachable.
12751293
12761294
Also note that combining this functionality with :c:func:`PyGILState_\*` APIs
12771295
is delicate, because these APIs assume a bijection between Python thread states

0 commit comments

Comments
 (0)