cpython icon indicating copy to clipboard operation
cpython copied to clipboard

Wrong binding of closure variable with intervening list comprehension

Open limwz01 opened this issue 1 year ago • 1 comments

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

limwz01 avatar Jul 04 '24 18:07 limwz01

This is probably related to PEP 709 @carljm .

gaogaotiantian avatar Jul 04 '24 19:07 gaogaotiantian

I've just moved house and am behind on everything, so it may be a bit before I can get to this, but I will fix it.

carljm avatar Jul 05 '24 03:07 carljm

@carljm any updates?

limwz01 avatar Aug 27 '24 06:08 limwz01

I'm intending to work on this during the upcoming core dev sprint in September. If someone else gets to it first that's fine, though!

carljm avatar Aug 27 '24 13:08 carljm