From 67cb25cd4f7d400c3b134ac6aabc1f9d0f14034d Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Tue, 25 Jan 2022 10:57:09 +0000 Subject: [PATCH 1/2] bpo-46510: Add missing test for types.TracebackType/FrameType. Calculate them directly from the caught exception. --- Lib/test/test_types.py | 8 ++++++++ Lib/types.py | 8 +++----- .../next/Library/2022-01-25-10-59-41.bpo-46510.PM5svI.rst | 3 +++ 3 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2022-01-25-10-59-41.bpo-46510.PM5svI.rst diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index 3dfda5cb956636..f1be301ef75475 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -624,6 +624,14 @@ def test_notimplemented_type(self): 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(e.__traceback__, types.TracebackType) + self.assertIsInstance(e.__traceback__.tb_frame, types.FrameType) + class UnionTests(unittest.TestCase): diff --git a/Lib/types.py b/Lib/types.py index 679c7f638b3100..9490da7b9ee3b9 100644 --- a/Lib/types.py +++ b/Lib/types.py @@ -52,11 +52,9 @@ def _m(self): pass 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 00000000000000..e13e1ac41cb73c --- /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 :meth:`sys.exc_info`. From 164f48a35fd58fedd967bb6719414ccf5ebe83ad Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Tue, 25 Jan 2022 11:48:24 +0000 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> --- Lib/test/test_types.py | 4 ++-- .../next/Library/2022-01-25-10-59-41.bpo-46510.PM5svI.rst | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index f1be301ef75475..c54854eeb5ad22 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -629,8 +629,8 @@ def test_traceback_and_frame_types(self): raise OSError except OSError as e: exc = e - self.assertIsInstance(e.__traceback__, types.TracebackType) - self.assertIsInstance(e.__traceback__.tb_frame, types.FrameType) + self.assertIsInstance(exc.__traceback__, types.TracebackType) + self.assertIsInstance(exc.__traceback__.tb_frame, types.FrameType) class UnionTests(unittest.TestCase): 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 index e13e1ac41cb73c..b416a1692270b0 100644 --- 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 @@ -1,3 +1,3 @@ Add missing test for :class:`types.TracebackType` and :class:`types.FrameType`. Calculate them directly from the caught exception -without calling :meth:`sys.exc_info`. +without calling :func:`sys.exc_info`.