gh-102008: simplify test_except_star by using sys.exception() instead of sys.exc_info() (#102009)
This commit is contained in:
parent
7346a381be
commit
c2b85a95a5
@ -208,44 +208,38 @@ class ExceptStarTest(unittest.TestCase):
|
|||||||
|
|
||||||
class TestExceptStarSplitSemantics(ExceptStarTest):
|
class TestExceptStarSplitSemantics(ExceptStarTest):
|
||||||
def doSplitTestNamed(self, exc, T, match_template, rest_template):
|
def doSplitTestNamed(self, exc, T, match_template, rest_template):
|
||||||
initial_exc_info = sys.exc_info()
|
initial_sys_exception = sys.exception()
|
||||||
exc_info = match = rest = None
|
sys_exception = match = rest = None
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
raise exc
|
raise exc
|
||||||
except* T as e:
|
except* T as e:
|
||||||
exc_info = sys.exc_info()
|
sys_exception = sys.exception()
|
||||||
match = e
|
match = e
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
rest = e
|
rest = e
|
||||||
|
|
||||||
if match_template:
|
self.assertEqual(sys_exception, match)
|
||||||
self.assertEqual(exc_info[1], match)
|
|
||||||
else:
|
|
||||||
self.assertIsNone(exc_info)
|
|
||||||
self.assertExceptionIsLike(match, match_template)
|
self.assertExceptionIsLike(match, match_template)
|
||||||
self.assertExceptionIsLike(rest, rest_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):
|
def doSplitTestUnnamed(self, exc, T, match_template, rest_template):
|
||||||
initial_exc_info = sys.exc_info()
|
initial_sys_exception = sys.exception()
|
||||||
exc_info = match = rest = None
|
sys_exception = match = rest = None
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
raise exc
|
raise exc
|
||||||
except* T:
|
except* T:
|
||||||
exc_info = sys.exc_info()
|
sys_exception = match = sys.exception()
|
||||||
match = sys.exc_info()[1]
|
|
||||||
else:
|
else:
|
||||||
if rest_template:
|
if rest_template:
|
||||||
self.fail("Exception not raised")
|
self.fail("Exception not raised")
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
rest = e
|
rest = e
|
||||||
self.assertExceptionIsLike(match, match_template)
|
self.assertExceptionIsLike(match, match_template)
|
||||||
if match_template:
|
|
||||||
self.assertEqual(exc_info[0], type(match_template))
|
|
||||||
self.assertExceptionIsLike(rest, rest_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):
|
def doSplitTestInExceptHandler(self, exc, T, match_template, rest_template):
|
||||||
try:
|
try:
|
||||||
@ -409,11 +403,11 @@ class TestExceptStarSplitSemantics(ExceptStarTest):
|
|||||||
try:
|
try:
|
||||||
raise ExceptionGroup("mmu", [OSError("os"), BlockingIOError("io")])
|
raise ExceptionGroup("mmu", [OSError("os"), BlockingIOError("io")])
|
||||||
except* BlockingIOError:
|
except* BlockingIOError:
|
||||||
e = sys.exc_info()[1]
|
e = sys.exception()
|
||||||
self.assertExceptionIsLike(e,
|
self.assertExceptionIsLike(e,
|
||||||
ExceptionGroup("mmu", [BlockingIOError("io")]))
|
ExceptionGroup("mmu", [BlockingIOError("io")]))
|
||||||
except* OSError:
|
except* OSError:
|
||||||
e = sys.exc_info()[1]
|
e = sys.exception()
|
||||||
self.assertExceptionIsLike(e,
|
self.assertExceptionIsLike(e,
|
||||||
ExceptionGroup("mmu", [OSError("os")]))
|
ExceptionGroup("mmu", [OSError("os")]))
|
||||||
else:
|
else:
|
||||||
@ -434,7 +428,7 @@ class TestExceptStarSplitSemantics(ExceptStarTest):
|
|||||||
try:
|
try:
|
||||||
raise ExceptionGroup("fstu", [BlockingIOError("io")])
|
raise ExceptionGroup("fstu", [BlockingIOError("io")])
|
||||||
except* OSError:
|
except* OSError:
|
||||||
e = sys.exc_info()[1]
|
e = sys.exception()
|
||||||
self.assertExceptionIsLike(e,
|
self.assertExceptionIsLike(e,
|
||||||
ExceptionGroup("fstu", [BlockingIOError("io")]))
|
ExceptionGroup("fstu", [BlockingIOError("io")]))
|
||||||
except* BlockingIOError:
|
except* BlockingIOError:
|
||||||
@ -452,7 +446,7 @@ class TestExceptStarSplitSemantics(ExceptStarTest):
|
|||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
self.fail("Exception not raised")
|
self.fail("Exception not raised")
|
||||||
e = sys.exc_info()[1]
|
e = sys.exception()
|
||||||
self.assertExceptionIsLike(e,
|
self.assertExceptionIsLike(e,
|
||||||
ExceptionGroup("n", [BlockingIOError("io")]))
|
ExceptionGroup("n", [BlockingIOError("io")]))
|
||||||
else:
|
else:
|
||||||
@ -766,7 +760,7 @@ class TestExceptStarRaiseFrom(ExceptStarTest):
|
|||||||
try:
|
try:
|
||||||
raise orig
|
raise orig
|
||||||
except* OSError:
|
except* OSError:
|
||||||
e = sys.exc_info()[1]
|
e = sys.exception()
|
||||||
raise TypeError(3) from e
|
raise TypeError(3) from e
|
||||||
except ExceptionGroup as e:
|
except ExceptionGroup as e:
|
||||||
exc = e
|
exc = e
|
||||||
@ -821,7 +815,7 @@ class TestExceptStarRaiseFrom(ExceptStarTest):
|
|||||||
try:
|
try:
|
||||||
raise orig
|
raise orig
|
||||||
except* (TypeError, ValueError) as e:
|
except* (TypeError, ValueError) as e:
|
||||||
e = sys.exc_info()[1]
|
e = sys.exception()
|
||||||
raise SyntaxError(3) from e
|
raise SyntaxError(3) from e
|
||||||
except ExceptionGroup as e:
|
except ExceptionGroup as e:
|
||||||
exc = e
|
exc = e
|
||||||
@ -882,10 +876,10 @@ class TestExceptStarRaiseFrom(ExceptStarTest):
|
|||||||
try:
|
try:
|
||||||
raise orig
|
raise orig
|
||||||
except* TypeError:
|
except* TypeError:
|
||||||
e = sys.exc_info()[1]
|
e = sys.exception()
|
||||||
raise SyntaxError(3) from e
|
raise SyntaxError(3) from e
|
||||||
except* ValueError:
|
except* ValueError:
|
||||||
e = sys.exc_info()[1]
|
e = sys.exception()
|
||||||
raise SyntaxError(4) from e
|
raise SyntaxError(4) from e
|
||||||
except ExceptionGroup as e:
|
except ExceptionGroup as e:
|
||||||
exc = e
|
exc = e
|
||||||
@ -982,7 +976,7 @@ class TestExceptStarExceptionGroupSubclass(ExceptStarTest):
|
|||||||
|
|
||||||
|
|
||||||
class TestExceptStarCleanup(ExceptStarTest):
|
class TestExceptStarCleanup(ExceptStarTest):
|
||||||
def test_exc_info_restored(self):
|
def test_sys_exception_restored(self):
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
raise ValueError(42)
|
raise ValueError(42)
|
||||||
@ -997,7 +991,7 @@ class TestExceptStarCleanup(ExceptStarTest):
|
|||||||
|
|
||||||
self.assertExceptionIsLike(exc, ZeroDivisionError('division by zero'))
|
self.assertExceptionIsLike(exc, ZeroDivisionError('division by zero'))
|
||||||
self.assertExceptionIsLike(exc.__context__, ValueError(42))
|
self.assertExceptionIsLike(exc.__context__, ValueError(42))
|
||||||
self.assertEqual(sys.exc_info(), (None, None, None))
|
self.assertEqual(sys.exception(), None)
|
||||||
|
|
||||||
|
|
||||||
class TestExceptStar_WeirdLeafExceptions(ExceptStarTest):
|
class TestExceptStar_WeirdLeafExceptions(ExceptStarTest):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user