Skip to content

Commit 1663bfc

Browse files
Add _PyRuntimeState.imports.last_module_index.
1 parent b3e3f45 commit 1663bfc

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

Include/internal/pycore_import.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ extern "C" {
77

88

99
struct _import_runtime_state {
10+
/* The most recent value assigned to a PyModuleDef.m_base.m_index.
11+
This is incremented each time PyModuleDef_Init() is called,
12+
which is just about every time an extension module is imported.
13+
See PyInterpreterState.modules_by_index for more info. */
14+
Py_ssize_t last_module_index;
1015
/* A dict mapping (filename, name) to PyModuleDef for modules.
1116
Only legacy (single-phase init) extension modules are added
1217
and only if they support multiple initialization (m_size >- 0)

Objects/moduleobject.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "pycore_moduleobject.h" // _PyModule_GetDef()
1010
#include "structmember.h" // PyMemberDef
1111

12-
static Py_ssize_t max_module_number;
1312

1413
static PyMemberDef module_members[] = {
1514
{"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
@@ -43,10 +42,10 @@ PyModuleDef_Init(PyModuleDef* def)
4342
{
4443
assert(PyModuleDef_Type.tp_flags & Py_TPFLAGS_READY);
4544
if (def->m_base.m_index == 0) {
46-
max_module_number++;
45+
_PyRuntime.imports.last_module_index++;
4746
Py_SET_REFCNT(def, 1);
4847
Py_SET_TYPE(def, &PyModuleDef_Type);
49-
def->m_base.m_index = max_module_number;
48+
def->m_base.m_index = _PyRuntime.imports.last_module_index;
5049
}
5150
return (PyObject*)def;
5251
}

Tools/c-analyzer/cpython/globals-to-fix.tsv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,6 @@ Python/getargs.c - static_arg_parsers -
448448
Objects/dictobject.c - _pydict_global_version -
449449
Objects/dictobject.c - next_dict_keys_version -
450450
Objects/funcobject.c - next_func_version -
451-
Objects/moduleobject.c - max_module_number -
452451
Objects/object.c - _Py_RefTotal -
453452
Python/perf_trampoline.c - perf_status -
454453
Python/perf_trampoline.c - extra_code_index -

0 commit comments

Comments
 (0)