diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index b43dca6f640b9a..1e4fec8cf2a718 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -2518,6 +2518,25 @@ def inner(): # Local variable dict should now be empty. self.assertEqual(len(inner_frame.f_locals), 0) + def test_do_not_clear_frame_of_suspended_generator(self): + # See gh-79932 + + def f(): + try: + raise TypeError + except Exception as e: + yield e + yield 42 + + def g(): + yield from f() + + gen = g() + e = next(gen) + self.assertIsInstance(e, TypeError) + traceback.clear_frames(e.__traceback__) + self.assertEqual(next(gen), 42) + def test_extract_stack(self): def extract(): return traceback.extract_stack() diff --git a/Lib/traceback.py b/Lib/traceback.py index b25a7291f6be51..69291eec91faa7 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -251,7 +251,7 @@ def clear_frames(tb): "Clear all references to local variables in the frames of a traceback." while tb is not None: try: - tb.tb_frame.clear() + tb.tb_frame.clear(True) except RuntimeError: # Ignore the exception raised if the frame is still executing. pass