Skip to content

inlined comprehension and closed over variables have buggy interactions #156664

Description

@cfbolz

Bug report

Bug description:

We are a bit late to report this, but the following of PyPy's unit tests got broken by inlined comprehensions in CPython 3.12:

def test_inlined_comprehension_private_name_reuse():
    x = 3

    def f():
        [x for x in [1]]
        return [x for _ in [1]]

    assert f() == [3]

test_inlined_comprehension_private_name_reuse()

this passes fine in CPython 3.11 but breaks like this in 3.12 onwards, including on main:

$ ./python /tmp/bug.py
Traceback (most recent call last):
  File "/tmp/bug.py", line 10, in <module>
    test_inlined_comprehension_private_name_reuse()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/tmp/bug.py", line 8, in test_inlined_comprehension_private_name_reuse
    assert f() == [3]
           ~^^
  File "/tmp/bug.py", line 6, in f
    return [x for _ in [1]]
            ^
UnboundLocalError: cannot access local variable 'x' where it is not associated with a value

I wonder whether that is the kind of bug that would be easier to fix when doing #124697

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions