summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2022-01-25 18:00:57 (GMT)
committerGitHub <noreply@github.com>2022-01-25 18:00:57 (GMT)
commitec7c17ea236f71c8376abcc2930a7c857d417966 (patch)
tree595522db57eb3ed91c0666fa979b76e4d374b86c
parentd69d3d8b2fec501e51309221fb1fa4622c8a3db3 (diff)
downloadcpython-ec7c17ea236f71c8376abcc2930a7c857d417966.zip
cpython-ec7c17ea236f71c8376abcc2930a7c857d417966.tar.gz
cpython-ec7c17ea236f71c8376abcc2930a7c857d417966.tar.bz2
bpo-46510: Add missing test for types.TracebackType/FrameType. Calculate them directly from the caught exception. (GH-30880)
-rw-r--r--Lib/test/test_types.py8
-rw-r--r--Lib/types.py8
-rw-r--r--Misc/NEWS.d/next/Library/2022-01-25-10-59-41.bpo-46510.PM5svI.rst3
3 files changed, 14 insertions, 5 deletions
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index 3dfda5c..c54854e 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -624,6 +624,14 @@ class TypesTests(unittest.TestCase):
def test_none_type(self):
self.assertIsInstance(None, types.NoneType)
+ def test_traceback_and_frame_types(self):
+ try:
+ raise OSError
+ except OSError as e:
+ exc = e
+ self.assertIsInstance(exc.__traceback__, types.TracebackType)
+ self.assertIsInstance(exc.__traceback__.tb_frame, types.FrameType)
+
class UnionTests(unittest.TestCase):
diff --git a/Lib/types.py b/Lib/types.py
index 679c7f6..9490da7 100644
--- a/Lib/types.py
+++ b/Lib/types.py
@@ -52,11 +52,9 @@ ModuleType = type(sys)
try:
raise TypeError
-except TypeError:
- tb = sys.exc_info()[2]
- TracebackType = type(tb)
- FrameType = type(tb.tb_frame)
- tb = None; del tb
+except TypeError as exc:
+ TracebackType = type(exc.__traceback__)
+ FrameType = type(exc.__traceback__.tb_frame)
# For Jython, the following two types are identical
GetSetDescriptorType = type(FunctionType.__code__)
diff --git a/Misc/NEWS.d/next/Library/2022-01-25-10-59-41.bpo-46510.PM5svI.rst b/Misc/NEWS.d/next/Library/2022-01-25-10-59-41.bpo-46510.PM5svI.rst
new file mode 100644
index 0000000..b416a16
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-01-25-10-59-41.bpo-46510.PM5svI.rst
@@ -0,0 +1,3 @@
+Add missing test for :class:`types.TracebackType` and
+:class:`types.FrameType`. Calculate them directly from the caught exception
+without calling :func:`sys.exc_info`.