Skip to content

Commit 5ecdace

Browse files
iritkatrielpython-sidebar
authored andcommitted
pythongh-102008: simplify test_except_star by using sys.exception() instead of sys.exc_info() (python#102009)
1 parent 6f23007 commit 5ecdace

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

Lib/test/test_except_star.py

+19-25
Original file line numberDiff line numberDiff line change
@@ -208,44 +208,38 @@ def assertMetadataNotEqual(self, e1, e2):
208208

209209
class TestExceptStarSplitSemantics(ExceptStarTest):
210210
def doSplitTestNamed(self, exc, T, match_template, rest_template):
211-
initial_exc_info = sys.exc_info()
212-
exc_info = match = rest = None
211+
initial_sys_exception = sys.exception()
212+
sys_exception = match = rest = None
213213
try:
214214
try:
215215
raise exc
216216
except* T as e:
217-
exc_info = sys.exc_info()
217+
sys_exception = sys.exception()
218218
match = e
219219
except BaseException as e:
220220
rest = e
221221

222-
if match_template:
223-
self.assertEqual(exc_info[1], match)
224-
else:
225-
self.assertIsNone(exc_info)
222+
self.assertEqual(sys_exception, match)
226223
self.assertExceptionIsLike(match, match_template)
227224
self.assertExceptionIsLike(rest, rest_template)
228-
self.assertEqual(sys.exc_info(), initial_exc_info)
225+
self.assertEqual(sys.exception(), initial_sys_exception)
229226

230227
def doSplitTestUnnamed(self, exc, T, match_template, rest_template):
231-
initial_exc_info = sys.exc_info()
232-
exc_info = match = rest = None
228+
initial_sys_exception = sys.exception()
229+
sys_exception = match = rest = None
233230
try:
234231
try:
235232
raise exc
236233
except* T:
237-
exc_info = sys.exc_info()
238-
match = sys.exc_info()[1]
234+
sys_exception = match = sys.exception()
239235
else:
240236
if rest_template:
241237
self.fail("Exception not raised")
242238
except BaseException as e:
243239
rest = e
244240
self.assertExceptionIsLike(match, match_template)
245-
if match_template:
246-
self.assertEqual(exc_info[0], type(match_template))
247241
self.assertExceptionIsLike(rest, rest_template)
248-
self.assertEqual(sys.exc_info(), initial_exc_info)
242+
self.assertEqual(sys.exception(), initial_sys_exception)
249243

250244
def doSplitTestInExceptHandler(self, exc, T, match_template, rest_template):
251245
try:
@@ -409,11 +403,11 @@ def test_multiple_matches_unnamed(self):
409403
try:
410404
raise ExceptionGroup("mmu", [OSError("os"), BlockingIOError("io")])
411405
except* BlockingIOError:
412-
e = sys.exc_info()[1]
406+
e = sys.exception()
413407
self.assertExceptionIsLike(e,
414408
ExceptionGroup("mmu", [BlockingIOError("io")]))
415409
except* OSError:
416-
e = sys.exc_info()[1]
410+
e = sys.exception()
417411
self.assertExceptionIsLike(e,
418412
ExceptionGroup("mmu", [OSError("os")]))
419413
else:
@@ -434,7 +428,7 @@ def test_first_match_wins_unnamed(self):
434428
try:
435429
raise ExceptionGroup("fstu", [BlockingIOError("io")])
436430
except* OSError:
437-
e = sys.exc_info()[1]
431+
e = sys.exception()
438432
self.assertExceptionIsLike(e,
439433
ExceptionGroup("fstu", [BlockingIOError("io")]))
440434
except* BlockingIOError:
@@ -452,7 +446,7 @@ def test_nested_except_stars(self):
452446
pass
453447
else:
454448
self.fail("Exception not raised")
455-
e = sys.exc_info()[1]
449+
e = sys.exception()
456450
self.assertExceptionIsLike(e,
457451
ExceptionGroup("n", [BlockingIOError("io")]))
458452
else:
@@ -766,7 +760,7 @@ def test_raise_unnamed(self):
766760
try:
767761
raise orig
768762
except* OSError:
769-
e = sys.exc_info()[1]
763+
e = sys.exception()
770764
raise TypeError(3) from e
771765
except ExceptionGroup as e:
772766
exc = e
@@ -821,7 +815,7 @@ def test_raise_handle_all_raise_one_unnamed(self):
821815
try:
822816
raise orig
823817
except* (TypeError, ValueError) as e:
824-
e = sys.exc_info()[1]
818+
e = sys.exception()
825819
raise SyntaxError(3) from e
826820
except ExceptionGroup as e:
827821
exc = e
@@ -882,10 +876,10 @@ def test_raise_handle_all_raise_two_unnamed(self):
882876
try:
883877
raise orig
884878
except* TypeError:
885-
e = sys.exc_info()[1]
879+
e = sys.exception()
886880
raise SyntaxError(3) from e
887881
except* ValueError:
888-
e = sys.exc_info()[1]
882+
e = sys.exception()
889883
raise SyntaxError(4) from e
890884
except ExceptionGroup as e:
891885
exc = e
@@ -982,7 +976,7 @@ def derive(self, excs):
982976

983977

984978
class TestExceptStarCleanup(ExceptStarTest):
985-
def test_exc_info_restored(self):
979+
def test_sys_exception_restored(self):
986980
try:
987981
try:
988982
raise ValueError(42)
@@ -997,7 +991,7 @@ def test_exc_info_restored(self):
997991

998992
self.assertExceptionIsLike(exc, ZeroDivisionError('division by zero'))
999993
self.assertExceptionIsLike(exc.__context__, ValueError(42))
1000-
self.assertEqual(sys.exc_info(), (None, None, None))
994+
self.assertEqual(sys.exception(), None)
1001995

1002996

1003997
class TestExceptStar_WeirdLeafExceptions(ExceptStarTest):

0 commit comments

Comments
 (0)