dis: FOR_ITER reports wrong exhausted-path jump target (END_FOR instead of POP_ITER)
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
Bug description:
In Python 3.15, dis reports the wrong jump target for FOR_ITER when the iterator is exhausted. The reported target lands on END_FOR, but the CPython runtime actually jumps one instruction past it to POP_ITER.
Root cause: In bytecodes.c, FOR_ITER uses JUMPBY(oparg + 1) to skip past END_FOR. However, dis computes the jump target as NIP + oparg * 2 bytes (without the +1), landing on END_FOR rather than POP_ITER.
import dis
def f():
for x in [1, 2, 3]:
pass
instrs = {i.offset: i for i in dis.get_instructions(f.__code__, show_caches=True)}
for i in dis.get_instructions(f.__code__):
if i.opname == "FOR_ITER":
for_iter = i
# dis reports this as the jump target:
reported_target = for_iter.jump_target
reported_name = instrs[reported_target].opname # "END_FOR"
# CPython runtime actually jumps here (oparg+1 instructions past NIP):
nip = for_iter.offset + 4 # 4 bytes = FOR_ITER word + CACHE word
actual_target = nip + (for_iter.arg + 1) * 2
actual_name = instrs[actual_target].opname # "POP_ITER"
print(f"dis reports: offset {reported_target} = {reported_name}") # END_FOR
print(f"runtime jumps: offset {actual_target} = {actual_name}") # POP_ITER
Output:
dis reports: offset 20 = END_FOR
runtime jumps: offset 22 = POP_ITER
The stack-effect model remains self-consistent (FOR_ITER +1, END_FOR -1, POP_ITER -2), but the reported jump target is misleading to any tool that builds a control-flow graph from dis output. The exhausted-iterator edge should point to POP_ITER, not END_FOR.
CPython versions tested on:
CPython main branch (3.15)
Operating systems tested on:
macOS
Linked PRs
- gh-149503
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con l’implementazione di FOR_ITER in bytecodes.c e con il punto di ingresso di dis che calcola le destinazioni dei salti. Confronta la destinazione dell’iteratore esaurito descritta nel report con il comportamento a runtime, quindi aggiungi una copertura di regressione che dimostri che la destinazione è POP_ITER anziché END_FOR.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 35/100