Skip to content

inspect.getargvalues fails if arg name is not bound to a value #61448

Open

Description

@AndrewLutomirski
BPO 17246
Nosy @ezio-melotti, @iritkatriel, @macgors
Files
  • test_cgitb.py: Short script demonstrating the problem
  • issue17246.diff: Proof of concept against 2.7.
  • 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 = None
    created_at = <Date 2013-02-19.22:22:36.701>
    labels = ['easy', 'type-bug', '3.9', '3.10', '3.11', 'library']
    title = 'inspect.getargvalues fails if arg name is not bound to a value'
    updated_at = <Date 2022-03-29.18:46:57.608>
    user = 'https://bugs.python.org/AndrewLutomirski'

    bugs.python.org fields:

    activity = <Date 2022-03-29.18:46:57.608>
    actor = 'macgors'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2013-02-19.22:22:36.701>
    creator = 'Andrew.Lutomirski'
    dependencies = []
    files = ['29125', '29166']
    hgrepos = []
    issue_num = 17246
    keywords = ['easy']
    message_count = 4.0
    messages = ['182446', '182705', '396039', '416283']
    nosy_count = 4.0
    nosy_names = ['ezio.melotti', 'Andrew.Lutomirski', 'iritkatriel', 'macgors']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue17246'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    Linked PRs

    Activity

    1. AndrewLutomirski commented on Feb 19, 2013

      AndrewLutomirskimannequin
      MannequinAuthor

      inspect.formatargvalues assumes (incorrectly) that every argument in args is a key in values. This isn't very hard to break -- see the attachment for a complete example.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Feb 19, 2013
    3. ezio-melotti commented on Feb 22, 2013

      @ezio-melotti
      Member

      The bug seems to be in inspect indeed:

      import inspect
      def fun(x):
          del x
          return inspect.currentframe()
      
      inspect.formatargvalues(*inspect.getargvalues(fun(10)))

      Attached a proof-of-concept patch that replaces the missing value with <deleted>. If the approach is ok, the same fix might have to be applied to other functions and/or other types of arguments.

    4. iritkatriel commented on Jun 18, 2021

      @iritkatriel
      Member

      Still the same in 3.11:

      >>> import inspect
      >>> def fun(x):
      ...     del x
      ...     return inspect.currentframe()
      ...
      >>> inspect.formatargvalues(*inspect.getargvalues(fun(10)))
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "C:\Users\User\src\cpython-dev\lib\inspect.py", line 1444, in formatargvalues
          specs.append(convert(args[i]))
        File "C:\Users\User\src\cpython-dev\lib\inspect.py", line 1441, in convert
          return formatarg(name) + formatvalue(locals[name])
      KeyError: 'x'
      >>>
    5. changed the title [-]cgitb fails when frame arguments are deleted (due to inspect bug I think)[/-] [+]inspect.getargvalues fails if arg name is not bound to a value[/+] on Jun 18, 2021
    6. macgors commented on Mar 29, 2022

      macgorsmannequin
      Mannequin

      In my opinion formatargvalues function is quite useless in current day as ArgInfo provides all the information. I'm however willing to fix this. IMO we should just omit the args/varargs/kwargs that have been deleted, since the values do not exist in the current frame anymore.

      I can implement the idea propossed in 2013, but I feel this seems less intuative(?) and might be worse for backwards compatibility (if anyone ever actually expects some values from formatargvalues, but I haven't been able to find an example of that).

      Thank you in advance for any input.

    7. transferred this issue fromon Apr 10, 2022
    8. furkanonder commented on Mar 7, 2023

      @furkanonder
      Contributor

      The PR is ready for review. @iritkatriel @ezio-melotti

    9. added
      3.13only security fixes
      3.14bugs and security fixes
      3.15bugs and security fixes
      triagedThe issue has been accepted as valid by a triager.
      and removed
      3.11only security fixes
      on Aug 19, 2025
    10. added 2 commits that reference this issue on Oct 21, 2026
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.13only security fixes3.14bugs and security fixes3.15bugs and security fixeseasystdlibStandard Library Python modules in the Lib/ directorytriagedThe issue has been accepted as valid by a triager.type-bugAn unexpected behavior, bug, or error

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions