summaryrefslogtreecommitdiff
path: root/Include
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2022-11-11 14:16:28 -0700
committerGitHub <noreply@github.com>2022-11-11 14:16:28 -0700
commitdd36b71fa6164ebba5d94bb4a24eac43b1c54906 (patch)
treeb80a158cde971908ead6d89f7ed75c82b736992f /Include
parentfe55ff3f68d56e11526652a21d8fe27444f96224 (diff)
downloadcpython-git-dd36b71fa6164ebba5d94bb4a24eac43b1c54906.tar.gz
gh-81057: Move the Extension Modules Cache to _PyRuntimeState (gh-99355)
We also move the closely related max_module_number and add comments documenting the group of struct members. https://github.com/python/cpython/issues/81057
Diffstat (limited to 'Include')
-rw-r--r--Include/internal/pycore_import.h17
-rw-r--r--Include/internal/pycore_interp.h19
-rw-r--r--Include/internal/pycore_runtime.h2
-rw-r--r--Include/moduleobject.h14
4 files changed, 52 insertions, 0 deletions
diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h
index aee1f66a3e..7f1240f7c9 100644
--- a/Include/internal/pycore_import.h
+++ b/Include/internal/pycore_import.h
@@ -5,6 +5,23 @@
extern "C" {
#endif
+
+struct _import_runtime_state {
+ /* The most recent value assigned to a PyModuleDef.m_base.m_index.
+ This is incremented each time PyModuleDef_Init() is called,
+ which is just about every time an extension module is imported.
+ See PyInterpreterState.modules_by_index for more info. */
+ Py_ssize_t last_module_index;
+ /* A dict mapping (filename, name) to PyModuleDef for modules.
+ Only legacy (single-phase init) extension modules are added
+ and only if they support multiple initialization (m_size >- 0)
+ or are imported in the main interpreter.
+ This is initialized lazily in _PyImport_FixupExtensionObject().
+ Modules are added there and looked up in _imp.find_extension(). */
+ PyObject *extensions;
+};
+
+
#ifdef HAVE_FORK
extern PyStatus _PyImport_ReInitLock(void);
#endif
diff --git a/Include/internal/pycore_interp.h b/Include/internal/pycore_interp.h
index ae2a3d3b13..068b0a700a 100644
--- a/Include/internal/pycore_interp.h
+++ b/Include/internal/pycore_interp.h
@@ -123,6 +123,25 @@ struct _is {
// sys.modules dictionary
PyObject *modules;
+ /* This is the list of module objects for all legacy (single-phase init)
+ extension modules ever loaded in this process (i.e. imported
+ in this interpreter or in any other). Py_None stands in for
+ modules that haven't actually been imported in this interpreter.
+
+ A module's index (PyModuleDef.m_base.m_index) is used to look up
+ the corresponding module object for this interpreter, if any.
+ (See PyState_FindModule().) When any extension module
+ is initialized during import, its moduledef gets initialized by
+ PyModuleDef_Init(), and the first time that happens for each
+ PyModuleDef, its index gets set to the current value of
+ a global counter (see _PyRuntimeState.imports.last_module_index).
+ The entry for that index in this interpreter remains unset until
+ the module is actually imported here. (Py_None is used as
+ a placeholder.) Note that multi-phase init modules always get
+ an index for which there will never be a module set.
+
+ This is initialized lazily in _PyState_AddModule(), which is also
+ where modules get added. */
PyObject *modules_by_index;
// Dictionary of the sys module
PyObject *sysdict;
diff --git a/Include/internal/pycore_runtime.h b/Include/internal/pycore_runtime.h
index d1fbc09f1e..df35e34291 100644
--- a/Include/internal/pycore_runtime.h
+++ b/Include/internal/pycore_runtime.h
@@ -11,6 +11,7 @@ extern "C" {
#include "pycore_atomic.h" /* _Py_atomic_address */
#include "pycore_gil.h" // struct _gil_runtime_state
#include "pycore_global_objects.h" // struct _Py_global_objects
+#include "pycore_import.h" // struct _import_runtime_state
#include "pycore_interp.h" // PyInterpreterState
#include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_ids
@@ -115,6 +116,7 @@ typedef struct pyruntimestate {
void (*exitfuncs[NEXITFUNCS])(void);
int nexitfuncs;
+ struct _import_runtime_state imports;
struct _ceval_runtime_state ceval;
struct _gilstate_runtime_state gilstate;
struct _getargs_runtime_state getargs;
diff --git a/Include/moduleobject.h b/Include/moduleobject.h
index fbb2c5ae79..555564ec73 100644
--- a/Include/moduleobject.h
+++ b/Include/moduleobject.h
@@ -43,8 +43,22 @@ PyAPI_DATA(PyTypeObject) PyModuleDef_Type;
typedef struct PyModuleDef_Base {
PyObject_HEAD
+ /* The function used to re-initialize the module.
+ This is only set for legacy (single-phase init) extension modules
+ and only used for those that support multiple initializations
+ (m_size >= 0).
+ It is set by _PyImport_LoadDynamicModuleWithSpec()
+ and _imp.create_builtin(). */
PyObject* (*m_init)(void);
+ /* The module's index into its interpreter's modules_by_index cache.
+ This is set for all extension modules but only used for legacy ones.
+ (See PyInterpreterState.modules_by_index for more info.)
+ It is set by PyModuleDef_Init(). */
Py_ssize_t m_index;
+ /* A copy of the module's __dict__ after the first time it was loaded.
+ This is only set/used for legacy modules that do not support
+ multiple initializations.
+ It is set by _PyImport_FixupExtensionObject(). */
PyObject* m_copy;
} PyModuleDef_Base;