Skip to content

Commit 16348eb

Browse files
Stop using None as the m_copy sentinel.
1 parent 0512932 commit 16348eb

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

Python/bltinmodule.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3098,9 +3098,6 @@ _PyBuiltin_Init(PyInterpreterState *interp)
30983098
}
30993099
Py_DECREF(debug);
31003100

3101-
/* m_copy of Py_None means it is copied some other way. */
3102-
builtinsmodule.m_base.m_copy = Py_NewRef(Py_None);
3103-
31043101
return mod;
31053102
#undef ADD_TO_ALL
31063103
#undef SETBUILTIN

Python/import.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -982,21 +982,27 @@ static PyObject *
982982
get_core_module_dict(PyInterpreterState *interp,
983983
PyObject *name, PyObject *filename)
984984
{
985-
if (filename != name && filename != NULL) {
986-
/* It isn't a builtin module, which means it isn't core. */
987-
return 0;
988-
}
989-
if (PyUnicode_CompareWithASCIIString(name, "sys") == 0) {
990-
return interp->sysdict_copy;
991-
}
992-
assert(!PyErr_Occurred());
993-
if (PyUnicode_CompareWithASCIIString(name, "builtins") == 0) {
994-
return interp->builtins_copy;
985+
/* Only builtin modules are core. */
986+
if (filename == name) {
987+
assert(!PyErr_Occurred());
988+
if (PyUnicode_CompareWithASCIIString(name, "sys") == 0) {
989+
return interp->sysdict_copy;
990+
}
991+
assert(!PyErr_Occurred());
992+
if (PyUnicode_CompareWithASCIIString(name, "builtins") == 0) {
993+
return interp->builtins_copy;
994+
}
995+
assert(!PyErr_Occurred());
995996
}
996-
assert(!PyErr_Occurred());
997997
return 0;
998998
}
999999

1000+
static inline int
1001+
is_core_module(PyInterpreterState *interp, PyObject *name, PyObject *filename)
1002+
{
1003+
return get_core_module_dict(interp, name, filename) == NULL;
1004+
}
1005+
10001006
static int
10011007
fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
10021008
{
@@ -1019,9 +1025,8 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
10191025
// bpo-44050: Extensions and def->m_base.m_copy can be updated
10201026
// when the extension module doesn't support sub-interpreters.
10211027
// XXX Why special-case the main interpreter?
1022-
if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) {
1023-
/* m_copy of Py_None means it is copied some other way. */
1024-
if (def->m_size == -1 && def->m_base.m_copy != Py_None) {
1028+
if (def->m_size == -1) {
1029+
if (!is_core_module(tstate->interp, name, filename)) {
10251030
if (def->m_base.m_copy) {
10261031
/* Somebody already imported the module,
10271032
likely under a different name.
@@ -1037,7 +1042,9 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
10371042
return -1;
10381043
}
10391044
}
1045+
}
10401046

1047+
if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) {
10411048
if (_extensions_cache_set(filename, name, def) < 0) {
10421049
return -1;
10431050
}
@@ -1078,12 +1085,8 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
10781085
PyObject *m_copy = def->m_base.m_copy;
10791086
/* Module does not support repeated initialization */
10801087
if (m_copy == NULL) {
1081-
return NULL;
1082-
}
1083-
else if (m_copy == Py_None) {
10841088
m_copy = get_core_module_dict(tstate->interp, name, filename);
10851089
if (m_copy == NULL) {
1086-
_PyErr_SetString(tstate, PyExc_ImportError, "missing m_copy");
10871090
return NULL;
10881091
}
10891092
}

Python/sysmodule.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3425,9 +3425,6 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p)
34253425
return _PyStatus_ERR("failed to create a module object");
34263426
}
34273427

3428-
/* m_copy of Py_None means it is copied some other way. */
3429-
sysmodule.m_base.m_copy = Py_NewRef(Py_None);
3430-
34313428
PyObject *sysdict = PyModule_GetDict(sysmod);
34323429
if (sysdict == NULL) {
34333430
goto error;

0 commit comments

Comments
 (0)