Skip to content

Commit e058be6

Browse files
aiskclaude
andcommitted
Fix crash in Interpreter.exec() when the uncaught exception's __str__ raises
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 1d64195 commit e058be6

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

‎Lib/test/test_interpreters/test_api.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,16 @@ 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+
with self.assertRaises(interpreters.InterpreterError):
953+
interp.exec(dedent("""
954+
class BrokenError(Exception):
955+
def __str__(self):
956+
raise ValueError
957+
raise BrokenError
958+
"""))
959+
950960
@force_not_colorized
951961
def test_display_preserved_exception(self):
952962
tempdir = self.temp_dir()

‎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)