diff --git a/Doc/library/traceback.rst b/Doc/library/traceback.rst --- a/Doc/library/traceback.rst +++ b/Doc/library/traceback.rst @@ -129,6 +129,11 @@ A shorthand for ``format_list(extract_stack(f, limit))``. +..function:: clear_tb_frames(tb) + + Clears the local variables of all the stack frames in a traceback by + calling the :meth:`clear` method of each frame object. + .. _traceback-example: diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -388,6 +388,36 @@ return s.getvalue() +class MiscTracebackCases(unittest.TestCase): + # + # Check non-printing functions in traceback module + # + + def test_clear(self): + def outer(): + middle() + def middle(): + inner() + def inner(): + i = 1 + 1/0 + + try: + outer() + except: + type_, value, tb = sys.exc_info() + + # Initial assertion: there's one local in the inner frame. + inner_frame = tb.tb_next.tb_next.tb_next.tb_frame + self.assertEqual(len(inner_frame.f_locals), 1) + + # Clear traceback frames + traceback.clear_tb_frames(tb) + + # Local variable dict should now be empty. + self.assertEqual(len(inner_frame.f_locals), 0) + + def test_main(): run_unittest(__name__) diff --git a/Lib/traceback.py b/Lib/traceback.py --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -7,7 +7,8 @@ __all__ = ['extract_stack', 'extract_tb', 'format_exception', 'format_exception_only', 'format_list', 'format_stack', 'format_tb', 'print_exc', 'format_exc', 'print_exception', - 'print_last', 'print_stack', 'print_tb'] + 'print_last', 'print_stack', 'print_tb', + 'clear_tb_frames'] # # Formatting and printing lists of traceback lines. @@ -299,3 +300,11 @@ stack = list(_extract_stack_iter(_get_stack(f), limit=limit)) stack.reverse() return stack + +def clear_tb_frames(tb): + "Clear all references to local variables in the frames of a traceback." + # Skip the initial frame, which may still be running. + tb = tb.tb_next + while tb is not None: + tb.tb_frame.clear() + tb = tb.tb_next