Skip to content

Commit dbd72d1

Browse files
committed
add recursion guard in split
1 parent 26b0426 commit dbd72d1

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Lib/test/test_exception_group.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,24 @@ def test_basics_split_by_predicate__match(self):
337337
rest, ExceptionGroup, rest_template)
338338

339339

340+
class DeepRecursionInSplitAndSubgroup(unittest.TestCase):
341+
def make_deep_eg(self):
342+
e = TypeError(1)
343+
for i in range(2000):
344+
e = ExceptionGroup('eg', [e])
345+
return e
346+
347+
def test_deep_split(self):
348+
e = self.make_deep_eg()
349+
with self.assertRaises(RecursionError):
350+
e.split(TypeError)
351+
352+
def test_deep_subgroup(self):
353+
e = self.make_deep_eg()
354+
with self.assertRaises(RecursionError):
355+
e.subgroup(TypeError)
356+
357+
340358
def leaf_generator(exc, tbs=None):
341359
if tbs is None:
342360
tbs = []

Objects/exceptions.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,13 +982,18 @@ exceptiongroup_split_recursive(PyObject *exc,
982982
for (Py_ssize_t i = 0; i < num_excs; i++) {
983983
PyObject *e = PyTuple_GET_ITEM(eg->excs, i);
984984
_exceptiongroup_split_result rec_result;
985+
if (Py_EnterRecursiveCall(" in exceptiongroup_split_recursive")) {
986+
goto done;
987+
}
985988
if (exceptiongroup_split_recursive(
986989
e, matcher_type, matcher_value,
987990
construct_rest, &rec_result) == -1) {
988991
assert(!rec_result.match);
989992
assert(!rec_result.rest);
993+
Py_LeaveRecursiveCall();
990994
goto done;
991995
}
996+
Py_LeaveRecursiveCall();
992997
if (rec_result.match) {
993998
assert(PyList_CheckExact(match_list));
994999
if (PyList_Append(match_list, rec_result.match) == -1) {

0 commit comments

Comments
 (0)