Skip to content

Commit 2507a7f

Browse files
gh-159294: Fix crash when creating an ExceptionGroup fails with MemoryError (#159295)
1 parent a96fbdc commit 2507a7f

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

‎Lib/test/test_exception_group.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import unittest
44
from test.support import (skip_emscripten_stack_overflow,
55
skip_wasi_stack_overflow, run_with_limited_c_stack,
6-
exceeds_recursion_limit)
6+
exceeds_recursion_limit, nomemtest)
77

88
class TestExceptionGroupTypeHierarchy(unittest.TestCase):
99
def test_exception_group_types(self):
@@ -130,6 +130,15 @@ class MyBEG(BaseExceptionGroup):
130130
type(MyBEG("eg", [ValueError(12), KeyboardInterrupt(42)])),
131131
MyBEG)
132132

133+
@nomemtest
134+
def test_no_memory(self):
135+
import _testcapi
136+
# A tuple of exceptions is not copied, so the first allocation
137+
# is the one of the exception group itself.
138+
excs = (ValueError(1),)
139+
with self.assertRaises(MemoryError):
140+
_testcapi.call_with_nomemory(0, 0, ExceptionGroup, "eg", excs)
141+
133142

134143
class StrAndReprTests(unittest.TestCase):
135144
def test_ExceptionGroup(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash in a debug build when creating an :exc:`ExceptionGroup` or
2+
:exc:`BaseExceptionGroup` fails with :exc:`MemoryError`. Patch by Christian
3+
Aurich Zanettini Martins.

‎Objects/exceptions.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -968,11 +968,11 @@ BaseExceptionGroup_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
968968
*/
969969
cls = (PyTypeObject*)PyExc_BaseExceptionGroup;
970970
}
971-
PyBaseExceptionGroupObject *self =
972-
PyBaseExceptionGroupObject_CAST(BaseException_new(cls, args, kwds));
973-
if (!self) {
971+
PyObject *op = BaseException_new(cls, args, kwds);
972+
if (!op) {
974973
goto error;
975974
}
975+
PyBaseExceptionGroupObject *self = PyBaseExceptionGroupObject_CAST(op);
976976

977977
self->msg = Py_NewRef(message);
978978
self->excs = exceptions;

0 commit comments

Comments
 (0)