Skip to content

Commit 6f63e57

Browse files
committed
Don't use exception object across interpreters.
1 parent d736349 commit 6f63e57

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

Python/import.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2176,13 +2176,29 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
21762176
}
21772177

21782178
main_finally:
2179+
if (rc < 0) {
2180+
_Py_ext_module_loader_result_apply_error(&res, name_buf);
2181+
}
2182+
21792183
/* Switch back to the subinterpreter. */
21802184
if (switched) {
2185+
// gh-144601: The exception object can't be transferred across
2186+
// interpreters. Instead, we print out an unraisable exception, and
2187+
// then raise a different exception for the calling interpreter.
2188+
if (rc < 0) {
2189+
assert(PyErr_Occurred());
2190+
PyErr_FormatUnraisable("Exception while importing from subinterpreter");
2191+
}
21812192
assert(main_tstate != tstate);
21822193
switch_back_from_main_interpreter(tstate, main_tstate, mod);
21832194
/* Any module we got from the init function will have to be
21842195
* reloaded in the subinterpreter. */
21852196
mod = NULL;
2197+
if (rc < 0) {
2198+
PyErr_SetString(PyExc_ImportError,
2199+
"failed to import from subinterpreter due to exception");
2200+
goto error;
2201+
}
21862202
}
21872203

21882204
/*****************************************************************/
@@ -2191,7 +2207,6 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
21912207

21922208
/* Finally we handle the error return from _PyImport_RunModInitFunc(). */
21932209
if (rc < 0) {
2194-
_Py_ext_module_loader_result_apply_error(&res, name_buf);
21952210
goto error;
21962211
}
21972212

0 commit comments

Comments
 (0)