@@ -288,22 +288,40 @@ An alternate way to specify extensions is to request "multi-phase initialization
288
288
Extension modules created this way behave more like Python modules: the
289
289
initialization is split between the *creation phase *, when the module object
290
290
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.
293
293
294
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.
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>`.
303
317
304
318
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.
307
325
308
326
To request multi-phase initialization, the initialization function
309
327
(PyInit_modulename) returns a :c:type:`PyModuleDef` instance with non-empty
0 commit comments