https://github.com/python/cpython/commit/58934828d41c190fdb0463fba0a8cefd12694269 commit: 58934828d41c190fdb0463fba0a8cefd12694269 branch: 3.14 author: Miss Islington (bot) <[email protected]> committer: ZeroIntensity <[email protected]> date: 2026-11-17T03:31:17Z summary:
[3.14] gh-161996: Fix crash in `Interpreter.exec` when the uncaught exception's `__str__` raises (GH-162000) (GH-162146) (cherry picked from commit b90572af8f94568d9373edb7b1f22ee09d49b8a4) Co-authored-by: An Long <[email protected]> files: A Misc/NEWS.d/next/Library/2026-11-14-21-04-56.gh-issue-161996.35uL2d.rst M Lib/test/test_interpreters/test_api.py M Python/crossinterp.c diff --git a/Lib/test/test_interpreters/test_api.py b/Lib/test/test_interpreters/test_api.py index 3a49586ae140edc..4128c18047985d1 100644 --- a/Lib/test/test_interpreters/test_api.py +++ b/Lib/test/test_interpreters/test_api.py @@ -891,6 +891,21 @@ def test_failure(self): with self.assertRaises(ExecutionFailed): interp.exec('raise Exception') + def test_failure_with_broken_exception_str(self): + interp = interpreters.create() + 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): tempdir = self.temp_dir() 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. diff --git a/Python/crossinterp.c b/Python/crossinterp.c index 19c19145143151e..520df30f41958b0 100644 --- a/Python/crossinterp.c +++ b/Python/crossinterp.c @@ -1972,6 +1972,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; } _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
