]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-115649: Copy the filename into main interpreter before intern in import.c (#120315)
authorAN Long <aisk@users.noreply.github.com>
Mon, 17 Jun 2024 15:57:22 +0000 (23:57 +0800)
committerGitHub <noreply@github.com>
Mon, 17 Jun 2024 15:57:22 +0000 (21:27 +0530)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Lib/test/test_import/__init__.py
Python/import.c

index 97e262cfdb90239906fb68161d8693117b1eb8ae..e29097baaf53ae1c25f6e369c97b8b1530c8aae4 100644 (file)
@@ -2138,6 +2138,8 @@ class SubinterpImportTests(unittest.TestCase):
             self.check_incompatible_here(module)
         with self.subTest(f'{module}: strict, fresh'):
             self.check_incompatible_fresh(module)
+        with self.subTest(f'{module}: isolated, fresh'):
+            self.check_incompatible_fresh(module, isolated=True)
 
     @unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
     def test_multi_init_extension_compat(self):
index 2c7a461ac786c87e2ef5eb0b6daedfe4ab2a1e1b..932881950d7baa75cce26439745fb7f73a544596 100644 (file)
@@ -1969,7 +1969,17 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
             if (info->filename != NULL) {
                 // XXX There's a refleak somewhere with the filename.
                 // Until we can track it down, we intern it.
-                PyObject *filename = Py_NewRef(info->filename);
+                PyObject *filename = NULL;
+                if (switched) {
+                    // The original filename may be allocated by subinterpreter's
+                    // obmalloc, so we create a copy here.
+                    filename = _PyUnicode_Copy(info->filename);
+                    if (filename == NULL) {
+                        return NULL;
+                    }
+                } else {
+                    filename = Py_NewRef(info->filename);
+                }
                 PyUnicode_InternInPlace(&filename);
                 if (PyModule_AddObjectRef(mod, "__file__", filename) < 0) {
                     PyErr_Clear(); /* Not important enough to report */