From 2b99cf9b705de688ff764dd6a728c84ebd3f3687 Mon Sep 17 00:00:00 2001 From: Gaurav Kumar Yadav Date: Sun, 22 Nov 2026 23:39:52 +0000 Subject: [PATCH] gh-162455: Copy the traceback in BaseExceptionGroup.split() and subgroup() even when the original has none When the original group has no traceback, exceptiongroup_subset() skipped copying it, so the parts kept whatever traceback derive() had set. The docs say split() and subgroup() copy __traceback__ from the original group, "so these fields do not need to be updated by derive". The traceback is now always copied; when the original has none, the parts traceback is cleared (Py_None, since SetTraceback rejects NULL). Signed-off-by: Gaurav Kumar Yadav --- Lib/test/test_exception_group.py | 32 +++++++++++++++++++ ...-11-23-05-30-00.gh-issue-162455.x7K2pQ.rst | 1 + Objects/exceptions.c | 13 ++++---- 3 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-23-05-30-00.gh-issue-162455.x7K2pQ.rst diff --git a/Lib/test/test_exception_group.py b/Lib/test/test_exception_group.py index ec3b346a8db4486..8318ef6a1815b7d 100644 --- a/Lib/test/test_exception_group.py +++ b/Lib/test/test_exception_group.py @@ -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): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-23-05-30-00.gh-issue-162455.x7K2pQ.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-23-05-30-00.gh-issue-162455.x7K2pQ.rst new file mode 100644 index 000000000000000..056416b60dbd70b --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-23-05-30-00.gh-issue-162455.x7K2pQ.rst @@ -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. diff --git a/Objects/exceptions.c b/Objects/exceptions.c index dc0ba0d7e533dda..d0fedb63cd6d0cd 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -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));