summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_traceback.py
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2023-11-28 08:03:25 (GMT)
committerGitHub <noreply@github.com>2023-11-28 08:03:25 (GMT)
commit2c68011780bd68463f5183601ea9c10af368dff6 (patch)
tree88f6ff1359d1825fc6e1f3d8060db856e927ed9c /Lib/test/test_traceback.py
parent2df26d83486b8f9ac6b7df2a9a4669508aa61983 (diff)
downloadcpython-2c68011780bd68463f5183601ea9c10af368dff6.zip
cpython-2c68011780bd68463f5183601ea9c10af368dff6.tar.gz
cpython-2c68011780bd68463f5183601ea9c10af368dff6.tar.bz2
gh-112332: Deprecate TracebackException.exc_type, add exc_type_str. (#112333)
Diffstat (limited to 'Lib/test/test_traceback.py')
-rw-r--r--Lib/test/test_traceback.py51
1 files changed, 43 insertions, 8 deletions
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index b43dca6..c58d979 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -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