summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-11-10 14:00:48 (GMT)
committerGitHub <noreply@github.com>2023-11-10 14:00:48 (GMT)
commit37804149ec432af9049d31a740f3422d0c29caa3 (patch)
treeda94f4c1b6d05787e1cf43bfdf69d83ded8b0dbc
parent4b0c875d91727440251a8427a80d8515e39d18cd (diff)
downloadcpython-37804149ec432af9049d31a740f3422d0c29caa3.zip
cpython-37804149ec432af9049d31a740f3422d0c29caa3.tar.gz
cpython-37804149ec432af9049d31a740f3422d0c29caa3.tar.bz2
[3.12] gh-103791: handle `BaseExceptionGroup` in `contextlib.suppress()` (GH-111910) (#111955)
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>
-rw-r--r--Doc/library/contextlib.rst6
-rw-r--r--Lib/contextlib.py2
-rw-r--r--Lib/test/test_contextlib.py18
-rw-r--r--Misc/NEWS.d/next/Library/2023-11-09-10-45-56.gh-issue-103791.sdfkja.rst3
4 files changed, 25 insertions, 4 deletions
diff --git a/Doc/library/contextlib.rst b/Doc/library/contextlib.rst
index 66b9c13..f6ebbfa 100644
--- a/Doc/library/contextlib.rst
+++ b/Doc/library/contextlib.rst
@@ -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)
diff --git a/Lib/contextlib.py b/Lib/contextlib.py
index 2efed2d..b831d89 100644
--- a/Lib/contextlib.py
+++ b/Lib/contextlib.py
@@ -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
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py
index 3cc1944..a50a4ed 100644
--- a/Lib/test/test_contextlib.py
+++ b/Lib/test/test_contextlib.py
@@ -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
index 0000000..5bfdd75
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-11-09-10-45-56.gh-issue-103791.sdfkja.rst
@@ -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`.