Visitar URL original
gh-102660: Fix Refleaks in import.c by ericsnowcurrently · Pull Request #102744 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Stop using None as the m_copy sentinel.
  • Loading branch information
ericsnowcurrently committed Mar 15, 2023
commit 16348ebbee3c27aa06c4943498cf5fa7c8cb0732
3 changes: 0 additions & 3 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3098,9 +3098,6 @@ _PyBuiltin_Init(PyInterpreterState *interp)
}
Py_DECREF(debug);

/* m_copy of Py_None means it is copied some other way. */
builtinsmodule.m_base.m_copy = Py_NewRef(Py_None);

return mod;
#undef ADD_TO_ALL
#undef SETBUILTIN
Expand Down
39 changes: 21 additions & 18 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -982,21 +982,27 @@ static PyObject *
get_core_module_dict(PyInterpreterState *interp,
PyObject *name, PyObject *filename)
{
if (filename != name && filename != NULL) {
/* It isn't a builtin module, which means it isn't core. */
return 0;
}
if (PyUnicode_CompareWithASCIIString(name, "sys") == 0) {
return interp->sysdict_copy;
}
assert(!PyErr_Occurred());
if (PyUnicode_CompareWithASCIIString(name, "builtins") == 0) {
return interp->builtins_copy;
/* 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;
}

static inline int
is_core_module(PyInterpreterState *interp, PyObject *name, PyObject *filename)
{
return get_core_module_dict(interp, name, filename) == NULL;
}

static int
fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
{
Expand All @@ -1019,9 +1025,8 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
// 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.
Expand All @@ -1037,7 +1042,9 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
return -1;
}
}
}

if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) {
if (_extensions_cache_set(filename, name, def) < 0) {
return -1;
}
Expand Down Expand Up @@ -1078,12 +1085,8 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
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) {
m_copy = get_core_module_dict(tstate->interp, name, filename);
if (m_copy == NULL) {
_PyErr_SetString(tstate, PyExc_ImportError, "missing m_copy");
return NULL;
}
}
Expand Down
3 changes: 0 additions & 3 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3425,9 +3425,6 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p)
return _PyStatus_ERR("failed to create a module object");
}

/* m_copy of Py_None means it is copied some other way. */
sysmodule.m_base.m_copy = Py_NewRef(Py_None);

PyObject *sysdict = PyModule_GetDict(sysmod);
if (sysdict == NULL) {
goto error;
Expand Down