]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-115649: Copy the filename into main interpreter before intern in import...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 17 Jun 2024 17:00:26 +0000 (19:00 +0200)
committerGitHub <noreply@github.com>
Mon, 17 Jun 2024 17:00:26 +0000 (22:30 +0530)
gh-115649: Copy the filename into main interpreter before intern in import.c (GH-120315)
(cherry picked from commit 28140d1f2da1766bfbb83f58779f15255c73c871)

Co-authored-by: AN Long <aisk@users.noreply.github.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Lib/test/test_import/__init__.py
Python/import.c

index b09065f812c0e499b6f5070b309d129a7f8918bd..f9e8558d1a71c80bddd91140423b599be2c192d7 100644 (file)
@@ -2157,6 +2157,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 10ac49fd95fc5925c4b54f6efbfb5a285d8d38c7..8cf97b51f65fecd95b1145ac4b6387e8b5bb405e 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 */