diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_traceback.py | 28 | ||||
-rw-r--r-- | Lib/traceback.py | 3 |
2 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 5d48e9d..d1967aa 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -1514,6 +1514,34 @@ class TestStack(unittest.TestCase): s.format(), [f'{__file__}:{some_inner.__code__.co_firstlineno + 1}']) + def test_dropping_frames(self): + def f(): + 1/0 + + def g(): + try: + f() + except: + return sys.exc_info() + + exc_info = g() + + class Skip_G(traceback.StackSummary): + def format_frame(self, frame): + if frame.name == 'g': + return None + return super().format_frame(frame) + + stack = Skip_G.extract( + traceback.walk_tb(exc_info[2])).format() + + self.assertEqual(len(stack), 1) + lno = f.__code__.co_firstlineno + 1 + self.assertEqual( + stack[0], + f' File "{__file__}", line {lno}, in f\n 1/0\n' + ) + class TestTracebackException(unittest.TestCase): diff --git a/Lib/traceback.py b/Lib/traceback.py index 4ad8c9a..8d83fd9 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -515,6 +515,9 @@ class StackSummary(list): last_name = None count = 0 for frame in self: + formatted_frame = self.format_frame(frame) + if formatted_frame is None: + continue if (last_file is None or last_file != frame.filename or last_line is None or last_line != frame.lineno or last_name is None or last_name != frame.name): |