Skip to content

Commit 9368557

Browse files
ericsnowcurrentlyFidget-Spinner
authored andcommitted
pythongh-102660: Fix Refleaks in import.c (python#102744)
pythongh-102661 introduced some leaks. This fixes them. python#102660
1 parent bc1ff3a commit 9368557

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

Python/bltinmodule.c

-3
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

+28-22
Original file line numberDiff line numberDiff line change
@@ -978,14 +978,29 @@ _PyImport_CheckSubinterpIncompatibleExtensionAllowed(const char *name)
978978
return 0;
979979
}
980980

981-
static inline int
982-
match_mod_name(PyObject *actual, const char *expected)
981+
static PyObject *
982+
get_core_module_dict(PyInterpreterState *interp,
983+
PyObject *name, PyObject *filename)
983984
{
984-
if (PyUnicode_CompareWithASCIIString(actual, expected) == 0) {
985-
return 1;
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());
986996
}
987-
assert(!PyErr_Occurred());
988-
return 0;
997+
return NULL;
998+
}
999+
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;
9891004
}
9901005

9911006
static int
@@ -1009,10 +1024,8 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
10091024

10101025
// bpo-44050: Extensions and def->m_base.m_copy can be updated
10111026
// when the extension module doesn't support sub-interpreters.
1012-
// XXX Why special-case the main interpreter?
1013-
if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) {
1014-
/* m_copy of Py_None means it is copied some other way. */
1015-
if (def->m_size == -1 && def->m_base.m_copy != Py_None) {
1027+
if (def->m_size == -1) {
1028+
if (!is_core_module(tstate->interp, name, filename)) {
10161029
if (def->m_base.m_copy) {
10171030
/* Somebody already imported the module,
10181031
likely under a different name.
@@ -1028,7 +1041,10 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
10281041
return -1;
10291042
}
10301043
}
1044+
}
10311045

1046+
// XXX Why special-case the main interpreter?
1047+
if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) {
10321048
if (_extensions_cache_set(filename, name, def) < 0) {
10331049
return -1;
10341050
}
@@ -1069,21 +1085,11 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
10691085
PyObject *m_copy = def->m_base.m_copy;
10701086
/* Module does not support repeated initialization */
10711087
if (m_copy == NULL) {
1072-
return NULL;
1073-
}
1074-
else if (m_copy == Py_None) {
1075-
if (match_mod_name(name, "sys")) {
1076-
m_copy = tstate->interp->sysdict_copy;
1077-
}
1078-
else if (match_mod_name(name, "builtins")) {
1079-
m_copy = tstate->interp->builtins_copy;
1080-
}
1081-
else {
1082-
_PyErr_SetString(tstate, PyExc_ImportError, "missing m_copy");
1088+
m_copy = get_core_module_dict(tstate->interp, name, filename);
1089+
if (m_copy == NULL) {
10831090
return NULL;
10841091
}
10851092
}
1086-
/* m_copy of Py_None means it is copied some other way. */
10871093
mod = import_add_module(tstate, name);
10881094
if (mod == NULL) {
10891095
return NULL;

Python/sysmodule.c

-3
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)