gh-102799: use sys.exception() instead of sys.exc_info() in tests (#103293)

This commit is contained in:
Irit Katriel 2023-04-06 11:08:25 +01:00 committed by GitHub
parent a44568b80d
commit 482b6eeadc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 68 additions and 71 deletions

View file

@ -234,16 +234,16 @@ class ExceptionTest(unittest.TestCase):
def test_except_throw(self):
def store_raise_exc_generator():
try:
self.assertEqual(sys.exc_info()[0], None)
self.assertIsNone(sys.exception())
yield
except Exception as exc:
# exception raised by gen.throw(exc)
self.assertEqual(sys.exc_info()[0], ValueError)
self.assertIsInstance(sys.exception(), ValueError)
self.assertIsNone(exc.__context__)
yield
# ensure that the exception is not lost
self.assertEqual(sys.exc_info()[0], ValueError)
self.assertIsInstance(sys.exception(), ValueError)
yield
# we should be able to raise back the ValueError
@ -265,11 +265,11 @@ class ExceptionTest(unittest.TestCase):
next(make)
self.assertIsNone(cm.exception.__context__)
self.assertEqual(sys.exc_info(), (None, None, None))
self.assertIsNone(sys.exception())
def test_except_next(self):
def gen():
self.assertEqual(sys.exc_info()[0], ValueError)
self.assertIsInstance(sys.exception(), ValueError)
yield "done"
g = gen()
@ -277,23 +277,23 @@ class ExceptionTest(unittest.TestCase):
raise ValueError
except Exception:
self.assertEqual(next(g), "done")
self.assertEqual(sys.exc_info(), (None, None, None))
self.assertIsNone(sys.exception())
def test_except_gen_except(self):
def gen():
try:
self.assertEqual(sys.exc_info()[0], None)
self.assertIsNone(sys.exception())
yield
# we are called from "except ValueError:", TypeError must
# inherit ValueError in its context
raise TypeError()
except TypeError as exc:
self.assertEqual(sys.exc_info()[0], TypeError)
self.assertIsInstance(sys.exception(), TypeError)
self.assertEqual(type(exc.__context__), ValueError)
# here we are still called from the "except ValueError:"
self.assertEqual(sys.exc_info()[0], ValueError)
self.assertIsInstance(sys.exception(), ValueError)
yield
self.assertIsNone(sys.exc_info()[0])
self.assertIsNone(sys.exception())
yield "done"
g = gen()
@ -304,7 +304,7 @@ class ExceptionTest(unittest.TestCase):
next(g)
self.assertEqual(next(g), "done")
self.assertEqual(sys.exc_info(), (None, None, None))
self.assertIsNone(sys.exception())
def test_nested_gen_except_loop(self):
def gen():
@ -330,19 +330,19 @@ class ExceptionTest(unittest.TestCase):
def gen():
try:
try:
self.assertEqual(sys.exc_info()[0], None)
self.assertIsNone(sys.exception())
yield
except ValueError:
# we are called from "except ValueError:"
self.assertEqual(sys.exc_info()[0], ValueError)
self.assertIsInstance(sys.exception(), ValueError)
raise TypeError()
except Exception as exc:
self.assertEqual(sys.exc_info()[0], TypeError)
self.assertIsInstance(sys.exception(), TypeError)
self.assertEqual(type(exc.__context__), ValueError)
# we are still called from "except ValueError:"
self.assertEqual(sys.exc_info()[0], ValueError)
self.assertIsInstance(sys.exception(), ValueError)
yield
self.assertIsNone(sys.exc_info()[0])
self.assertIsNone(sys.exception())
yield "done"
g = gen()
@ -353,7 +353,7 @@ class ExceptionTest(unittest.TestCase):
g.throw(exc)
self.assertEqual(next(g), "done")
self.assertEqual(sys.exc_info(), (None, None, None))
self.assertIsNone(sys.exception())
def test_except_throw_bad_exception(self):
class E(Exception):