This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: inspect.stack() should return list of named tuples
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Claudiu.Popa, danielsh, davet, meador.inge, pitrou, python-dev, rhettinger, terry.reedy, yselivanov
Priority: normal Keywords: easy, patch

Created on 2012-12-29 03:02 by danielsh, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
inspect-v1.diff danielsh, 2012-12-29 03:02 review
inspect-v2.diff danielsh, 2012-12-30 03:04 review
inspect-v3.diff danielsh, 2012-12-31 18:29 review
inspect-v5.diff danielsh, 2013-01-05 04:13 review
Messages (11)
msg178467 - (view) Author: Daniel Shahaf (danielsh) Date: 2012-12-29 03:02
Currently inspect.stack() returns a list of 6-tuples.  I suggest to make it return a list of named tuples, so code that only needs one tuple element can get it by name.

Current behaviour:
% ./python -c 'import inspect; print(inspect.stack()[0])' 
(<frame object at 0x157fb38>, '<string>', 1, '<module>', None, None)

Suggested behaviour:
% ./python -c 'import inspect; print(inspect.stack()[0])'    
FrameInfo(frame=<frame object at 0xddab38>, filename='<string>', lineno=1, function='<module>', code_context=None, index=None)
msg178478 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2012-12-29 07:01
+1
msg178485 - (view) Author: Daniel Shahaf (danielsh) Date: 2012-12-29 12:44
Why did you set stage to 'needs patch'?  One is already attached.
msg178549 - (view) Author: Daniel Shahaf (danielsh) Date: 2012-12-30 03:04
Add versionchanged per review.
msg178709 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2012-12-31 17:14
This patch looks good to me with the exception that "versionchanged" should be 3.4.
msg178713 - (view) Author: Daniel Shahaf (danielsh) Date: 2012-12-31 18:29
Fixed that in v3.
msg179099 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-01-05 01:33
Should a test be added to or changed in test_inspect? Line 163 has a test_stack method that calls inspect.stack.
msg179100 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2013-01-05 02:18
I suppose asserting the type wouldn't hurt, but I don't consider it that important:

--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -164,12 +164,16 @@ class TestInterpreterStack(IsTestBase):
         self.assertTrue(len(mod.st) >= 5)
         self.assertEqual(revise(*mod.st[0][1:]),
              (modfile, 16, 'eggs', ['    st = inspect.stack()\n'], 0))
+        self.assertIsInstance(mod.st[0], inspect.FrameInfo)
         self.assertEqual(revise(*mod.st[1][1:]),
              (modfile, 9, 'spam', ['    eggs(b + d, c + f)\n'], 0))
+        self.assertIsInstance(mod.st[1], inspect.FrameInfo)
         self.assertEqual(revise(*mod.st[2][1:]),
              (modfile, 43, 'argue', ['            spam(a, b, c)\n'], 0))
+        self.assertIsInstance(mod.st[2], inspect.FrameInfo)
         self.assertEqual(revise(*mod.st[3][1:]),
              (modfile, 39, 'abuse', ['        self.argue(a, b, c)\n'], 0))
+        self.assertIsInstance(mod.st[3], inspect.FrameInfo)


TestGetClosureVars builds the named tuples directly and compares them.  For example:

        expected = inspect.ClosureVars(nonlocal_vars, global_vars,
                                       builtin_vars, unbound_names)
        self.assertEqual(inspect.getclosurevars(f(_arg)), expected)

Doing this for FrameInfo is awkward because we don't have a frame object to construct
the named tuple with.
msg179106 - (view) Author: Daniel Shahaf (danielsh) Date: 2013-01-05 04:10
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.
msg225823 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-08-24 14:54
New changeset d03730abd2f6 by Antoine Pitrou in branch 'default':
Issue #16808: inspect.stack() now returns a named tuple instead of a tuple.
http://hg.python.org/cpython/rev/d03730abd2f6
msg225824 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-08-24 14:55
It seems like this patch had been overlooked. I refreshed it for 3.5, added a couple tests, and pushed it. Thank you, Daniel!
History
Date User Action Args
2022-04-11 14:57:39adminsetgithub: 61012
2014-08-24 14:55:31pitrousetstatus: open -> closed

assignee: meador.inge ->

nosy: + pitrou
messages: + msg225824
resolution: fixed
stage: commit review -> resolved
2014-08-24 14:54:33python-devsetnosy: + python-dev
messages: + msg225823
2014-08-22 21:13:16davetsetnosy: + davet
2014-01-29 06:43:54Claudiu.Popasetnosy: + Claudiu.Popa
2014-01-28 23:36:17yselivanovsetnosy: + yselivanov
2014-01-28 23:36:11yselivanovsetversions: + Python 3.5, - Python 3.4
2013-01-05 04:13:49danielshsetfiles: + inspect-v5.diff
2013-01-05 04:13:09danielshsetfiles: - inspect-v4.diff
2013-01-05 04:10:05danielshsetfiles: + inspect-v4.diff

messages: + msg179106
2013-01-05 02:18:44meador.ingesetmessages: + msg179100
2013-01-05 01:33:50terry.reedysetnosy: + terry.reedy
messages: + msg179099
2012-12-31 18:29:22danielshsetfiles: + inspect-v3.diff

messages: + msg178713
2012-12-31 17:14:07meador.ingesetnosy: + meador.inge
messages: + msg178709

assignee: meador.inge
stage: needs patch -> commit review
2012-12-30 03:04:15danielshsetfiles: + inspect-v2.diff
keywords: + patch
messages: + msg178549
2012-12-29 12:44:41danielshsetmessages: + msg178485
2012-12-29 07:01:36rhettingersetnosy: + rhettinger
messages: + msg178478

keywords: + easy, - patch
type: enhancement
stage: needs patch
2012-12-29 03:02:29danielshcreate