From b1cd8eb530310b8f543dc62e2fe0a74d3ae1bc0e Mon Sep 17 00:00:00 2001 From: Christian Aurich Zanettini Martins Date: Fri, 23 Oct 2026 17:57:58 -0300 Subject: [PATCH 1/2] gh-159294: Fix crash when creating an `ExceptionGroup` fails with `MemoryError` (GH-159295) (cherry picked from commit 2507a7fa7361762ed6056dd9088a6b3f6e2c52de) Co-authored-by: Christian Aurich Zanettini Martins --- Lib/test/test_exception_group.py | 11 ++++++++++- .../2026-10-13-06-23-56.gh-issue-159294.9LTsNA.rst | 3 +++ Objects/exceptions.c | 6 +++--- 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-06-23-56.gh-issue-159294.9LTsNA.rst diff --git a/Lib/test/test_exception_group.py b/Lib/test/test_exception_group.py index 23fc4f5d3ea03f..9b266b0a7bf5d7 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 67ba540b552a58..b3473751684f0f 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -998,11 +998,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; From 6cb5bb0303c72f0cdcc370b44013c851555f525a Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Fri, 23 Oct 2026 23:20:02 +0100 Subject: [PATCH 2/2] Drop test --- Lib/test/test_exception_group.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/Lib/test/test_exception_group.py b/Lib/test/test_exception_group.py index 9b266b0a7bf5d7..23fc4f5d3ea03f 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, nomemtest) + exceeds_recursion_limit) class TestExceptionGroupTypeHierarchy(unittest.TestCase): def test_exception_group_types(self): @@ -130,15 +130,6 @@ 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):