]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-150454: Fix wrong-interpreter return in import_run_extension error path (#150455)
authorpengyu lee <lipengyu@kylinos.cn>
Fri, 29 May 2026 10:03:37 +0000 (18:03 +0800)
committerGitHub <noreply@github.com>
Fri, 29 May 2026 10:03:37 +0000 (15:33 +0530)
Python/import.c

index 8d57b92925b63035fd633200d4fa3b9a62a56493..2a8f7bddb9898672f349835c9dc82a4adc0239cc 100644 (file)
@@ -2168,6 +2168,7 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
 
     struct _Py_ext_module_loader_result res;
     int rc = _PyImport_RunModInitFunc(p0, info, &res);
+    bool main_error = false;
     if (rc < 0) {
         /* We discard res.def. */
         assert(res.module == NULL);
@@ -2192,7 +2193,8 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
                     // obmalloc, so we create a copy here.
                     filename = _PyUnicode_Copy(info->filename);
                     if (filename == NULL) {
-                        return NULL;
+                        main_error = true;
+                        goto main_finally;
                     }
                 }
                 else {
@@ -2238,6 +2240,7 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
                     main_tstate, info->path, info->name, def, &singlephase);
             if (cached == NULL) {
                 assert(PyErr_Occurred());
+                main_error = true;
                 goto main_finally;
             }
         }
@@ -2253,7 +2256,7 @@ main_finally:
         // gh-144601: The exception object can't be transferred across
         // interpreters. Instead, we print out an unraisable exception, and
         // then raise a different exception for the calling interpreter.
-        if (rc < 0) {
+        if (rc < 0 || main_error) {
             assert(PyErr_Occurred());
             PyErr_FormatUnraisable("Exception while importing from subinterpreter");
         }
@@ -2262,7 +2265,7 @@ main_finally:
         /* Any module we got from the init function will have to be
          * reloaded in the subinterpreter. */
         mod = NULL;
-        if (rc < 0) {
+        if (rc < 0 || main_error) {
             PyErr_SetString(PyExc_ImportError,
                             "failed to import from subinterpreter due to exception");
             goto error;
@@ -2277,6 +2280,9 @@ main_finally:
     if (rc < 0) {
         goto error;
     }
+    if (main_error) {
+        goto error;
+    }
 
     if (res.kind == _Py_ext_module_kind_MULTIPHASE) {
         assert_multiphase_def(def);