python / python/cpython

Same arguments and more similar documentation for exec() and for eval().

Offen
#100,068 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

interpreter-core type-feature
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
36k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Feature or enhancement

This involves changes to the documentation and to the implementation.

I notice that the arguments for exec() and eval() differ in certain ways, which I believe are unnecessary.
When the source is a code object, both calls result in a call to PyEval_EvalCode or, if closure is given, to PyEval_EvalCodeEx.
When the source is a string, there is no closure argument allowed, and the source is compiled as a file input or as an eval input, and then there is a call to PyRun_String or PyRun_StringFlags.
Therefore, the differences between the two functions are solely in the builtin_exec_impl() and builtin_eval_impl() functions in bltinmodule.c., and builtin_exec() and builtin_eval() in bltinmodule.c.h.

  1. eval() doesn't take a closure argument. It would be useful if calling eval('x := 3'), for example, if x is a nonlocal variable.
  2. eval() and exec() both require that a globals argument be an actual dict object or subclass. However, the documentation for exec() is incorrect and should be changed, by removing the phrase ' (and not a subclass of dictionary)'.
  3. When the first argument is a string, eval() returns the result of compiling and running the string as an expresion, and exec() compiles and runs the string as a file input and returns None.

Pitch

I've had difficulty understanding how both of these functions work, due to confusing documentation of them. They largely say the same thing but in different ways, and this obscures the fact that the two functions are mostly the same.

I imagine that the closure argument was not added to eval() as an oversight. However, eval() could be used to access a nonlocal variable in the caller's namespace in the same way as exec() could. It could even modify the variable if the code object is compiled from an expression containing a walrus.

Previous discussion

discussion.
This discussion post has more details and suggestions for making the proposed changes.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit bltinmodule.c und bltinmodule.c.h, insbesondere mit builtin_exec_impl(), builtin_eval_impl(), builtin_exec() und builtin_eval(). Lies die verlinkte Python-Diskussion, bevor du das beabsichtigte Verhalten von Argumenten und Closures festlegst, und sieh dir anschließend die Dokumentation zu exec() und eval() an. Als erledigt gilt die Aufgabe, wenn Implementierung und Dokumentation das vereinbarte Verhalten konsistent beschreiben und unterstützen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
backend, documentation
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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