Skip to content

Commit 9314ec2

Browse files
[3.13] gh-144601: Avoid sharing exception objects raised in a PyInit function across multiple interpreters (GH-144602) (GH-144880)
(cherry picked from commit fd6b639)
1 parent e701a5c commit 9314ec2

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

Lib/test/test_import/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
requires_gil_enabled,
4343
Py_GIL_DISABLED,
4444
force_not_colorized_test_class,
45+
catch_unraisable_exception
4546
)
4647
from test.support.import_helper import (
4748
forget, make_legacy_pyc, unlink, unload, ready_to_import,
@@ -2559,6 +2560,32 @@ def test_disallowed_reimport(self):
25592560
excsnap = _interpreters.run_string(interpid, script)
25602561
self.assertIsNot(excsnap, None)
25612562

2563+
@requires_subinterpreters
2564+
def test_pyinit_function_raises_exception(self):
2565+
# gh-144601: PyInit functions that raised exceptions would cause a
2566+
# crash when imported from a subinterpreter.
2567+
import _testsinglephase
2568+
filename = _testsinglephase.__file__
2569+
script = f"""if True:
2570+
from test.test_import import import_extension_from_file
2571+
2572+
import_extension_from_file('_testsinglephase_raise_exception', {filename!r})"""
2573+
2574+
interp = _interpreters.create()
2575+
try:
2576+
with catch_unraisable_exception() as cm:
2577+
exception = _interpreters.run_string(interp, script)
2578+
unraisable = cm.unraisable
2579+
finally:
2580+
_interpreters.destroy(interp)
2581+
2582+
self.assertIsNotNone(exception)
2583+
self.assertIsNotNone(exception.type.__name__, "ImportError")
2584+
self.assertIsNotNone(exception.msg, "failed to import from subinterpreter due to exception")
2585+
self.assertIsNotNone(unraisable)
2586+
self.assertIs(unraisable.exc_type, RuntimeError)
2587+
self.assertEqual(str(unraisable.exc_value), "evil")
2588+
25622589

25632590
class TestSinglePhaseSnapshot(ModuleSnapshot):
25642591
"""A representation of a single-phase init module for testing.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix crash when importing a module whose ``PyInit`` function raises an
2+
exception from a subinterpreter.

Modules/_testsinglephase.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,3 +799,11 @@ PyInit__testsinglephase_circular(void)
799799
}
800800
return Py_NewRef(static_module_circular);
801801
}
802+
803+
804+
PyMODINIT_FUNC
805+
PyInit__testsinglephase_raise_exception(void)
806+
{
807+
PyErr_SetString(PyExc_RuntimeError, "evil");
808+
return NULL;
809+
}

Python/import.c

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

20912091
main_finally:
2092+
if (rc < 0) {
2093+
_Py_ext_module_loader_result_apply_error(&res, name_buf);
2094+
}
2095+
20922096
/* Switch back to the subinterpreter. */
20932097
if (switched) {
2098+
// gh-144601: The exception object can't be transferred across
2099+
// interpreters. Instead, we print out an unraisable exception, and
2100+
// then raise a different exception for the calling interpreter.
2101+
if (rc < 0) {
2102+
assert(PyErr_Occurred());
2103+
PyErr_FormatUnraisable("Exception while importing from subinterpreter");
2104+
}
20942105
assert(main_tstate != tstate);
20952106
switch_back_from_main_interpreter(tstate, main_tstate, mod);
20962107
/* Any module we got from the init function will have to be
20972108
* reloaded in the subinterpreter. */
20982109
mod = NULL;
2110+
if (rc < 0) {
2111+
PyErr_SetString(PyExc_ImportError,
2112+
"failed to import from subinterpreter due to exception");
2113+
goto error;
2114+
}
20992115
}
21002116

21012117
/*****************************************************************/
@@ -2104,7 +2120,6 @@ import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
21042120

21052121
/* Finally we handle the error return from _PyImport_RunModInitFunc(). */
21062122
if (rc < 0) {
2107-
_Py_ext_module_loader_result_apply_error(&res, name_buf);
21082123
goto error;
21092124
}
21102125

0 commit comments

Comments
 (0)