mirror of
https://github.com/python/cpython.git
synced 2025-10-31 21:51:50 +00:00
[3.13] gh-70764: inspect.getclosurevars now identifies global variables with LOAD_GLOBAL (GH-120143) (#126459)
gh-70764: inspect.getclosurevars now identifies global variables with LOAD_GLOBAL (GH-120143)
(cherry picked from commit 83ba8c2bba)
Co-authored-by: blhsing <blhsing@gmail.com>
This commit is contained in:
parent
9dd3addc0c
commit
ba86632f7e
3 changed files with 23 additions and 5 deletions
|
|
@ -1609,11 +1609,15 @@ def getclosurevars(func):
|
|||
global_vars = {}
|
||||
builtin_vars = {}
|
||||
unbound_names = set()
|
||||
for name in code.co_names:
|
||||
if name in ("None", "True", "False"):
|
||||
# Because these used to be builtins instead of keywords, they
|
||||
# may still show up as name references. We ignore them.
|
||||
continue
|
||||
global_names = set()
|
||||
for instruction in dis.get_instructions(code):
|
||||
opname = instruction.opname
|
||||
name = instruction.argval
|
||||
if opname == "LOAD_ATTR":
|
||||
unbound_names.add(name)
|
||||
elif opname == "LOAD_GLOBAL":
|
||||
global_names.add(name)
|
||||
for name in global_names:
|
||||
try:
|
||||
global_vars[name] = global_ns[name]
|
||||
except KeyError:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue