diff --git a/Lib/test/test_exception_group.py b/Lib/test/test_exception_group.py index f79bfa4ae2d322..ec3b346a8db448 100644 --- a/Lib/test/test_exception_group.py +++ b/Lib/test/test_exception_group.py @@ -3,7 +3,7 @@ import unittest from test.support import (skip_emscripten_stack_overflow, skip_wasi_stack_overflow, run_with_limited_c_stack, - exceeds_recursion_limit) + exceeds_recursion_limit, nomemtest) class TestExceptionGroupTypeHierarchy(unittest.TestCase): def test_exception_group_types(self): @@ -130,6 +130,15 @@ class MyBEG(BaseExceptionGroup): type(MyBEG("eg", [ValueError(12), KeyboardInterrupt(42)])), MyBEG) + @nomemtest + def test_no_memory(self): + import _testcapi + # A tuple of exceptions is not copied, so the first allocation + # is the one of the exception group itself. + excs = (ValueError(1),) + with self.assertRaises(MemoryError): + _testcapi.call_with_nomemory(0, 0, ExceptionGroup, "eg", excs) + class StrAndReprTests(unittest.TestCase): def test_ExceptionGroup(self): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-06-23-56.gh-issue-159294.9LTsNA.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-06-23-56.gh-issue-159294.9LTsNA.rst new file mode 100644 index 00000000000000..9504f1f753cbec --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-06-23-56.gh-issue-159294.9LTsNA.rst @@ -0,0 +1,3 @@ +Fix a crash in a debug build when creating an :exc:`ExceptionGroup` or +:exc:`BaseExceptionGroup` fails with :exc:`MemoryError`. Patch by Christian +Aurich Zanettini Martins. diff --git a/Objects/exceptions.c b/Objects/exceptions.c index f262a1a52ab3d8..7849c1b840b473 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -990,11 +990,11 @@ BaseExceptionGroup_new(PyTypeObject *type, PyObject *args, PyObject *kwds) */ cls = (PyTypeObject*)PyExc_BaseExceptionGroup; } - PyBaseExceptionGroupObject *self = - PyBaseExceptionGroupObject_CAST(BaseException_new(cls, args, kwds)); - if (!self) { + PyObject *op = BaseException_new(cls, args, kwds); + if (!op) { goto error; } + PyBaseExceptionGroupObject *self = PyBaseExceptionGroupObject_CAST(op); self->msg = Py_NewRef(message); self->excs = exceptions;