Skip to content

Commit 5893482

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

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
@@ -891,6 +891,21 @@ def test_failure(self):
891891
with self.assertRaises(ExecutionFailed):
892892
interp.exec('raise Exception')
893893

894+
def test_failure_with_broken_exception_str(self):
895+
interp = interpreters.create()
896+
for raised, expected in [
897+
('ValueError', interpreters.InterpreterError),
898+
('MemoryError', MemoryError),
899+
]:
900+
with self.subTest(raised=raised):
901+
with self.assertRaises(expected):
902+
interp.exec(dedent(f"""
903+
class BrokenError(Exception):
904+
def __str__(self):
905+
raise {raised}
906+
raise BrokenError
907+
"""))
908+
894909
@force_not_colorized
895910
def test_display_preserved_exception(self):
896911
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
@@ -1972,6 +1972,14 @@ _PyXI_ApplyError(_PyXI_error *error, const char *failure)
19721972
PyThreadState *tstate = PyThreadState_Get();
19731973

19741974
if (failure != NULL) {
1975+
if (error->override != NULL
1976+
&& error->override->code == _PyXI_ERR_NO_MEMORY)
1977+
{
1978+
PyErr_NoMemory();
1979+
}
1980+
else {
1981+
PyErr_SetString(PyExc_InterpreterError, failure);
1982+
}
19751983
xi_error_clear(error);
19761984
return NULL;
19771985
}

0 commit comments

Comments
 (0)