gh-70764: inspect.getclosurevars now identifies global variables with… · python/cpython@83ba8c2 · GitHub
Skip to content

Commit 83ba8c2

Browse files
authored
gh-70764: inspect.getclosurevars now identifies global variables with LOAD_GLOBAL (#120143)
1 parent fc233f4 commit 83ba8c2

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

Lib/inspect.py

Lines changed: 9 additions & 5 deletions

Lib/test/test_inspect/test_inspect.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,6 +1960,19 @@ def g(local_ref):
19601960
builtin_vars, unbound_names)
19611961
self.assertEqual(inspect.getclosurevars(C().f(_arg)), expected)
19621962

1963+
def test_attribute_same_name_as_global_var(self):
1964+
class C:
1965+
_global_ref = object()
1966+
def f():
1967+
print(C._global_ref, _global_ref)
1968+
nonlocal_vars = {"C": C}
1969+
global_vars = {"_global_ref": _global_ref}
1970+
builtin_vars = {"print": print}
1971+
unbound_names = {"_global_ref"}
1972+
expected = inspect.ClosureVars(nonlocal_vars, global_vars,
1973+
builtin_vars, unbound_names)
1974+
self.assertEqual(inspect.getclosurevars(f), expected)
1975+
19631976
def test_nonlocal_vars(self):
19641977
# More complex tests of nonlocal resolution
19651978
def _nonlocal_vars(f):
Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)