Skip to content

Commit 28140d1

Browse files
aiskkumaraditya303
andauthored
gh-115649: Copy the filename into main interpreter before intern in import.c (#120315)
Co-authored-by: Kumar Aditya <[email protected]>
1 parent 95737bb commit 28140d1

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Lib/test/test_import/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,6 +2138,8 @@ def test_single_init_extension_compat(self):
21382138
self.check_incompatible_here(module)
21392139
with self.subTest(f'{module}: strict, fresh'):
21402140
self.check_incompatible_fresh(module)
2141+
with self.subTest(f'{module}: isolated, fresh'):
2142+
self.check_incompatible_fresh(module, isolated=True)
21412143

21422144
@unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
21432145
def test_multi_init_extension_compat(self):

Python/import.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1969,7 +1969,17 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
19691969
if (info->filename != NULL) {
19701970
// XXX There's a refleak somewhere with the filename.
19711971
// Until we can track it down, we intern it.
1972-
PyObject *filename = Py_NewRef(info->filename);
1972+
PyObject *filename = NULL;
1973+
if (switched) {
1974+
// The original filename may be allocated by subinterpreter's
1975+
// obmalloc, so we create a copy here.
1976+
filename = _PyUnicode_Copy(info->filename);
1977+
if (filename == NULL) {
1978+
return NULL;
1979+
}
1980+
} else {
1981+
filename = Py_NewRef(info->filename);
1982+
}
19731983
PyUnicode_InternInPlace(&filename);
19741984
if (PyModule_AddObjectRef(mod, "__file__", filename) < 0) {
19751985
PyErr_Clear(); /* Not important enough to report */

0 commit comments

Comments
 (0)