[issue34947] inspect.getclosurevars() does not get all globals

Jan Hermann report at bugs.python.org
Tue Oct 9 16:58:00 EDT 2018


New submission from Jan Hermann <dev at janhermann.cz>:

inspect.getclosurevars() omits globals or nonlocals that are bound within comprehensions:

22:50 ~ python3
Python 3.7.0 (default, Aug 17 2018, 21:14:48) 
[Clang 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(ins)>>> import inspect
(ins)>>> x = 1
(ins)>>> def f():
(ins)...     return [x for _ in range(1)]
(ins)... 
(ins)>>> inspect.getclosurevars(f)
ClosureVars(nonlocals={}, globals={}, builtins={'range': <class 'range'>}, unbound=set())
(ins)>>> def f():
(ins)...     return x
(ins)... 
(ins)>>> inspect.getclosurevars(f)
ClosureVars(nonlocals={}, globals={'x': 1}, builtins={}, unbound=set())

It can be fixed quite easily along the following lines:

https://github.com/azag0/calcfw/blob/master/caf2/hashing/func.py#L133-L146

Does this look like a reasonable solution?

----------
components: Library (Lib)
messages: 327436
nosy: azag0
priority: normal
severity: normal
status: open
title: inspect.getclosurevars() does not get all globals
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34947>
_______________________________________


More information about the Python-bugs-list mailing list