Skip to content

gh-61448: Fix inspect.formatargvalues for deleted arguments#144854

Open
RoryGlenn wants to merge 1 commit intopython:mainfrom
RoryGlenn:gh-61448-inspect-getargvalues
Open

gh-61448: Fix inspect.formatargvalues for deleted arguments#144854
RoryGlenn wants to merge 1 commit intopython:mainfrom
RoryGlenn:gh-61448-inspect-getargvalues

Conversation

@RoryGlenn
Copy link

Fix inspect.formatargvalues() to handle arguments that are no longer bound in the frame locals (e.g. after del) by showing them as <deleted> instead of raising KeyError.

Changes

  • Added a _DELETED sentinel object with repr of <deleted> in Lib/inspect.py
  • Changed formatargvalues() to use locals.get(name, _DELETED) instead of locals[name] for regular arguments, *args, and **kwargs
  • Added two tests: one for deleted regular arguments, one for deleted *args/**kwargs

Reproducer (before fix)

import inspect

def fun(x):
    del x
    return inspect.currentframe()

inspect.formatargvalues(*inspect.getargvalues(fun(10)))
# KeyError: 'x'

After fix

inspect.formatargvalues(*inspect.getargvalues(fun(10)))
# '(x=<deleted>)'

Note: This supersedes the stale PR #101318 which has been awaiting changes since August 2023 with merge conflicts.

Fix inspect.formatargvalues() to handle arguments that are no longer
bound in the frame locals (e.g. after 'del') by showing them as
<deleted> instead of raising KeyError. This applies to regular
arguments, *args, and **kwargs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant