summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_contextlib.py
diff options
context:
space:
mode:
authorƁukasz Langa <lukasz@langa.pl>2023-04-24 22:17:02 (GMT)
committerGitHub <noreply@github.com>2023-04-24 22:17:02 (GMT)
commit22bed58e531ce780d91f3364c5ace98fad28c2e8 (patch)
tree09719c749f02573ed8f47cb5f682651596f008e8 /Lib/test/test_contextlib.py
parent19e4f757de8c7cf2b4b9b4cbb32e376d0e50d2d4 (diff)
downloadcpython-22bed58e531ce780d91f3364c5ace98fad28c2e8.zip
cpython-22bed58e531ce780d91f3364c5ace98fad28c2e8.tar.gz
cpython-22bed58e531ce780d91f3364c5ace98fad28c2e8.tar.bz2
gh-103791: Make contextlib.suppress also act on exceptions within an ExceptionGroup (#103792)
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Diffstat (limited to 'Lib/test/test_contextlib.py')
-rw-r--r--Lib/test/test_contextlib.py27
1 files changed, 26 insertions, 1 deletions
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py
index ec06785..0f8351a 100644
--- a/Lib/test/test_contextlib.py
+++ b/Lib/test/test_contextlib.py
@@ -10,6 +10,7 @@ import unittest
from contextlib import * # Tests __all__
from test import support
from test.support import os_helper
+from test.support.testcase import ExceptionIsLikeMixin
import weakref
@@ -1148,7 +1149,7 @@ class TestRedirectStderr(TestRedirectStream, unittest.TestCase):
orig_stream = "stderr"
-class TestSuppress(unittest.TestCase):
+class TestSuppress(ExceptionIsLikeMixin, unittest.TestCase):
@support.requires_docstrings
def test_instance_docs(self):
@@ -1202,6 +1203,30 @@ class TestSuppress(unittest.TestCase):
1/0
self.assertTrue(outer_continued)
+ def test_exception_groups(self):
+ eg_ve = lambda: ExceptionGroup(
+ "EG with ValueErrors only",
+ [ValueError("ve1"), ValueError("ve2"), ValueError("ve3")],
+ )
+ eg_all = lambda: ExceptionGroup(
+ "EG with many types of exceptions",
+ [ValueError("ve1"), KeyError("ke1"), ValueError("ve2"), KeyError("ke2")],
+ )
+ with suppress(ValueError):
+ raise eg_ve()
+ with suppress(ValueError, KeyError):
+ raise eg_all()
+ with self.assertRaises(ExceptionGroup) as eg1:
+ with suppress(ValueError):
+ raise eg_all()
+ self.assertExceptionIsLike(
+ eg1.exception,
+ ExceptionGroup(
+ "EG with many types of exceptions",
+ [KeyError("ke1"), KeyError("ke2")],
+ ),
+ )
+
class TestChdir(unittest.TestCase):
def make_relative_path(self, *parts):