return 0;
}
-static inline int
-match_mod_name(PyObject *actual, const char *expected)
+static PyObject *
+get_core_module_dict(PyInterpreterState *interp,
+ PyObject *name, PyObject *filename)
{
- if (PyUnicode_CompareWithASCIIString(actual, expected) == 0) {
- return 1;
+ /* Only builtin modules are core. */
+ if (filename == name) {
+ assert(!PyErr_Occurred());
+ if (PyUnicode_CompareWithASCIIString(name, "sys") == 0) {
+ return interp->sysdict_copy;
+ }
+ assert(!PyErr_Occurred());
+ if (PyUnicode_CompareWithASCIIString(name, "builtins") == 0) {
+ return interp->builtins_copy;
+ }
+ assert(!PyErr_Occurred());
}
- assert(!PyErr_Occurred());
- return 0;
+ return NULL;
+}
+
+static inline int
+is_core_module(PyInterpreterState *interp, PyObject *name, PyObject *filename)
+{
+ return get_core_module_dict(interp, name, filename) != NULL;
}
static int
// bpo-44050: Extensions and def->m_base.m_copy can be updated
// when the extension module doesn't support sub-interpreters.
- // XXX Why special-case the main interpreter?
- if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) {
- /* m_copy of Py_None means it is copied some other way. */
- if (def->m_size == -1 && def->m_base.m_copy != Py_None) {
+ if (def->m_size == -1) {
+ if (!is_core_module(tstate->interp, name, filename)) {
if (def->m_base.m_copy) {
/* Somebody already imported the module,
likely under a different name.
return -1;
}
}
+ }
+ // XXX Why special-case the main interpreter?
+ if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) {
if (_extensions_cache_set(filename, name, def) < 0) {
return -1;
}
PyObject *m_copy = def->m_base.m_copy;
/* Module does not support repeated initialization */
if (m_copy == NULL) {
- return NULL;
- }
- else if (m_copy == Py_None) {
- if (match_mod_name(name, "sys")) {
- m_copy = tstate->interp->sysdict_copy;
- }
- else if (match_mod_name(name, "builtins")) {
- m_copy = tstate->interp->builtins_copy;
- }
- else {
- _PyErr_SetString(tstate, PyExc_ImportError, "missing m_copy");
+ m_copy = get_core_module_dict(tstate->interp, name, filename);
+ if (m_copy == NULL) {
return NULL;
}
}
- /* m_copy of Py_None means it is copied some other way. */
mod = import_add_module(tstate, name);
if (mod == NULL) {
return NULL;