@@ -313,16 +313,32 @@ This is typically done using :c:func:`PyModule_Create` and functions like
313313Single-phase initialization differs from the :ref:`default <multi-phase-initialization>`
314314in the following ways:
315315
316- * Single-phase modules are, or rather *contain*, “singletons”.
316+ * Single-phase modules with :c:member:`PyModuleDef.m_size` set to -1 are,
317+ or rather *contain*, “singletons”.
317318
318- When the module is first initialized, Python saves the contents of
319+ When such a module is first initialized, Python saves the contents of
319320 the module's ``__dict__`` (that is, typically, the module's functions and
320321 types).
321322
322323 For subsequent imports, Python does not call the initialization function
323324 again.
324325 Instead, it creates a new module object with a new ``__dict__``, and copies
325326 the saved contents to it.
327+ Modules are matched to the saved contents by a (*module_name *, *file_name *)
328+ tuple where:
329+
330+ - *module_name* is the module's full dotted name, taken from
331+ :py:attr:`ModuleSpec.name <importlib.machinery.ModuleSpec.name>`
332+ (or the equivalent argument of the importing function).
333+ Note that names that the initialization function uses to create the module
334+ (for example, :c:member: `PyModuleDef.m_name `) are not taken into account.
335+ - *file_name* is the extension's file name.
336+ It is unset for modules not loaded from a file, such as built-ins and
337+ modules added with :c:func:`PyImport_CreateModuleFromInitfunc` or
338+ :c:func:`PyImport_AppendInittab`.
339+
340+ As part of the first initialization, Python also adds the module to
341+ :data:`sys.modules` under its name.
326342 For example, given a single-phase module ``_testsinglephase``
327343 [#testsinglephase]_ that defines a function ``sum`` and an exception class
328344 ``error ``:
@@ -344,6 +360,16 @@ in the following ways:
344360
345361 The exact behavior should be considered a CPython implementation detail.
346362
363+ * For single-phase modules with non-negative :c:member: `PyModuleDef.m_size `,
364+ Python may cache a pointer to the initialization function under the
365+ (*module_name *, *file_name *) key as for ``m_size == -1 ``.
366+
367+ Subsequent imports that specify a different initialization function
368+ (for example, using :c:func: `PyImport_CreateModuleFromInitfunc `)
369+ may call either the cached function or the provided "new" one.
370+
371+ The exact behavior should again be considered a CPython implementation detail.
372+
347373* To work around the fact that ``PyInit_modulename `` does not take a *spec *
348374 argument, some state of the import machinery is saved and applied to the
349375 first suitable module created during the ``PyInit_modulename `` call.
0 commit comments