[issue16808] inspect.stack() should return list of named tuples

Daniel Shahaf report at bugs.python.org
Sat Jan 5 05:10:05 CET 2013


Daniel Shahaf added the comment:

Terry J. Reedy wrote on Sat, Jan 05, 2013 at 01:33:50 +0000:
> Should a test be added to or changed in test_inspect? Line 163 has
> a test_stack method that calls inspect.stack.

Makes sense; added a test that tests named attribute access.  Thanks for
the pointer.

----------
Added file: http://bugs.python.org/file28571/inspect-v4.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16808>
_______________________________________
-------------- next part --------------
diff -r 4b42d7f288c5 Doc/library/inspect.rst
--- a/Doc/library/inspect.rst	Thu Jan 03 09:22:41 2013 +0100
+++ b/Doc/library/inspect.rst	Sat Jan 05 04:06:25 2013 +0000
@@ -800,11 +800,17 @@ Classes and functions
 The interpreter stack
 ---------------------
 
-When the following functions return "frame records," each record is a tuple of
-six items: the frame object, the filename, the line number of the current line,
+When the following functions return "frame records," each record is a
+:term:`named tuple`
+``FrameInfo(frame, filename, lineno, function, code_context, index)``.
+The tuple contains the frame object, the filename, the line number of the
+current line,
 the function name, a list of lines of context from the source code, and the
 index of the current line within that list.
 
+.. versionchanged:: 3.3
+   Return a named tuple instead of a tuple.
+
 .. note::
 
    Keeping references to frame objects, as found in the first element of the frame
diff -r 4b42d7f288c5 Lib/inspect.py
--- a/Lib/inspect.py	Thu Jan 03 09:22:41 2013 +0100
+++ b/Lib/inspect.py	Sat Jan 05 04:06:25 2013 +0000
@@ -1139,6 +1139,8 @@ def getlineno(frame):
     # FrameType.f_lineno is now a descriptor that grovels co_lnotab
     return frame.f_lineno
 
+FrameInfo = namedtuple('FrameInfo', ('frame',) + Traceback._fields)
+
 def getouterframes(frame, context=1):
     """Get a list of records for a frame and all higher (calling) frames.
 
@@ -1146,7 +1148,8 @@ def getouterframes(frame, context=1):
     name, a list of lines of context, and index within the context."""
     framelist = []
     while frame:
-        framelist.append((frame,) + getframeinfo(frame, context))
+        frameinfo = (frame,) + getframeinfo(frame, context)
+        framelist.append(FrameInfo(*frameinfo))
         frame = frame.f_back
     return framelist
 
@@ -1157,7 +1160,8 @@ def getinnerframes(tb, context=1):
     name, a list of lines of context, and index within the context."""
     framelist = []
     while tb:
-        framelist.append((tb.tb_frame,) + getframeinfo(tb, context))
+        frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
+        framelist.append(FrameInfo(*frameinfo))
         tb = tb.tb_next
     return framelist
 
diff -r 4b42d7f288c5 Lib/test/test_inspect.py
--- a/Lib/test/test_inspect.py	Thu Jan 03 09:22:41 2013 +0100
+++ b/Lib/test/test_inspect.py	Sat Jan 05 04:06:25 2013 +0000
@@ -170,6 +170,8 @@ class TestInterpreterStack(IsTestBase):
              (modfile, 43, 'argue', ['            spam(a, b, c)\n'], 0))
         self.assertEqual(revise(*mod.st[3][1:]),
              (modfile, 39, 'abuse', ['        self.argue(a, b, c)\n'], 0))
+        self.assertEqual(mod.st[0].frame, mod.fr)
+        self.assertEqual(mod.st[0].lineno, 16)
 
     def test_trace(self):
         self.assertEqual(len(git.tr), 3)


More information about the Python-bugs-list mailing list