Skip to content

Commit b1cd8eb

Browse files
christianaurichzmmiss-islington
authored andcommitted
gh-159294: Fix crash when creating an ExceptionGroup fails with MemoryError (GH-159295)
(cherry picked from commit 2507a7f) Co-authored-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
1 parent 1fa5e07 commit b1cd8eb

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
@@ -998,11 +998,11 @@ BaseExceptionGroup_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
998998
*/
999999
cls = (PyTypeObject*)PyExc_BaseExceptionGroup;
10001000
}
1001-
PyBaseExceptionGroupObject *self =
1002-
PyBaseExceptionGroupObject_CAST(BaseException_new(cls, args, kwds));
1003-
if (!self) {
1001+
PyObject *op = BaseException_new(cls, args, kwds);
1002+
if (!op) {
10041003
goto error;
10051004
}
1005+
PyBaseExceptionGroupObject *self = PyBaseExceptionGroupObject_CAST(op);
10061006

10071007
self->msg = Py_NewRef(message);
10081008
self->excs = exceptions;

0 commit comments

Comments
 (0)