Skip to content

Commit a2535e0

Browse files
Zac-HDaisk
authored andcommitted
pythongh-103791: handle BaseExceptionGroup in contextlib.suppress() (python#111910)
1 parent fc28e6b commit a2535e0

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

Doc/library/contextlib.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,15 @@ 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
312312

313313
.. versionchanged:: 3.12
314314
``suppress`` now supports suppressing exceptions raised as
315-
part of an :exc:`ExceptionGroup`.
315+
part of an :exc:`BaseExceptionGroup`.
316316

317317
.. function:: redirect_stdout(new_target)
318318

Lib/contextlib.py

+1-1
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

+18
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,24 @@ 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()])
1304+
# If we raise a BaseException group, we can still suppress parts
1305+
with self.assertRaises(BaseExceptionGroup) as eg1:
1306+
with suppress(KeyError):
1307+
raise BaseExceptionGroup("message", [GeneratorExit("g"), KeyError("k")])
1308+
self.assertExceptionIsLike(
1309+
eg1.exception, BaseExceptionGroup("message", [GeneratorExit("g")]),
1310+
)
1311+
# If we suppress all the leaf BaseExceptions, we get a non-base ExceptionGroup
1312+
with self.assertRaises(ExceptionGroup) as eg1:
1313+
with suppress(GeneratorExit):
1314+
raise BaseExceptionGroup("message", [GeneratorExit("g"), KeyError("k")])
1315+
self.assertExceptionIsLike(
1316+
eg1.exception, ExceptionGroup("message", [KeyError("k")]),
1317+
)
13001318

13011319

13021320
class TestChdir(unittest.TestCase):
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)