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.
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 . If the approach is ok, the same fix might have to be applied to other functions and/or other types of arguments.
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 "", line 1, in 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' >>>
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.
nosy: + iritkatrieltitle: cgitb fails when frame arguments are deleted (due to inspect bug I think) -> inspect.getargvalues fails if arg name is not bound to a valuemessages: + versions: + Python 3.9, Python 3.10, Python 3.11, - Python 2.7