gh-112332: Deprecate TracebackException.exc_type, add exc_type_str. (#112333)

This commit is contained in:
Irit Katriel 2023-11-28 08:03:25 +00:00 committed by GitHub
parent 2df26d8348
commit 2c68011780
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 102 additions and 20 deletions

View file

@ -2715,9 +2715,9 @@ class Unrepresentable:
class TestTracebackException(unittest.TestCase):
def test_smoke(self):
def do_test_smoke(self, exc, expected_type_str):
try:
1/0
raise exc
except Exception as e:
exc_obj = e
exc = traceback.TracebackException.from_exception(e)
@ -2727,9 +2727,23 @@ class TestTracebackException(unittest.TestCase):
self.assertEqual(None, exc.__context__)
self.assertEqual(False, exc.__suppress_context__)
self.assertEqual(expected_stack, exc.stack)
self.assertEqual(type(exc_obj), exc.exc_type)
with self.assertWarns(DeprecationWarning):
self.assertEqual(type(exc_obj), exc.exc_type)
self.assertEqual(expected_type_str, exc.exc_type_str)
self.assertEqual(str(exc_obj), str(exc))
def test_smoke_builtin(self):
self.do_test_smoke(ValueError(42), 'ValueError')
def test_smoke_user_exception(self):
class MyException(Exception):
pass
self.do_test_smoke(
MyException('bad things happened'),
('test.test_traceback.TestTracebackException.'
'test_smoke_user_exception.<locals>.MyException'))
def test_from_exception(self):
# Check all the parameters are accepted.
def foo():
@ -2750,7 +2764,9 @@ class TestTracebackException(unittest.TestCase):
self.assertEqual(None, exc.__context__)
self.assertEqual(False, exc.__suppress_context__)
self.assertEqual(expected_stack, exc.stack)
self.assertEqual(type(exc_obj), exc.exc_type)
with self.assertWarns(DeprecationWarning):
self.assertEqual(type(exc_obj), exc.exc_type)
self.assertEqual(type(exc_obj).__name__, exc.exc_type_str)
self.assertEqual(str(exc_obj), str(exc))
def test_cause(self):
@ -2772,7 +2788,9 @@ class TestTracebackException(unittest.TestCase):
self.assertEqual(exc_context, exc.__context__)
self.assertEqual(True, exc.__suppress_context__)
self.assertEqual(expected_stack, exc.stack)
self.assertEqual(type(exc_obj), exc.exc_type)
with self.assertWarns(DeprecationWarning):
self.assertEqual(type(exc_obj), exc.exc_type)
self.assertEqual(type(exc_obj).__name__, exc.exc_type_str)
self.assertEqual(str(exc_obj), str(exc))
def test_context(self):
@ -2792,7 +2810,9 @@ class TestTracebackException(unittest.TestCase):
self.assertEqual(exc_context, exc.__context__)
self.assertEqual(False, exc.__suppress_context__)
self.assertEqual(expected_stack, exc.stack)
self.assertEqual(type(exc_obj), exc.exc_type)
with self.assertWarns(DeprecationWarning):
self.assertEqual(type(exc_obj), exc.exc_type)
self.assertEqual(type(exc_obj).__name__, exc.exc_type_str)
self.assertEqual(str(exc_obj), str(exc))
def test_long_context_chain(self):
@ -2837,7 +2857,9 @@ class TestTracebackException(unittest.TestCase):
self.assertEqual(None, exc.__context__)
self.assertEqual(True, exc.__suppress_context__)
self.assertEqual(expected_stack, exc.stack)
self.assertEqual(type(exc_obj), exc.exc_type)
with self.assertWarns(DeprecationWarning):
self.assertEqual(type(exc_obj), exc.exc_type)
self.assertEqual(type(exc_obj).__name__, exc.exc_type_str)
self.assertEqual(str(exc_obj), str(exc))
def test_compact_no_cause(self):
@ -2857,9 +2879,22 @@ class TestTracebackException(unittest.TestCase):
self.assertEqual(exc_context, exc.__context__)
self.assertEqual(False, exc.__suppress_context__)
self.assertEqual(expected_stack, exc.stack)
self.assertEqual(type(exc_obj), exc.exc_type)
with self.assertWarns(DeprecationWarning):
self.assertEqual(type(exc_obj), exc.exc_type)
self.assertEqual(type(exc_obj).__name__, exc.exc_type_str)
self.assertEqual(str(exc_obj), str(exc))
def test_no_save_exc_type(self):
try:
1/0
except Exception as e:
exc = e
te = traceback.TracebackException.from_exception(
exc, save_exc_type=False)
with self.assertWarns(DeprecationWarning):
self.assertIsNone(te.exc_type)
def test_no_refs_to_exception_and_traceback_objects(self):
try:
1/0