mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[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 d61313bdb1
)
Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
This commit is contained in:
parent
4b0c875d91
commit
37804149ec
4 changed files with 25 additions and 4 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue