https://github.com/python/cpython/commit/f4043ab1be0205d834a8b9aa72e289be12637344
commit: f4043ab1be0205d834a8b9aa72e289be12637344
branch: main
author: Alex Grönholm <[email protected]>
committer: StanFromIreland <[email protected]>
date: 2026-10-26T15:18:01Z
summary:
gh-160659: Add tests for `except*` handlers that raise a `BaseException`
(#160660)
files:
M Lib/test/test_except_star.py
diff --git a/Lib/test/test_except_star.py b/Lib/test/test_except_star.py
index 47006c6d3a0c36..9dd28b592aeeea 100644
--- a/Lib/test/test_except_star.py
+++ b/Lib/test/test_except_star.py
@@ -705,6 +705,88 @@ def test_raise_handle_all_raise_two_unnamed(self):
self.assertMetadataEqual(orig, exc.exceptions[0].__context__)
self.assertMetadataEqual(orig, exc.exceptions[1].__context__)
+ def test_raise_base_exception(self):
+ class MyBaseException(BaseException):
+ pass
+
+ orig = ExceptionGroup("eg", [ValueError(1), OSError(2)])
+ try:
+ try:
+ raise orig
+ except* OSError:
+ raise MyBaseException(3)
+ except BaseException as e:
+ exc = e
+
+ self.assertIs(type(exc), BaseExceptionGroup)
+ self.assertEqual(exc.message, "")
+ self.assertEqual(len(exc.exceptions), 2)
+ self.assertExceptionIsLike(exc.exceptions[0], MyBaseException(3))
+ self.assertExceptionIsLike(
+ exc.exceptions[1], ExceptionGroup("eg", [ValueError(1)]))
+
+ self.assertExceptionIsLike(
+ exc.exceptions[0].__context__,
+ ExceptionGroup("eg", [OSError(2)]))
+
+ self.assertMetadataNotEqual(orig, exc)
+ self.assertMetadataEqual(orig, exc.exceptions[0].__context__)
+
+ def test_raise_handle_all_raise_one_base_exception(self):
+ class MyBaseException(BaseException):
+ pass
+
+ orig = ExceptionGroup("eg", [TypeError(1), ValueError(2)])
+ try:
+ try:
+ raise orig
+ except* (TypeError, ValueError):
+ raise MyBaseException(3)
+ except BaseException as e:
+ exc = e
+
+ self.assertExceptionIsLike(exc, MyBaseException(3))
+
+ self.assertExceptionIsLike(
+ exc.__context__,
+ ExceptionGroup("eg", [TypeError(1), ValueError(2)]))
+
+ self.assertMetadataNotEqual(orig, exc)
+ self.assertMetadataEqual(orig, exc.__context__)
+
+ def test_raise_handle_all_raise_base_exception_and_exception(self):
+ class MyBaseException(BaseException):
+ pass
+
+ orig = ExceptionGroup("eg", [TypeError(1), ValueError(2)])
+ try:
+ try:
+ raise orig
+ except* TypeError:
+ raise MyBaseException(3)
+ except* ValueError:
+ raise SyntaxError(4)
+ except BaseException as e:
+ exc = e
+
+ self.assertIs(type(exc), BaseExceptionGroup)
+ self.assertEqual(exc.message, "")
+ self.assertEqual(len(exc.exceptions), 2)
+ self.assertExceptionIsLike(exc.exceptions[0], MyBaseException(3))
+ self.assertExceptionIsLike(exc.exceptions[1], SyntaxError(4))
+
+ self.assertExceptionIsLike(
+ exc.exceptions[0].__context__,
+ ExceptionGroup("eg", [TypeError(1)]))
+
+ self.assertExceptionIsLike(
+ exc.exceptions[1].__context__,
+ ExceptionGroup("eg", [ValueError(2)]))
+
+ self.assertMetadataNotEqual(orig, exc)
+ self.assertMetadataEqual(orig, exc.exceptions[0].__context__)
+ self.assertMetadataEqual(orig, exc.exceptions[1].__context__)
+
class TestExceptStarRaiseFrom(ExceptStarTest):
def test_raise_named(self):
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]