Skip to content

Commit 7a0051f

Browse files
aiskkumaraditya303
authored andcommitted
pythongh-115649: Copy the filename into main interpreter before intern in import.c (python#120315)
Co-authored-by: Kumar Aditya <[email protected]>
1 parent 7b0c339 commit 7a0051f

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
@@ -1985,7 +1985,17 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
19851985
if (info->filename != NULL) {
19861986
// XXX There's a refleak somewhere with the filename.
19871987
// Until we can track it down, we intern it.
1988-
PyObject *filename = Py_NewRef(info->filename);
1988+
PyObject *filename = NULL;
1989+
if (switched) {
1990+
// The original filename may be allocated by subinterpreter's
1991+
// obmalloc, so we create a copy here.
1992+
filename = _PyUnicode_Copy(info->filename);
1993+
if (filename == NULL) {
1994+
return NULL;
1995+
}
1996+
} else {
1997+
filename = Py_NewRef(info->filename);
1998+
}
19891999
PyUnicode_InternInPlace(&filename);
19902000
if (PyModule_AddObjectRef(mod, "__file__", filename) < 0) {
19912001
PyErr_Clear(); /* Not important enough to report */

0 commit comments

Comments
 (0)