From e058be6ce4415462bfb61eb92b2dc4be2ec7fe8f Mon Sep 17 00:00:00 2001 From: An Long Date: Sat, 14 Nov 2026 20:55:40 +0900 Subject: [PATCH 1/3] Fix crash in Interpreter.exec() when the uncaught exception's __str__ raises Co-Authored-By: Claude Fable 5.2 --- Lib/test/test_interpreters/test_api.py | 10 ++++++++++ Python/crossinterp.c | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/Lib/test/test_interpreters/test_api.py b/Lib/test/test_interpreters/test_api.py index a9e296891bb8705..1ceb5916bd5f9f5 100644 --- a/Lib/test/test_interpreters/test_api.py +++ b/Lib/test/test_interpreters/test_api.py @@ -947,6 +947,16 @@ def test_failure(self): with self.assertRaises(ExecutionFailed): interp.exec('raise Exception') + def test_failure_with_broken_exception_str(self): + interp = interpreters.create() + with self.assertRaises(interpreters.InterpreterError): + interp.exec(dedent(""" + class BrokenError(Exception): + def __str__(self): + raise ValueError + raise BrokenError + """)) + @force_not_colorized def test_display_preserved_exception(self): tempdir = self.temp_dir() diff --git a/Python/crossinterp.c b/Python/crossinterp.c index d189d7bcefe313e..a91b45ba2cbcf45 100644 --- a/Python/crossinterp.c +++ b/Python/crossinterp.c @@ -2018,6 +2018,14 @@ _PyXI_ApplyError(_PyXI_error *error, const char *failure) PyThreadState *tstate = PyThreadState_Get(); if (failure != NULL) { + if (error->override != NULL + && error->override->code == _PyXI_ERR_NO_MEMORY) + { + PyErr_NoMemory(); + } + else { + PyErr_SetString(PyExc_InterpreterError, failure); + } xi_error_clear(error); return NULL; } From fc49e09cfa15dc4769bb0efcbff75acdada409a2 Mon Sep 17 00:00:00 2001 From: An Long Date: Sat, 14 Nov 2026 21:05:52 +0900 Subject: [PATCH 2/3] Add news entry --- .../next/Library/2026-11-14-21-04-56.gh-issue-161996.35uL2d.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-14-21-04-56.gh-issue-161996.35uL2d.rst diff --git a/Misc/NEWS.d/next/Library/2026-11-14-21-04-56.gh-issue-161996.35uL2d.rst b/Misc/NEWS.d/next/Library/2026-11-14-21-04-56.gh-issue-161996.35uL2d.rst new file mode 100644 index 000000000000000..561bb62d6537073 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-14-21-04-56.gh-issue-161996.35uL2d.rst @@ -0,0 +1,2 @@ +Fix a crash in :meth:`concurrent.interpreters.Interpreter.exec` when the +uncaught exception's ``__str__`` raises. From 820c90d98a67f8e80d0b577b002feb46b007a450 Mon Sep 17 00:00:00 2001 From: An Long Date: Sat, 14 Nov 2026 22:50:43 +0900 Subject: [PATCH 3/3] Test the no memory case Co-Authored-By: Claude Fable 5.2 --- Lib/test/test_interpreters/test_api.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_interpreters/test_api.py b/Lib/test/test_interpreters/test_api.py index 1ceb5916bd5f9f5..e9a64915a1b4a87 100644 --- a/Lib/test/test_interpreters/test_api.py +++ b/Lib/test/test_interpreters/test_api.py @@ -949,13 +949,18 @@ def test_failure(self): def test_failure_with_broken_exception_str(self): interp = interpreters.create() - with self.assertRaises(interpreters.InterpreterError): - interp.exec(dedent(""" - class BrokenError(Exception): - def __str__(self): - raise ValueError - raise BrokenError - """)) + for raised, expected in [ + ('ValueError', interpreters.InterpreterError), + ('MemoryError', MemoryError), + ]: + with self.subTest(raised=raised): + with self.assertRaises(expected): + interp.exec(dedent(f""" + class BrokenError(Exception): + def __str__(self): + raise {raised} + raise BrokenError + """)) @force_not_colorized def test_display_preserved_exception(self):