Skip to content

Wrong binding of closure variable with intervening list comprehension #121377

Open

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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Jul 4, 2024
  2. gaogaotiantian commented on Jul 4, 2024

    @gaogaotiantian
    Member

    This is probably related to PEP 709 @carljm .

  3. added
    3.12only security fixes
    3.13only security fixes
    3.14bugs and security fixes
    on Jul 4, 2024
  4. self-assigned this
    on Jul 5, 2024
  5. carljm commented on Jul 5, 2024

    @carljm
    Member

    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.

  6. limwz01 commented on Aug 27, 2024

    @limwz01
    Author

    @carljm any updates?

  7. carljm commented on Aug 27, 2024

    @carljm
    Member

    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!

  8. added 2 commits that reference this issue on Sep 27, 2024
  9. removed their assignment
    on Jan 30, 2026
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.12only security fixes3.13only 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