diff options
author | Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com> | 2023-11-10 13:32:36 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-10 13:32:36 (GMT) |
commit | d61313bdb1eee3e4bb111e0b248ac2dbb48be917 (patch) | |
tree | c5cff2f6ee3ab3d8350127adfea5834964c11d47 /Lib/test | |
parent | 64fea3211d08082236d05c38ee728f922eb7d8ed (diff) | |
download | cpython-d61313bdb1eee3e4bb111e0b248ac2dbb48be917.zip cpython-d61313bdb1eee3e4bb111e0b248ac2dbb48be917.tar.gz cpython-d61313bdb1eee3e4bb111e0b248ac2dbb48be917.tar.bz2 |
gh-103791: handle `BaseExceptionGroup` in `contextlib.suppress()` (#111910)
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_contextlib.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index e32a091..36c3abc 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -1297,6 +1297,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): |