[Python-checkins] bpo-46107: ExceptionGroup.subgroup()/split() should copy __note__ to the parts (GH-30159)

iritkatriel webhook-mailer at python.org
Tue Dec 21 05:12:34 EST 2021


https://github.com/python/cpython/commit/c66fc0fb53b5316dc325fde3bc738890515d38a4
commit: c66fc0fb53b5316dc325fde3bc738890515d38a4
branch: main
author: Irit Katriel <1055913+iritkatriel at users.noreply.github.com>
committer: iritkatriel <1055913+iritkatriel at users.noreply.github.com>
date: 2021-12-21T10:12:26Z
summary:

bpo-46107: ExceptionGroup.subgroup()/split() should copy __note__ to the parts (GH-30159)

files:
A Misc/NEWS.d/next/Core and Builtins/2021-12-16-23-27-05.bpo-46107.7q5an0.rst
M Lib/test/test_exception_group.py
M Objects/exceptions.c

diff --git a/Lib/test/test_exception_group.py b/Lib/test/test_exception_group.py
index c793c45e7241a..f0ae37741ab60 100644
--- a/Lib/test/test_exception_group.py
+++ b/Lib/test/test_exception_group.py
@@ -495,13 +495,14 @@ def leaves(exc):
                 match and e in match_leaves,
                 rest and e in rest_leaves)
 
-        # message, cause and context equal to eg
+        # message, cause and context, traceback and note equal to eg
         for part in [match, rest, sg]:
             if part is not None:
                 self.assertEqual(eg.message, part.message)
                 self.assertIs(eg.__cause__, part.__cause__)
                 self.assertIs(eg.__context__, part.__context__)
                 self.assertIs(eg.__traceback__, part.__traceback__)
+                self.assertIs(eg.__note__, part.__note__)
 
         def tbs_for_leaf(leaf, eg):
             for e, tbs in leaf_generator(eg):
@@ -566,6 +567,7 @@ def level3(i):
         try:
             nested_group()
         except ExceptionGroup as e:
+            e.__note__ = f"the note: {id(e)}"
             eg = e
 
         eg_template = [
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-12-16-23-27-05.bpo-46107.7q5an0.rst b/Misc/NEWS.d/next/Core and Builtins/2021-12-16-23-27-05.bpo-46107.7q5an0.rst
new file mode 100644
index 0000000000000..3257805f2ab5a
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-12-16-23-27-05.bpo-46107.7q5an0.rst	
@@ -0,0 +1 @@
+Fix bug where :meth:`ExceptionGroup.split` and :meth:`ExceptionGroup.subgroup` did not copy the exception group's ``__note__`` field to the parts.
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index 1db49d949b5c0..d82340b8e78ab 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -901,6 +901,11 @@ exceptiongroup_subset(
     }
     PyException_SetContext(eg, PyException_GetContext(orig));
     PyException_SetCause(eg, PyException_GetCause(orig));
+
+    PyObject *note = _PyBaseExceptionObject_cast(orig)->note;
+    Py_XINCREF(note);
+    _PyBaseExceptionObject_cast(eg)->note = note;
+
     *result = eg;
     return 0;
 error:



More information about the Python-checkins mailing list