Skip to content

Commit b72ee13

Browse files
[3.15] gh-159294: Fix crash when creating an ExceptionGroup fails with MemoryError (GH-159295) (#160504)
(cherry picked from commit 2507a7f) Co-authored-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
1 parent e243e46 commit b72ee13

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
@@ -990,11 +990,11 @@ BaseExceptionGroup_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
990990
*/
991991
cls = (PyTypeObject*)PyExc_BaseExceptionGroup;
992992
}
993-
PyBaseExceptionGroupObject *self =
994-
PyBaseExceptionGroupObject_CAST(BaseException_new(cls, args, kwds));
995-
if (!self) {
993+
PyObject *op = BaseException_new(cls, args, kwds);
994+
if (!op) {
996995
goto error;
997996
}
997+
PyBaseExceptionGroupObject *self = PyBaseExceptionGroupObject_CAST(op);
998998

999999
self->msg = Py_NewRef(message);
10001000
self->excs = exceptions;

0 commit comments

Comments
 (0)