Skip to content

Commit c66fc0f

Browse files
authored
bpo-46107: ExceptionGroup.subgroup()/split() should copy __note__ to the parts (GH-30159)
1 parent e9a01e2 commit c66fc0f

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

Lib/test/test_exception_group.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,13 +495,14 @@ def leaves(exc):
495495
match and e in match_leaves,
496496
rest and e in rest_leaves)
497497

498-
# message, cause and context equal to eg
498+
# message, cause and context, traceback and note equal to eg
499499
for part in [match, rest, sg]:
500500
if part is not None:
501501
self.assertEqual(eg.message, part.message)
502502
self.assertIs(eg.__cause__, part.__cause__)
503503
self.assertIs(eg.__context__, part.__context__)
504504
self.assertIs(eg.__traceback__, part.__traceback__)
505+
self.assertIs(eg.__note__, part.__note__)
505506

506507
def tbs_for_leaf(leaf, eg):
507508
for e, tbs in leaf_generator(eg):
@@ -566,6 +567,7 @@ def level3(i):
566567
try:
567568
nested_group()
568569
except ExceptionGroup as e:
570+
e.__note__ = f"the note: {id(e)}"
569571
eg = e
570572

571573
eg_template = [
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug where :meth:`ExceptionGroup.split` and :meth:`ExceptionGroup.subgroup` did not copy the exception group's ``__note__`` field to the parts.

Objects/exceptions.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,11 @@ exceptiongroup_subset(
901901
}
902902
PyException_SetContext(eg, PyException_GetContext(orig));
903903
PyException_SetCause(eg, PyException_GetCause(orig));
904+
905+
PyObject *note = _PyBaseExceptionObject_cast(orig)->note;
906+
Py_XINCREF(note);
907+
_PyBaseExceptionObject_cast(eg)->note = note;
908+
904909
*result = eg;
905910
return 0;
906911
error:

0 commit comments

Comments
 (0)