Skip to content

Commit aded5b9

Browse files
miss-islingtonaisk
andauthored
[3.15] gh-161996: Fix crash in Interpreter.exec when the uncaught exception's __str__ raises (GH-162000) (GH-162145)
(cherry picked from commit b90572a) Co-authored-by: An Long <aisk@users.noreply.github.com>
1 parent 87b06c9 commit aded5b9

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
@@ -922,6 +922,21 @@ def test_failure(self):
922922
with self.assertRaises(ExecutionFailed):
923923
interp.exec('raise Exception')
924924

925+
def test_failure_with_broken_exception_str(self):
926+
interp = interpreters.create()
927+
for raised, expected in [
928+
('ValueError', interpreters.InterpreterError),
929+
('MemoryError', MemoryError),
930+
]:
931+
with self.subTest(raised=raised):
932+
with self.assertRaises(expected):
933+
interp.exec(dedent(f"""
934+
class BrokenError(Exception):
935+
def __str__(self):
936+
raise {raised}
937+
raise BrokenError
938+
"""))
939+
925940
@force_not_colorized
926941
def test_display_preserved_exception(self):
927942
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)