python / python/cpython

dis: FOR_ITER says it no longer pops the stack in 3.12 but it still does when the iterator ended normally

Offen
#121,399 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

docs
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Documentation

The FOR_ITER docs in the dis module say "Up until 3.11 the iterator was popped when it was exhausted". This sounds like in 3.12+ the iterator is not popped anymore:

https://github.com/python/cpython/blob/cecd6012b0ed5dca3916ae341e705ae44172991d/Doc/library/dis.rst?plain=1#L1334-L1342

Instead there is a new opcode END_FOR, which takes care of popping the iterator off the stack. This surprised me because in 3.12 END_FOR is supposed to remove 2 elements from the top of the stack. But if the iterator ends normally, there will only be the iterator at the top. I tried to document my thought process in this godbolt repro.

Reading the generated code for FOR_ITER, specifically:

https://github.com/python/cpython/blob/cecd6012b0ed5dca3916ae341e705ae44172991d/Python/generated_cases.c.h#L3069

https://github.com/python/cpython/blob/cecd6012b0ed5dca3916ae341e705ae44172991d/Python/generated_cases.c.h#L3085-L3087

it looks like there are 2 cases:

  • If the iterator ends normally, FOR_ITER pops the iterator off the stack, then it skips the next END_FOR (and in 3.13 POP_TOP) instructions.
  • Otherwise (I'm not sure when that happens?), the iterator ends with both the iter and iter() on the stack, which are both popped by END_FOR (and in 3.13 POP_TOP).

Is it worth documenting the 2 different cases?

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 der FOR_ITER-Formulierung in Doc/library/dis.rst und vergleiche sie mit den FOR_ITER- und END_FOR-Fällen in Python/generated_cases.c.h, auf die sich das Issue bezieht. Überprüfe die normalen und alternativen Pfade bei erschöpften Iteratoren in Python 3.12 und 3.13 und aktualisiere die Dokumentation, wenn beide Stack-Verhaltensweisen bestätigt und klar erklärt werden können.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers, documentation
Issue-Typ
Dokumentation
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

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