summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2023-03-16 00:43:54 (GMT)
committerGitHub <noreply@github.com>2023-03-16 00:43:54 (GMT)
commit2a03ed034edc6bb2db6fb972b2efe08db5145a28 (patch)
treeb6d8ea3de51cf72a15a12c8f052d20ac31fdb4c5 /Python
parent675b97a6ab483573f07ce6e0b79b0bc370ab76c9 (diff)
downloadcpython-2a03ed034edc6bb2db6fb972b2efe08db5145a28.zip
cpython-2a03ed034edc6bb2db6fb972b2efe08db5145a28.tar.gz
cpython-2a03ed034edc6bb2db6fb972b2efe08db5145a28.tar.bz2
gh-102660: Fix Refleaks in import.c (#102744)
gh-102661 introduced some leaks. This fixes them. https://github.com/python/cpython/issues/102660
Diffstat (limited to 'Python')
-rw-r--r--Python/bltinmodule.c3
-rw-r--r--Python/import.c50
-rw-r--r--Python/sysmodule.c3
3 files changed, 28 insertions, 28 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index a8a3462..12ca0ba 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -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
diff --git a/Python/import.c b/Python/import.c
index 612fee2..9f80c6d 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -978,14 +978,29 @@ _PyImport_CheckSubinterpIncompatibleExtensionAllowed(const char *name)
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
@@ -1009,10 +1024,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.
@@ -1028,7 +1041,10 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
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;
}
@@ -1069,21 +1085,11 @@ 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) {
- 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;
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index fc05502..d282104 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -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;