Issue 17246: inspect.getargvalues fails if arg name is not bound to a value (original) (raw)

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 . 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 "", 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' >>>
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:42 admin set github: 61448
2022-03-29 18:46:57 macgors set nosy: + macgorsmessages: +
2022-03-24 14:37:23 iritkatriel set keywords: + easy, - patch
2021-06-18 10:14:36 iritkatriel set 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
2013-02-22 22:59:09 ezio.melotti set files: + issue17246.difftype: behaviorkeywords: + patchnosy: + ezio.melottimessages: + stage: patch review
2013-02-19 22:22:36 Andrew.Lutomirski create