[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:
Miss Islington (bot) 2024-11-06 01:18:32 +01:00 committed by GitHub
parent 9dd3addc0c
commit ba86632f7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 5 deletions

View File

@ -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:

View File

@ -2160,6 +2160,19 @@ class TestGetClosureVars(unittest.TestCase):
builtin_vars, unbound_names)
self.assertEqual(inspect.getclosurevars(C().f(_arg)), expected)
def test_attribute_same_name_as_global_var(self):
class C:
_global_ref = object()
def f():
print(C._global_ref, _global_ref)
nonlocal_vars = {"C": C}
global_vars = {"_global_ref": _global_ref}
builtin_vars = {"print": print}
unbound_names = {"_global_ref"}
expected = inspect.ClosureVars(nonlocal_vars, global_vars,
builtin_vars, unbound_names)
self.assertEqual(inspect.getclosurevars(f), expected)
def test_nonlocal_vars(self):
# More complex tests of nonlocal resolution
def _nonlocal_vars(f):

View File

@ -0,0 +1 @@
Fixed an issue where :func:`inspect.getclosurevars` would incorrectly classify an attribute name as a global variable when the name exists both as an attribute name and a global variable.