]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-103791: handle `BaseExceptionGroup` in `contextlib.suppress()` (GH-111910...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 10 Nov 2023 14:00:48 +0000 (15:00 +0100)
committerGitHub <noreply@github.com>
Fri, 10 Nov 2023 14:00:48 +0000 (14:00 +0000)
gh-103791: handle `BaseExceptionGroup` in `contextlib.suppress()` (GH-111910)
(cherry picked from commit d61313bdb1eee3e4bb111e0b248ac2dbb48be917)

Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
Doc/library/contextlib.rst
Lib/contextlib.py
Lib/test/test_contextlib.py
Misc/NEWS.d/next/Library/2023-11-09-10-45-56.gh-issue-103791.sdfkja.rst [new file with mode: 0644]

index 66b9c13710522350d1661146cd34ad5abdfbb236..f6ebbfacfba509f4927ea6129b00dd082a2c6fd4 100644 (file)
@@ -304,15 +304,15 @@ Functions and classes provided:
 
    This context manager is :ref:`reentrant <reentrant-cms>`.
 
-   If the code within the :keyword:`!with` block raises an
-   :exc:`ExceptionGroup`, suppressed exceptions are removed from the
+   If the code within the :keyword:`!with` block raises a
+   :exc:`BaseExceptionGroup`, suppressed exceptions are removed from the
    group.  If any exceptions in the group are not suppressed, a group containing them is re-raised.
 
    .. versionadded:: 3.4
 
    .. versionchanged:: 3.12
       ``suppress`` now supports suppressing exceptions raised as
-      part of an :exc:`ExceptionGroup`.
+      part of an :exc:`BaseExceptionGroup`.
 
 .. function:: redirect_stdout(new_target)
 
index 2efed2d9ec47425605fbb4639cc848566441d465..b831d8916ca6cf05546999fceb6d2453c423d722 100644 (file)
@@ -457,7 +457,7 @@ class suppress(AbstractContextManager):
             return
         if issubclass(exctype, self._exceptions):
             return True
-        if issubclass(exctype, ExceptionGroup):
+        if issubclass(exctype, BaseExceptionGroup):
             match, rest = excinst.split(self._exceptions)
             if rest is None:
                 return True
index 3cc194417f257eda2a34babc1a7d236cd29c3dad..a50a4ed7ee4912032beb651a93156fc591566610 100644 (file)
@@ -1287,6 +1287,24 @@ class TestSuppress(ExceptionIsLikeMixin, unittest.TestCase):
                 [KeyError("ke1"), KeyError("ke2")],
             ),
         )
+        # Check handling of BaseExceptionGroup, using GeneratorExit so that
+        # we don't accidentally discard a ctrl-c with KeyboardInterrupt.
+        with suppress(GeneratorExit):
+            raise BaseExceptionGroup("message", [GeneratorExit()])
+        # If we raise a BaseException group, we can still suppress parts
+        with self.assertRaises(BaseExceptionGroup) as eg1:
+            with suppress(KeyError):
+                raise BaseExceptionGroup("message", [GeneratorExit("g"), KeyError("k")])
+        self.assertExceptionIsLike(
+            eg1.exception, BaseExceptionGroup("message", [GeneratorExit("g")]),
+        )
+        # If we suppress all the leaf BaseExceptions, we get a non-base ExceptionGroup
+        with self.assertRaises(ExceptionGroup) as eg1:
+            with suppress(GeneratorExit):
+                raise BaseExceptionGroup("message", [GeneratorExit("g"), KeyError("k")])
+        self.assertExceptionIsLike(
+            eg1.exception, ExceptionGroup("message", [KeyError("k")]),
+        )
 
 
 class TestChdir(unittest.TestCase):
diff --git a/Misc/NEWS.d/next/Library/2023-11-09-10-45-56.gh-issue-103791.sdfkja.rst b/Misc/NEWS.d/next/Library/2023-11-09-10-45-56.gh-issue-103791.sdfkja.rst
new file mode 100644 (file)
index 0000000..5bfdd75
--- /dev/null
@@ -0,0 +1,3 @@
+:class:`contextlib.suppress` now supports suppressing exceptions raised as
+part of a :exc:`BaseExceptionGroup`, in addition to the recent support for
+:exc:`ExceptionGroup`.