summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_capi/test_exceptions.py
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2023-05-30 15:50:23 (GMT)
committerGitHub <noreply@github.com>2023-05-30 15:50:23 (GMT)
commitb45df737d43e809b1c41b9b2056ecb28c3cfb288 (patch)
tree70cc61338652bffc07f89a304d0e2c1c045e7d87 /Lib/test/test_capi/test_exceptions.py
parent7899fac3c5fba3b8258cdd72562230c51164d778 (diff)
downloadcpython-b45df737d43e809b1c41b9b2056ecb28c3cfb288.zip
cpython-b45df737d43e809b1c41b9b2056ecb28c3cfb288.tar.gz
cpython-b45df737d43e809b1c41b9b2056ecb28c3cfb288.tar.bz2
[3.12] gh-105071: add PyUnstable_Exc_PrepReraiseStar to expose except* implementation in the unstable API (GH-105072) (#105095)
(cherry picked from commit b7aadb4583b040ddc8564896b91f4e5e571c82d6)
Diffstat (limited to 'Lib/test/test_capi/test_exceptions.py')
-rw-r--r--Lib/test/test_capi/test_exceptions.py93
1 files changed, 93 insertions, 0 deletions
diff --git a/Lib/test/test_capi/test_exceptions.py b/Lib/test/test_capi/test_exceptions.py
index 1081f40..118b575 100644
--- a/Lib/test/test_capi/test_exceptions.py
+++ b/Lib/test/test_capi/test_exceptions.py
@@ -5,6 +5,7 @@ import unittest
from test import support
from test.support import import_helper
from test.support.script_helper import assert_python_failure
+from test.support.testcase import ExceptionIsLikeMixin
from .test_misc import decode_stderr
@@ -189,5 +190,97 @@ class Test_ErrSetAndRestore(unittest.TestCase):
'Normalization failed: type=Broken args=<unknown>')
+class Test_PyUnstable_Exc_PrepReraiseStar(ExceptionIsLikeMixin, unittest.TestCase):
+
+ def setUp(self):
+ super().setUp()
+ try:
+ raise ExceptionGroup("eg", [TypeError('bad type'), ValueError(42)])
+ except ExceptionGroup as e:
+ self.orig = e
+
+ def test_invalid_args(self):
+ with self.assertRaisesRegex(TypeError, "orig must be an exception"):
+ _testcapi.unstable_exc_prep_reraise_star(42, [None])
+
+ with self.assertRaisesRegex(TypeError, "excs must be a list"):
+ _testcapi.unstable_exc_prep_reraise_star(self.orig, 42)
+
+ with self.assertRaisesRegex(TypeError, "not an exception"):
+ _testcapi.unstable_exc_prep_reraise_star(self.orig, [TypeError(42), 42])
+
+ with self.assertRaisesRegex(ValueError, "orig must be a raised exception"):
+ _testcapi.unstable_exc_prep_reraise_star(ValueError(42), [TypeError(42)])
+
+ with self.assertRaisesRegex(ValueError, "orig must be a raised exception"):
+ _testcapi.unstable_exc_prep_reraise_star(ExceptionGroup("eg", [ValueError(42)]),
+ [TypeError(42)])
+
+
+ def test_nothing_to_reraise(self):
+ self.assertEqual(
+ _testcapi.unstable_exc_prep_reraise_star(self.orig, [None]), None)
+
+ try:
+ raise ValueError(42)
+ except ValueError as e:
+ orig = e
+ self.assertEqual(
+ _testcapi.unstable_exc_prep_reraise_star(orig, [None]), None)
+
+ def test_reraise_orig(self):
+ orig = self.orig
+ res = _testcapi.unstable_exc_prep_reraise_star(orig, [orig])
+ self.assertExceptionIsLike(res, orig)
+
+ def test_raise_orig_parts(self):
+ orig = self.orig
+ match, rest = orig.split(TypeError)
+
+ test_cases = [
+ ([match, rest], orig),
+ ([rest, match], orig),
+ ([match], match),
+ ([rest], rest),
+ ([], None),
+ ]
+
+ for input, expected in test_cases:
+ with self.subTest(input=input):
+ res = _testcapi.unstable_exc_prep_reraise_star(orig, input)
+ self.assertExceptionIsLike(res, expected)
+
+
+ def test_raise_with_new_exceptions(self):
+ orig = self.orig
+
+ match, rest = orig.split(TypeError)
+ new1 = OSError('bad file')
+ new2 = RuntimeError('bad runtime')
+
+ test_cases = [
+ ([new1, match, rest], ExceptionGroup("", [new1, orig])),
+ ([match, new1, rest], ExceptionGroup("", [new1, orig])),
+ ([match, rest, new1], ExceptionGroup("", [new1, orig])),
+
+ ([new1, new2, match, rest], ExceptionGroup("", [new1, new2, orig])),
+ ([new1, match, new2, rest], ExceptionGroup("", [new1, new2, orig])),
+ ([new2, rest, match, new1], ExceptionGroup("", [new2, new1, orig])),
+ ([rest, new2, match, new1], ExceptionGroup("", [new2, new1, orig])),
+
+
+ ([new1, new2, rest], ExceptionGroup("", [new1, new2, rest])),
+ ([new1, match, new2], ExceptionGroup("", [new1, new2, match])),
+ ([rest, new2, new1], ExceptionGroup("", [new2, new1, rest])),
+ ([new1, new2], ExceptionGroup("", [new1, new2])),
+ ([new2, new1], ExceptionGroup("", [new2, new1])),
+ ]
+
+ for (input, expected) in test_cases:
+ with self.subTest(input=input):
+ res = _testcapi.unstable_exc_prep_reraise_star(orig, input)
+ self.assertExceptionIsLike(res, expected)
+
+
if __name__ == "__main__":
unittest.main()