Skip to content

Commit d90864e

Browse files
committed
handle BaseExceptionGroup in contextlib.suppress()
1 parent 289af86 commit d90864e

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

Doc/library/contextlib.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ Functions and classes provided:
304304

305305
This context manager is :ref:`reentrant <reentrant-cms>`.
306306

307-
If the code within the :keyword:`!with` block raises an
308-
:exc:`ExceptionGroup`, suppressed exceptions are removed from the
307+
If the code within the :keyword:`!with` block raises a
308+
:exc:`BaseExceptionGroup`, suppressed exceptions are removed from the
309309
group. If any exceptions in the group are not suppressed, a group containing them is re-raised.
310310

311311
.. versionadded:: 3.4
@@ -314,6 +314,10 @@ Functions and classes provided:
314314
``suppress`` now supports suppressing exceptions raised as
315315
part of an :exc:`ExceptionGroup`.
316316

317+
.. versionchanged:: 3.12.1
318+
``suppress`` now supports suppressing exceptions raised as
319+
part of an :exc:`BaseExceptionGroup`.
320+
317321
.. function:: redirect_stdout(new_target)
318322

319323
Context manager for temporarily redirecting :data:`sys.stdout` to

Lib/contextlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ def __exit__(self, exctype, excinst, exctb):
461461
return
462462
if issubclass(exctype, self._exceptions):
463463
return True
464-
if issubclass(exctype, ExceptionGroup):
464+
if issubclass(exctype, BaseExceptionGroup):
465465
match, rest = excinst.split(self._exceptions)
466466
if rest is None:
467467
return True

Lib/test/test_contextlib.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,10 @@ def test_exception_groups(self):
12971297
[KeyError("ke1"), KeyError("ke2")],
12981298
),
12991299
)
1300+
# Check handling of BaseExceptionGroup, using GeneratorExit so that
1301+
# we don't accidentally discard a ctrl-c with KeyboardInterrupt.
1302+
with suppress(GeneratorExit):
1303+
raise BaseExceptionGroup("message", [GeneratorExit()])
13001304

13011305

13021306
class TestChdir(unittest.TestCase):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:class:`contextlib.suppress` now supports suppressing exceptions raised as
2+
part of a :exc:`BaseExceptionGroup`, in addition to the recent support for
3+
:exc:`ExceptionGroup`.

0 commit comments

Comments
 (0)