Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inspect.stack() should return list of named tuples #61012

Closed
danielsh mannequin opened this issue Dec 29, 2012 · 11 comments
Closed

inspect.stack() should return list of named tuples #61012

danielsh mannequin opened this issue Dec 29, 2012 · 11 comments
Labels
easy stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@danielsh
Copy link
Mannequin

danielsh mannequin commented Dec 29, 2012

BPO 16808
Nosy @rhettinger, @terryjreedy, @pitrou, @meadori, @PCManticore, @1st1
Files
  • inspect-v1.diff
  • inspect-v2.diff
  • inspect-v3.diff
  • inspect-v5.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2014-08-24.14:55:31.887>
    created_at = <Date 2012-12-29.03:02:29.153>
    labels = ['easy', 'type-feature', 'library']
    title = 'inspect.stack() should return list of named tuples'
    updated_at = <Date 2014-08-24.14:55:31.886>
    user = 'https://bugs.python.org/danielsh'

    bugs.python.org fields:

    activity = <Date 2014-08-24.14:55:31.886>
    actor = 'pitrou'
    assignee = 'none'
    closed = True
    closed_date = <Date 2014-08-24.14:55:31.887>
    closer = 'pitrou'
    components = ['Library (Lib)']
    creation = <Date 2012-12-29.03:02:29.153>
    creator = 'danielsh'
    dependencies = []
    files = ['28475', '28488', '28515', '28572']
    hgrepos = []
    issue_num = 16808
    keywords = ['patch', 'easy']
    message_count = 11.0
    messages = ['178467', '178478', '178485', '178549', '178709', '178713', '179099', '179100', '179106', '225823', '225824']
    nosy_count = 9.0
    nosy_names = ['rhettinger', 'terry.reedy', 'pitrou', 'meador.inge', 'Claudiu.Popa', 'python-dev', 'yselivanov', 'danielsh', 'davet']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue16808'
    versions = ['Python 3.5']

    @danielsh
    Copy link
    Mannequin Author

    danielsh mannequin commented Dec 29, 2012

    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)

    @danielsh danielsh mannequin added the stdlib Python modules in the Lib dir label Dec 29, 2012
    @rhettinger
    Copy link
    Contributor

    +1

    @rhettinger rhettinger added easy type-feature A feature request or enhancement labels Dec 29, 2012
    @danielsh
    Copy link
    Mannequin Author

    danielsh mannequin commented Dec 29, 2012

    Why did you set stage to 'needs patch'? One is already attached.

    @danielsh
    Copy link
    Mannequin Author

    danielsh mannequin commented Dec 30, 2012

    Add versionchanged per review.

    @meadori
    Copy link
    Member

    meadori commented Dec 31, 2012

    This patch looks good to me with the exception that "versionchanged" should be 3.4.

    @meadori meadori self-assigned this Dec 31, 2012
    @danielsh
    Copy link
    Mannequin Author

    danielsh mannequin commented Dec 31, 2012

    Fixed that in v3.

    @terryjreedy
    Copy link
    Member

    Should a test be added to or changed in test_inspect? Line 163 has a test_stack method that calls inspect.stack.

    @meadori
    Copy link
    Member

    meadori commented Jan 5, 2013

    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.

    @danielsh
    Copy link
    Mannequin Author

    danielsh mannequin commented Jan 5, 2013

    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.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Aug 24, 2014

    New changeset d03730abd2f6 by Antoine Pitrou in branch 'default':
    Issue bpo-16808: inspect.stack() now returns a named tuple instead of a tuple.
    http://hg.python.org/cpython/rev/d03730abd2f6

    @pitrou
    Copy link
    Member

    pitrou commented Aug 24, 2014

    It seems like this patch had been overlooked. I refreshed it for 3.5, added a couple tests, and pushed it. Thank you, Daniel!

    @pitrou pitrou closed this as completed Aug 24, 2014
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    easy stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants