Skip to content

Commit b90572a

Browse files
authored
gh-161996: Fix crash in Interpreter.exec when the uncaught exception's __str__ raises (GH-162000)
1 parent 6903425 commit b90572a

3 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎Lib/test/test_interpreters/test_api.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,21 @@ def test_failure(self):
947947
with self.assertRaises(ExecutionFailed):
948948
interp.exec('raise Exception')
949949

950+
def test_failure_with_broken_exception_str(self):
951+
interp = interpreters.create()
952+
for raised, expected in [
953+
('ValueError', interpreters.InterpreterError),
954+
('MemoryError', MemoryError),
955+
]:
956+
with self.subTest(raised=raised):
957+
with self.assertRaises(expected):
958+
interp.exec(dedent(f"""
959+
class BrokenError(Exception):
960+
def __str__(self):
961+
raise {raised}
962+
raise BrokenError
963+
"""))
964+
950965
@force_not_colorized
951966
def test_display_preserved_exception(self):
952967
tempdir = self.temp_dir()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash in :meth:`concurrent.interpreters.Interpreter.exec` when the
2+
uncaught exception's ``__str__`` raises.

‎Python/crossinterp.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,14 @@ _PyXI_ApplyError(_PyXI_error *error, const char *failure)
20182018
PyThreadState *tstate = PyThreadState_Get();
20192019

20202020
if (failure != NULL) {
2021+
if (error->override != NULL
2022+
&& error->override->code == _PyXI_ERR_NO_MEMORY)
2023+
{
2024+
PyErr_NoMemory();
2025+
}
2026+
else {
2027+
PyErr_SetString(PyExc_InterpreterError, failure);
2028+
}
20212029
xi_error_clear(error);
20222030
return NULL;
20232031
}

0 commit comments

Comments
 (0)