Skip to content

inspect.getclosurevars() does not get all globals #79128

Open

Description

@jhrmnn
mannequin
BPO 34947
Nosy @MojoVampire, @jhrmnn

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2018-10-09.20:58:00.924>
labels = ['3.7', 'type-bug', 'library']
title = 'inspect.getclosurevars() does not get all globals'
updated_at = <Date 2018-10-23.14:56:22.060>
user = 'https://github.com/jhrmnn'

bugs.python.org fields:

activity = <Date 2018-10-23.14:56:22.060>
actor = 'jhrmnn'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2018-10-09.20:58:00.924>
creator = 'jhrmnn'
dependencies = []
files = []
hgrepos = []
issue_num = 34947
keywords = []
message_count = 3.0
messages = ['327436', '327444', '328319']
nosy_count = 2.0
nosy_names = ['josh.r', 'jhrmnn']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue34947'
versions = ['Python 3.7']

Activity

  1. jhrmnn commented on Oct 9, 2018

    jhrmnnmannequin
    MannequinAuthor

    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?

  2. added
    3.7 (EOL)
    stdlib
    type-bug
    on Oct 9, 2018
  3. MojoVampire commented on Oct 9, 2018

    MojoVampiremannequin
    Mannequin

    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.

  4. jhrmnn commented on Oct 23, 2018

    jhrmnnmannequin
    MannequinAuthor

    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):
               codes.append(const)
       lclvars = getclosurevars_from_code(code)
       for v, lv in zip(clvars, lclvars):
           v.update(lv)
  5. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.7 (EOL)stdlibtype-bug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions