Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
handle BaseExceptionGroup in contextlib.suppress()
  • Loading branch information
Zac-HD committed Nov 10, 2023
commit d90864e06afefd1dc10fe9f3ba7a1d8e9f821505
8 changes: 6 additions & 2 deletions Doc/library/contextlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ 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
Expand All @@ -314,6 +314,10 @@ Functions and classes provided:
``suppress`` now supports suppressing exceptions raised as
part of an :exc:`ExceptionGroup`.

.. versionchanged:: 3.12.1
Comment thread
Zac-HD marked this conversation as resolved.
Outdated
``suppress`` now supports suppressing exceptions raised as
part of an :exc:`BaseExceptionGroup`.
Comment thread
Zac-HD marked this conversation as resolved.

.. function:: redirect_stdout(new_target)

Context manager for temporarily redirecting :data:`sys.stdout` to
Expand Down
2 changes: 1 addition & 1 deletion Lib/contextlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def __exit__(self, exctype, excinst, exctb):
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
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_contextlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,10 @@ def test_exception_groups(self):
[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()])
Comment thread
Zac-HD marked this conversation as resolved.


class TestChdir(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -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`.