From cae3cb83fb0ccc37290a45da30405fc4b9665d4d Mon Sep 17 00:00:00 2001 From: Robert Collins Date: Fri, 21 Nov 2014 10:19:02 +1300 Subject: [PATCH] Fixes for 3.3. Handle frame.clear being absent (and accept it won't be cleared in < 3.4). Don't try to check coding: behaviour which was fixed in 3.4. --- traceback2/__init__.py | 3 ++- traceback2/tests/test_traceback.py | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/traceback2/__init__.py b/traceback2/__init__.py index 8ce2a9d..d08702a 100644 --- a/traceback2/__init__.py +++ b/traceback2/__init__.py @@ -305,11 +305,12 @@ def extract_stack(f=None, limit=None): stack.reverse() return stack +_identity = lambda:None 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() + getattr(tb.tb_frame, 'clear', _identity)() except RuntimeError: # Ignore the exception raised if the frame is still executing. pass diff --git a/traceback2/tests/test_traceback.py b/traceback2/tests/test_traceback.py index 3598d3f..6865861 100644 --- a/traceback2/tests/test_traceback.py +++ b/traceback2/tests/test_traceback.py @@ -191,7 +191,9 @@ class SyntaxTracebackCases(unittest.TestCase): do_test(" \t\f\n# coding: {0}\n".format(charset), text, charset, 5) # Issue #18960: coding spec should has no effect - do_test("0\n# coding: GBK\n", "h\xe9 ho", 'utf-8', 5) + # (Fixed in 3.4) + if sys.version_info[:2] > (3, 3): + do_test("0\n# coding: GBK\n", "h\xe9 ho", 'utf-8', 5) class TracebackFormatTests(unittest.TestCase): @@ -457,5 +459,6 @@ class MiscTracebackCases(unittest.TestCase): # Clear traceback frames traceback.clear_frames(tb) - # Local variable dict should now be empty. - self.assertEqual(len(inner_frame.f_locals), 0) + # Local variable dict should now be empty (on Python 3.4+) + if sys.version_info[:2] > (3, 3): + self.assertEqual({}, inner_frame.f_locals)