[Feature Request]: Inline comprehensions (PEP 709) so that `eval()` / `exec()` / `locals()` inside one see the enclosing function's locals

Offen
#1,362 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Anfängerfreundlichkeit
12/100
Issue-Typ
Feature
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
java, python
Bereich
compilers

Rechercherichtung

The payload names no source files, so begin by running the two snippets from the issue on GraalPy and on CPython 3.12+ to confirm the mismatch in eval(), exec() and locals() inside a comprehension. The work is compiler-level: comprehensions must share the enclosing function's scope, as PEP 709 specifies. Done means the three rows of the issue's table match CPython output.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

enhancement
Describe the feature

List, set and dict comprehensions are compiled as separate code units, so eval(), exec() and locals() inside a comprehension in a function do not see the function's locals. CPython inlines these comprehensions since 3.12 (PEP 709). Seen on master 3289197e63 (Python 3.13.14) on macOS ARM64.

import sys

def f():
    a = 'abc'
    return [eval('a') for _ in range(1)]

def g():
    a = 'abc'
    return [sys._getframe().f_code.co_name for _ in range(1)]
Expression GraalPy CPython
f() NameError: name 'a' is not defined ['abc']
eval("(lambda a: [eval('a') for _ in range(1)])('abc')") NameError: name 'a' is not defined ['abc']
g() ['<listcomp>'] ['g']
Expected behavior

The CPython column above (3.12.13 and 3.13.14).

Additional context

No response

Vorherrschende Sprache
Python
Sterne
1.7k
Forks
167
Ø Merge
3 T. 11 Std.
Gemergte PRs (30 T.)
68

Entwicklungsumgebung

In Codespaces öffnen

Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus oracle/graalpython

Alle Issues in oracle/graalpython

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.