[Python-checkins] gh-102008: simplify test_except_star by using sys.exception() instead of sys.exc_info() (GH-102009)

miss-islington webhook-mailer at python.org
Tue Feb 21 10:38:41 EST 2023


https://github.com/python/cpython/commit/3bfa608cbe0d740be7628f8155b73d6f02eec797
commit: 3bfa608cbe0d740be7628f8155b73d6f02eec797
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2023-02-21T07:38:27-08:00
summary:

gh-102008: simplify test_except_star by using sys.exception() instead of sys.exc_info() (GH-102009)

(cherry picked from commit c2b85a95a50687a2e5d1873e17266370876e77e9)

Co-authored-by: Irit Katriel <1055913+iritkatriel at users.noreply.github.com>

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 9de72dbd5a32..c5167c5bba38 100644
--- a/Lib/test/test_except_star.py
+++ b/Lib/test/test_except_star.py
@@ -208,44 +208,38 @@ def assertMetadataNotEqual(self, e1, e2):
 
 class TestExceptStarSplitSemantics(ExceptStarTest):
     def doSplitTestNamed(self, exc, T, match_template, rest_template):
-        initial_exc_info = sys.exc_info()
-        exc_info = match = rest = None
+        initial_sys_exception = sys.exception()
+        sys_exception = match = rest = None
         try:
             try:
                 raise exc
             except* T as e:
-                exc_info = sys.exc_info()
+                sys_exception = sys.exception()
                 match = e
         except BaseException as e:
             rest = e
 
-        if match_template:
-            self.assertEqual(exc_info[1], match)
-        else:
-            self.assertIsNone(exc_info)
+        self.assertEqual(sys_exception, match)
         self.assertExceptionIsLike(match, match_template)
         self.assertExceptionIsLike(rest, rest_template)
-        self.assertEqual(sys.exc_info(), initial_exc_info)
+        self.assertEqual(sys.exception(), initial_sys_exception)
 
     def doSplitTestUnnamed(self, exc, T, match_template, rest_template):
-        initial_exc_info = sys.exc_info()
-        exc_info = match = rest = None
+        initial_sys_exception = sys.exception()
+        sys_exception = match = rest = None
         try:
             try:
                 raise exc
             except* T:
-                exc_info = sys.exc_info()
-                match = sys.exc_info()[1]
+                sys_exception = match = sys.exception()
             else:
                 if rest_template:
                     self.fail("Exception not raised")
         except BaseException as e:
             rest = e
         self.assertExceptionIsLike(match, match_template)
-        if match_template:
-            self.assertEqual(exc_info[0], type(match_template))
         self.assertExceptionIsLike(rest, rest_template)
-        self.assertEqual(sys.exc_info(), initial_exc_info)
+        self.assertEqual(sys.exception(), initial_sys_exception)
 
     def doSplitTestInExceptHandler(self, exc, T, match_template, rest_template):
         try:
@@ -409,11 +403,11 @@ def test_multiple_matches_unnamed(self):
         try:
             raise ExceptionGroup("mmu", [OSError("os"), BlockingIOError("io")])
         except* BlockingIOError:
-            e = sys.exc_info()[1]
+            e = sys.exception()
             self.assertExceptionIsLike(e,
                 ExceptionGroup("mmu", [BlockingIOError("io")]))
         except* OSError:
-            e = sys.exc_info()[1]
+            e = sys.exception()
             self.assertExceptionIsLike(e,
                 ExceptionGroup("mmu", [OSError("os")]))
         else:
@@ -434,7 +428,7 @@ def test_first_match_wins_unnamed(self):
         try:
             raise ExceptionGroup("fstu", [BlockingIOError("io")])
         except* OSError:
-            e = sys.exc_info()[1]
+            e = sys.exception()
             self.assertExceptionIsLike(e,
                 ExceptionGroup("fstu", [BlockingIOError("io")]))
         except* BlockingIOError:
@@ -452,7 +446,7 @@ def test_nested_except_stars(self):
                 pass
             else:
                 self.fail("Exception not raised")
-            e = sys.exc_info()[1]
+            e = sys.exception()
             self.assertExceptionIsLike(e,
                  ExceptionGroup("n", [BlockingIOError("io")]))
         else:
@@ -766,7 +760,7 @@ def test_raise_unnamed(self):
             try:
                 raise orig
             except* OSError:
-                e = sys.exc_info()[1]
+                e = sys.exception()
                 raise TypeError(3) from e
         except ExceptionGroup as e:
             exc = e
@@ -821,7 +815,7 @@ def test_raise_handle_all_raise_one_unnamed(self):
             try:
                 raise orig
             except* (TypeError, ValueError) as e:
-                e = sys.exc_info()[1]
+                e = sys.exception()
                 raise SyntaxError(3) from e
         except ExceptionGroup as e:
             exc = e
@@ -882,10 +876,10 @@ def test_raise_handle_all_raise_two_unnamed(self):
             try:
                 raise orig
             except* TypeError:
-                e = sys.exc_info()[1]
+                e = sys.exception()
                 raise SyntaxError(3) from e
             except* ValueError:
-                e = sys.exc_info()[1]
+                e = sys.exception()
                 raise SyntaxError(4) from e
         except ExceptionGroup as e:
             exc = e
@@ -982,7 +976,7 @@ def derive(self, excs):
 
 
 class TestExceptStarCleanup(ExceptStarTest):
-    def test_exc_info_restored(self):
+    def test_sys_exception_restored(self):
         try:
             try:
                 raise ValueError(42)
@@ -997,7 +991,7 @@ def test_exc_info_restored(self):
 
         self.assertExceptionIsLike(exc, ZeroDivisionError('division by zero'))
         self.assertExceptionIsLike(exc.__context__, ValueError(42))
-        self.assertEqual(sys.exc_info(), (None, None, None))
+        self.assertEqual(sys.exception(), None)
 
 
 class TestExceptStar_WeirdLeafExceptions(ExceptStarTest):



More information about the Python-checkins mailing list