Skip to content

Wrong binding of closure variable with intervening list comprehension #121377

@limwz01

Description

@limwz01

Bug report

Bug description:

def f(x):
    def g():
        print([x for x in range(10)])
        def h():
            print(x)
        return h
    print(x)
    return g
f(1)()()

The above had always bound x in h to the parameter of f. But it no longer works and somehow binds to a local in g, throwing the following error:

NameError: cannot access free variable 'x' where it is not associated with a value in enclosing scope

CPython versions tested on:

3.12

Operating systems tested on:

Linux, Windows

Linked PRs

Metadata

Metadata

Assignees

Labels

3.12only security fixes3.13bugs and security fixes3.14bugs and security fixestype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions