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.getargvalues fails if arg name is not bound to a value
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Andrew.Lutomirski, ezio.melotti, iritkatriel, macgors
Priority: normal Keywords: easy

Created on 2013-02-19 22:22 by Andrew.Lutomirski, last changed 2022-04-11 14:57 by admin.

Files
File name Uploaded Description Edit
test_cgitb.py Andrew.Lutomirski, 2013-02-19 22:22 Short script demonstrating the problem
issue17246.diff ezio.melotti, 2013-02-22 22:59 Proof of concept against 2.7.
Messages (4)
msg182446 - (view) Author: Andrew Lutomirski (Andrew.Lutomirski) Date: 2013-02-19 22:22
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.
msg182705 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-02-22 22:59
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.
msg396039 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-18 10:14
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'
>>>
msg416283 - (view) Author: Maciej Górski (macgors) * Date: 2022-03-29 18:46
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.
History
Date User Action Args
2022-04-11 14:57:42adminsetgithub: 61448
2022-03-29 18:46:57macgorssetnosy: + macgors
messages: + msg416283
2022-03-24 14:37:23iritkatrielsetkeywords: + easy, - patch
2021-06-18 10:14:36iritkatrielsetnosy: + iritkatriel
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
messages: + msg396039

versions: + Python 3.9, Python 3.10, Python 3.11, - Python 2.7
2013-02-22 22:59:09ezio.melottisetfiles: + issue17246.diff

type: behavior

keywords: + patch
nosy: + ezio.melotti
messages: + msg182705
stage: patch review
2013-02-19 22:22:36Andrew.Lutomirskicreate