This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: inspect.getclosurevars() does not get all globals
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: jhrmnn, josh.r
Priority: normal Keywords:

Created on 2018-10-09 20:58 by jhrmnn, last changed 2022-04-11 14:59 by admin.

Messages (3)
msg327436 - (view) Author: Jan Hermann (jhrmnn) Date: 2018-10-09 20:58
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)>>> inspect.getclosurevars(f)
ClosureVars(nonlocals={}, globals={}, builtins={'range': <class 'range'>}, unbound=set())
(ins)>>> def f():
(ins)...     return x
(ins)>>> inspect.getclosurevars(f)
ClosureVars(nonlocals={}, globals={'x': 1}, builtins={}, unbound=set())

It can be fixed quite easily along the following lines:

Does this look like a reasonable solution?
msg327444 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2018-10-09 23:04
Problem: The variables from the nested functions (which comprehensions are effectively a special case of) aren't actually closure variables for the function being inspected.

Allowing recursive identification of all closure variables might be helpful in some contexts, but you wouldn't want it to be the only behavior; it's easier to convert a non-recursive solution to a recursive solution than the other way around.
msg328319 - (view) Author: Jan Hermann (jhrmnn) Date: 2018-10-23 14:56
Ok, that’s fair. But then the inspect module currently doesn’t provide tools to the user to construct the recursive identification without duplicating code already in stdlib. For that, one would need to refactor getclosurevars() to two parts: getcode() and getclosurevars_from_code(). Then one could do:

clvars = ClosureVars({}, {}, {}, set())
codes = [getcode(func)]
while codes:
   code = codes.pop()
   for const in code.co_consts:
       if iscode(const):
   lclvars = getclosurevars_from_code(code)
   for v, lv in zip(clvars, lclvars):
Date User Action Args
2022-04-11 14:59:07adminsetgithub: 79128
2018-10-23 14:56:22jhrmnnsetmessages: + msg328319
2018-10-09 23:04:58josh.rsetnosy: + josh.r
messages: + msg327444
2018-10-09 20:58:00jhrmnncreate