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

Abierto
#121,399 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

docs
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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?

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con la redacción de FOR_ITER en Doc/library/dis.rst y compárala con los casos FOR_ITER y END_FOR de Python/generated_cases.c.h citados por el issue. Verifica las rutas normal y alternativa de agotamiento del iterador en Python 3.12 y 3.13, y actualiza la documentación si se confirman ambos comportamientos de la pila y pueden explicarse claramente.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
compilers, documentation
Tipo de issue
Documentación
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
48/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.