@@ -137,9 +137,15 @@ which export an initialization function), or compiled-in modules
137137(where the initialization function is added using :c:func: `PyImport_AppendInittab `).
138138See :ref:`building` or :ref:`extending-with-embedding` for details.
139139
140- The initialization function can either pass a module definition instance
141- to :c:func:`PyModule_Create`, and return the resulting module object,
142- or request "multi-phase initialization" by returning the definition struct itself.
140+ To perform ':ref:`multi-phase initialization <multi-phase-initialization>`'
141+ (:pep: `489 `), the initialization function must return a pointer to the
142+ :c:type:`module definition struct <PyModuleDef>`.
143+ This allows Python to determine which capabilities the module supports *before*
144+ it is executed, as creation and initialization are split,
145+ similarly to the :py:meth:`!__new__` and :py:meth:`!__init__` on classes.
146+
147+ The legacy method (prior to Python 3.5) to specify an extension module is
148+ ':ref:`single-phase initialization <single-phase-initialization>`'.
143149
144150.. c:type:: PyModuleDef
145151
@@ -189,7 +195,7 @@ or request "multi-phase initialization" by returning the definition struct itsel
189195
190196 An array of slot definitions for multi-phase initialization, terminated by
191197 a ``{0, NULL} `` entry.
192- When using single-phase initialization, *m_slots * must be ``NULL ``.
198+ When using legacy single-phase initialization, *m_slots * must be ``NULL ``.
193199
194200 .. versionchanged :: 3.5
195201
@@ -249,52 +255,22 @@ or request "multi-phase initialization" by returning the definition struct itsel
249255 .. versionchanged:: 3.9
250256 No longer called before the module state is allocated.
251257
252- Single-phase initialization
253- ...........................
254-
255- The module initialization function may create and return the module object
256- directly. This is referred to as "single-phase initialization", and uses one
257- of the following two module creation functions:
258-
259- .. c:function:: PyObject* PyModule_Create(PyModuleDef *def)
260-
261- Create a new module object, given the definition in *def *. This behaves
262- like :c:func: `PyModule_Create2 ` with *module_api_version * set to
263- :c:macro: `PYTHON_API_VERSION `.
264-
265-
266- .. c :function :: PyObject* PyModule_Create2 (PyModuleDef *def, int module_api_version)
267-
268- Create a new module object, given the definition in *def *, assuming the
269- API version *module_api_version *. If that version does not match the version
270- of the running interpreter, a :exc: `RuntimeWarning ` is emitted.
271-
272- Return ``NULL `` with an exception set on error.
273-
274- .. note ::
275-
276- Most uses of this function should be using :c:func: `PyModule_Create `
277- instead; only use this if you are sure you need it.
278-
279- Before it is returned from in the initialization function, the resulting module
280- object is typically populated using functions like :c:func: `PyModule_AddObjectRef `.
281-
282258.. _multi-phase-initialization:
283259
284260Multi-phase initialization
285261..........................
286262
287- An alternate way to specify extensions is to request "multi-phase initialization".
263+ The preferred method to specify extensions is to request "multi-phase initialization".
288264Extension modules created this way behave more like Python modules: the
289265initialization is split between the *creation phase*, when the module object
290266is created, and the *execution phase*, when it is populated.
291267The distinction is similar to the :py:meth:`!__new__` and :py:meth:`!__init__` methods
292268of classes.
293269
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.
270+ Unlike modules created using the legacy single-phase initialization mechanism,
271+ these modules are not singletons: if the *sys.modules* entry is removed and
272+ the module is re-imported, a new module object is created, and the old module
273+ is subject to normal garbage collection -- as with Python modules.
298274By default, multiple modules created from the same definition should be
299275independent: changes to one should not affect the others.
300276This means that all state should be specific to the module object (using e.g.
@@ -448,6 +424,44 @@ The available slot types are:
448424
449425See :PEP:`489` for more details on multi-phase initialization.
450426
427+ .. _single-phase-initialization:
428+
429+ Single-phase initialization
430+ ...........................
431+
432+ .. attention::
433+ Single-phase initialization is a legacy mechanism to initialize extension
434+ modules, with known drawbacks and design flaws. Extension module authors
435+ are encouraged to use multi-phase initialization instead.
436+
437+ The module initialization function may create and return the module object
438+ directly. This is referred to as "single-phase initialization", and uses one
439+ of the following two module creation functions, returning the resulting
440+ module object:
441+
442+ .. c:function:: PyObject* PyModule_Create(PyModuleDef *def)
443+
444+ Create a new module object, given the definition in *def *. This behaves
445+ like :c:func: `PyModule_Create2 ` with *module_api_version * set to
446+ :c:macro: `PYTHON_API_VERSION `.
447+
448+
449+ .. c :function :: PyObject* PyModule_Create2 (PyModuleDef *def, int module_api_version)
450+
451+ Create a new module object, given the definition in *def *, assuming the
452+ API version *module_api_version *. If that version does not match the version
453+ of the running interpreter, a :exc: `RuntimeWarning ` is emitted.
454+
455+ Return ``NULL `` with an exception set on error.
456+
457+ .. note ::
458+
459+ Most uses of this function should be using :c:func: `PyModule_Create `
460+ instead; only use this if you are sure you need it.
461+
462+ Before it is returned from in the initialization function, the resulting module
463+ object is typically populated using functions like :c:func: `PyModule_AddObjectRef `.
464+
451465Low-level module creation functions
452466...................................
453467
@@ -677,8 +691,8 @@ state:
677691 .. versionadded:: 3.13
678692
679693
680- Module lookup
681- ^^^^^^^^^^^^^
694+ Module lookup (single-phase initialization)
695+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
682696
683697Single-phase initialization creates singleton modules that can be looked up
684698in the context of the current interpreter. This allows the module object to be
0 commit comments