Skip to content

Commit f513d5c

Browse files
gh-102660: Fix is_core_module() (gh-103257)
In gh-102744 we added is_core_module() (in Python/import.c), which relies on get_core_module_dict() (also added in that PR). The problem is that_PyImport_FixupBuiltin(), which ultimately calls is_core_module(), is called on the builtins module before interp->builtins_copyis set. Consequently, the builtins module isn't considered a "core" module while it is getting "fixed up" and its module def m_copy erroneously gets set. Under isolated interpreters this causes problems since sys and builtins are allowed even though they are still single-phase init modules. (This was discovered while working on gh-101660.) The solution is to stop relying on get_core_module_dict() in is_core_module().
1 parent bceb9e0 commit f513d5c

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Python/import.c

+13-1
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,17 @@ get_core_module_dict(PyInterpreterState *interp,
11101110
static inline int
11111111
is_core_module(PyInterpreterState *interp, PyObject *name, PyObject *filename)
11121112
{
1113-
return get_core_module_dict(interp, name, filename) != NULL;
1113+
/* This might be called before the core dict copies are in place,
1114+
so we can't rely on get_core_module_dict() here. */
1115+
if (filename == name) {
1116+
if (PyUnicode_CompareWithASCIIString(name, "sys") == 0) {
1117+
return 1;
1118+
}
1119+
if (PyUnicode_CompareWithASCIIString(name, "builtins") == 0) {
1120+
return 1;
1121+
}
1122+
}
1123+
return 0;
11141124
}
11151125

11161126
static int
@@ -1136,6 +1146,8 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
11361146
// when the extension module doesn't support sub-interpreters.
11371147
if (def->m_size == -1) {
11381148
if (!is_core_module(tstate->interp, name, filename)) {
1149+
assert(PyUnicode_CompareWithASCIIString(name, "sys") != 0);
1150+
assert(PyUnicode_CompareWithASCIIString(name, "builtins") != 0);
11391151
if (def->m_base.m_copy) {
11401152
/* Somebody already imported the module,
11411153
likely under a different name.

0 commit comments

Comments
 (0)