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
Priority: normal Keywords: patch

Created on 2013-02-19 22:22 by Andrew.Lutomirski, last changed 2021-06-18 10:14 by iritkatriel.

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 (3)
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'
>>>
History
Date User Action Args
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