[Python-checkins] cpython: Issue #23631: Fix traceback.format_list when a traceback has been mutated.

robert.collins python-checkins at python.org
Mon Mar 16 03:27:32 CET 2015


https://hg.python.org/cpython/rev/ea3cc128ce35
changeset:   95002:ea3cc128ce35
user:        Robert Collins <rbtcollins at hp.com>
date:        Mon Mar 16 15:27:16 2015 +1300
summary:
  Issue #23631: Fix traceback.format_list when a traceback has been mutated.

files:
  Lib/test/test_traceback.py |  10 +++++++++-
  Lib/traceback.py           |  14 ++++++++++----
  Misc/NEWS                  |   2 ++
  3 files changed, 21 insertions(+), 5 deletions(-)


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
@@ -555,6 +555,14 @@
             ['  File "foo.py", line 1, in fred\n    line\n'],
             s.format())
 
+    def test_from_list_edited_stack(self):
+        s = traceback.StackSummary.from_list([('foo.py', 1, 'fred', 'line')])
+        s[0] = ('foo.py', 2, 'fred', 'line')
+        s2 = traceback.StackSummary.from_list(s)
+        self.assertEqual(
+            ['  File "foo.py", line 2, in fred\n    line\n'],
+            s2.format())
+
     def test_format_smoke(self):
         # For detailed tests see the format_list tests, which consume the same
         # code.
@@ -585,7 +593,7 @@
                 traceback.walk_stack(None), capture_locals=True, limit=1)
         s = some_inner(3, 4)
         self.assertEqual(
-            ['  File "' + __file__ + '", line 585, '
+            ['  File "' + __file__ + '", line 593, '
              'in some_inner\n'
              '    traceback.walk_stack(None), capture_locals=True, limit=1)\n'
              '    a = 1\n'
diff --git a/Lib/traceback.py b/Lib/traceback.py
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -348,11 +348,17 @@
         This method supports the older Python API. Each tuple should be a
         4-tuple with (filename, lineno, name, line) elements.
         """
-        if isinstance(a_list, StackSummary):
-            return StackSummary(a_list)
+        # While doing a fast-path check for isinstance(a_list, StackSummary) is
+        # appealing, idlelib.run.cleanup_traceback and other similar code may
+        # break this by making arbitrary frames plain tuples, so we need to
+        # check on a frame by frame basis.
         result = StackSummary()
-        for filename, lineno, name, line in a_list:
-            result.append(FrameSummary(filename, lineno, name, line=line))
+        for frame in a_list:
+            if isinstance(frame, FrameSummary):
+                result.append(frame)
+            else:
+                filename, lineno, name, line = frame
+                result.append(FrameSummary(filename, lineno, name, line=line))
         return result
 
     def format(self):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -18,6 +18,8 @@
 Library
 -------
 
+- Issue #23631: Fix traceback.format_list when a traceback has been mutated.
+
 - Issue #23568: Add rdivmod support to MagicMock() objects.
   Patch by Håkan Lövdahl.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list