https://github.com/python/cpython/commit/28140d1f2da1766bfbb83f58779f15255c73c871
commit: 28140d1f2da1766bfbb83f58779f15255c73c871
branch: main
author: AN Long <a...@users.noreply.github.com>
committer: kumaraditya303 <kumaradi...@python.org>
date: 2024-06-17T21:27:22+05:30
summary:

gh-115649: Copy the filename into main interpreter before intern in import.c 
(#120315)

Co-authored-by: Kumar Aditya <kumaradi...@python.org>

files:
M Lib/test/test_import/__init__.py
M Python/import.c

diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py
index 97e262cfdb9023..e29097baaf53ae 100644
--- a/Lib/test/test_import/__init__.py
+++ b/Lib/test/test_import/__init__.py
@@ -2138,6 +2138,8 @@ def test_single_init_extension_compat(self):
             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):
diff --git a/Python/import.c b/Python/import.c
index 2c7a461ac786c8..932881950d7baa 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -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 */

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to