diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2014-08-24 14:50:28 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2014-08-24 14:50:28 (GMT) |
commit | cdcafb78b22ec1cc677abbb2ce292db79279e221 (patch) | |
tree | ccf586cfa575fb1f8c67edb6a3ed7ea757bd4a8a | |
parent | 1fa36268cfdac3760de37859aa3ec1b5121248c5 (diff) | |
download | cpython-cdcafb78b22ec1cc677abbb2ce292db79279e221.zip cpython-cdcafb78b22ec1cc677abbb2ce292db79279e221.tar.gz cpython-cdcafb78b22ec1cc677abbb2ce292db79279e221.tar.bz2 |
Issue #16808: inspect.stack() now returns a named tuple instead of a tuple.
Patch by Daniel Shahaf.
-rw-r--r-- | Doc/library/inspect.rst | 10 | ||||
-rw-r--r-- | Lib/inspect.py | 8 | ||||
-rw-r--r-- | Lib/test/test_inspect.py | 8 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
4 files changed, 25 insertions, 4 deletions
diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst index 359f833..2e209eb 100644 --- a/Doc/library/inspect.rst +++ b/Doc/library/inspect.rst @@ -881,11 +881,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.5 + Return a named tuple instead of a tuple. + .. note:: Keeping references to frame objects, as found in the first element of the frame diff --git a/Lib/inspect.py b/Lib/inspect.py index da1d4b2..4dd9ab1 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1312,6 +1312,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. @@ -1319,7 +1321,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 @@ -1330,7 +1333,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 --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 0452445..d746636 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -182,6 +182,14 @@ 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)) + # Test named tuple fields + record = mod.st[0] + self.assertIs(record.frame, mod.fr) + self.assertEqual(record.lineno, 16) + self.assertEqual(record.filename, mod.__file__) + self.assertEqual(record.function, 'eggs') + self.assertIn('inspect.stack()', record.code_context[0]) + self.assertEqual(record.index, 0) def test_trace(self): self.assertEqual(len(git.tr), 3) @@ -124,6 +124,9 @@ Core and Builtins Library ------- +- Issue #16808: inspect.stack() now returns a named tuple instead of a tuple. + Patch by Daniel Shahaf. + - Issue #22236: Fixed Tkinter images copying operations in NoDefaultRoot mode. - Issue #2527: Add a *globals* argument to timeit functions, in order to |