From 65410868bca9efbcdd3a889cde6ff64098a8c3d4 Mon Sep 17 00:00:00 2001 From: AN Long Date: Mon, 10 Jun 2024 20:39:48 +0800 Subject: [PATCH 1/5] Copy the filename into main interpreter before intern in import.c --- Python/import.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Python/import.c b/Python/import.c index 2c7a461ac786c8..42c88dcfc1c8fb 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1969,7 +1969,12 @@ 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); + // The original filename may be allocated by subinterpreter's + // obmaloc, so we create a copy here. + PyObject *filename = PyUnicode_FromString(PyUnicode_AsUTF8(info->filename)); + if (filename == NULL) { + return NULL; + } PyUnicode_InternInPlace(&filename); if (PyModule_AddObjectRef(mod, "__file__", filename) < 0) { PyErr_Clear(); /* Not important enough to report */ From b6dd4e65e9e0399bc72a437cbdef2f35795e9a38 Mon Sep 17 00:00:00 2001 From: AN Long Date: Mon, 10 Jun 2024 21:34:09 +0800 Subject: [PATCH 2/5] Only copy then switched --- Python/import.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Python/import.c b/Python/import.c index 42c88dcfc1c8fb..0d38a69fc93f1b 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1969,11 +1969,16 @@ 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. - // The original filename may be allocated by subinterpreter's - // obmaloc, so we create a copy here. - PyObject *filename = PyUnicode_FromString(PyUnicode_AsUTF8(info->filename)); - if (filename == NULL) { - return NULL; + PyObject *filename = NULL; + if (switched) { + // The original filename may be allocated by subinterpreter's + // obmaloc, so we create a copy here. + filename = PyUnicode_FromString(PyUnicode_AsUTF8(info->filename)); + if (filename == NULL) { + return NULL; + } + } else { + filename = Py_NewRef(info->filename); } PyUnicode_InternInPlace(&filename); if (PyModule_AddObjectRef(mod, "__file__", filename) < 0) { From 455fe0942c09dc6bab2c2048ee6e70f2bae5a046 Mon Sep 17 00:00:00 2001 From: AN Long Date: Sun, 16 Jun 2024 22:54:09 +0800 Subject: [PATCH 3/5] Add test --- Lib/test/test_import/__init__.py | 2 ++ 1 file changed, 2 insertions(+) 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): From eb1ab13cb12fdf77a4d857f73d32edeb9f3f5b50 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Mon, 17 Jun 2024 20:44:29 +0530 Subject: [PATCH 4/5] Update Python/import.c --- Python/import.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/import.c b/Python/import.c index 0d38a69fc93f1b..c3fe8514afbedb 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1972,7 +1972,7 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0, PyObject *filename = NULL; if (switched) { // The original filename may be allocated by subinterpreter's - // obmaloc, so we create a copy here. + // obmalloc, so we create a copy here. filename = PyUnicode_FromString(PyUnicode_AsUTF8(info->filename)); if (filename == NULL) { return NULL; From 3087ed5989c7d536df585506e5b8661a67dc3adf Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Mon, 17 Jun 2024 20:47:23 +0530 Subject: [PATCH 5/5] Update Python/import.c --- Python/import.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/import.c b/Python/import.c index c3fe8514afbedb..932881950d7baa 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1973,7 +1973,7 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0, if (switched) { // The original filename may be allocated by subinterpreter's // obmalloc, so we create a copy here. - filename = PyUnicode_FromString(PyUnicode_AsUTF8(info->filename)); + filename = _PyUnicode_Copy(info->filename); if (filename == NULL) { return NULL; }