inlined comprehension and closed over variables have buggy interactions
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 35.9k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
- gh-156691
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Führe zunächst den bereitgestellten Reproducer /tmp/bug.py auf CPython main aus und vergleiche ihn mit Python 3.11. Lies vor der Untersuchung die Interaktion mit issue #124697 und den verknüpften PR gh-156691. Als erledigt gilt die Aufgabe, wenn das Beispiel [3] ohne UnboundLocalError zurückgibt und dabei das Verhalten von inline ausgeführten Comprehensions beibehält.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100