Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Lib/test/test_exception_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,38 @@ def test_split_does_not_copy_non_sequence_notes(self):
self.assertNotHasAttr(match, '__notes__')
self.assertNotHasAttr(rest, '__notes__')

def test_split_and_subgroup_traceback_from_derive_is_replaced(self):
# gh-162455: when the original group has no traceback, split()
# and subgroup() must not keep the traceback that derive() set.
# The docs say these fields are copied from the original group,
# "so these fields do not need to be updated by derive".
class TracedGroup(ExceptionGroup):
def derive(self, excs):
g = TracedGroup(self.message, excs)
try:
raise g
except TracedGroup as x:
return x

eg = TracedGroup("eg", [ValueError(1), TypeError(2)])
self.assertIsNone(eg.__traceback__)
match, rest = eg.split(ValueError)
self.assertIsNone(match.__traceback__)
self.assertIsNone(rest.__traceback__)
self.assertIsNone(eg.subgroup(ValueError).__traceback__)

def test_split_and_subgroup_copy_traceback(self):
# The original group's traceback is copied to the split parts.
try:
raise ExceptionGroup("eg", [ValueError(1), TypeError(2)])
except ExceptionGroup as eg:
tb = eg.__traceback__
self.assertIsNotNone(tb)
match, rest = eg.split(ValueError)
self.assertIs(match.__traceback__, tb)
self.assertIs(rest.__traceback__, tb)
self.assertIs(eg.subgroup(ValueError).__traceback__, tb)

def test_drive_invalid_return_value(self):
class MyEg(ExceptionGroup):
def derive(self, excs):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``BaseExceptionGroup.split`` and ``BaseExceptionGroup.subgroup`` keeping the traceback set by ``BaseExceptionGroup.derive`` when the original group has no traceback. The split parts now have no traceback, matching the documented behaviour.
13 changes: 7 additions & 6 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1166,12 +1166,13 @@ exceptiongroup_subset(
/* Now we hold a reference to the new eg */

PyObject *tb = PyException_GetTraceback(orig);
if (tb) {
int res = PyException_SetTraceback(eg, tb);
Py_DECREF(tb);
if (res < 0) {
goto error;
}
/* Copy the traceback even when the original has none, so the parts
* do not keep whatever traceback derive() gave them. Passing
* Py_None clears the traceback (SetTraceback rejects NULL). */
int res = PyException_SetTraceback(eg, tb ? tb : Py_None);
Py_XDECREF(tb);
if (res < 0) {
goto error;
}
PyException_SetContext(eg, PyException_GetContext(orig));
PyException_SetCause(eg, PyException_GetCause(orig));
Expand Down
Loading