python / python/cpython

`types._GeneratorWrapper` is no longer consistent with `types.GeneratorType`/`types.CoroutineType`

Abierto
#143,552 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

stdlib type-bug
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Bug report

Bug description:

types.coroutine() uses _GeneratorWrapper as a wrapper for generator iterators in cases where the callable object is not a user-defined function (but is, for example, a generator function compiled via Cython/Nuitka). However, the implementation has not been updated for 11 years and therefore does not provide certain properties, such as:

  • GeneratorType.gi_suspended (Python ≥3.11, python/cpython#30633)
  • CoroutineType.cr_suspended (Python ≥3.11, python/cpython#30633)
  • CoroutineType.cr_origin (Python ≥3.7, python/cpython#5250)

As a result, the objects returned by the new function do not look exactly like types.GeneratorType/types.CoroutineType, which in particular makes them incompatible with the inspect.getgeneratorstate() and inspect.getcoroutinestate() functions:

>>> from inspect import getcoroutinestate, getgeneratorstate
>>> from types import coroutine
>>> def generator_function():
...     return
...     yield  # generator definition
>>> @coroutine
... def coroutine_function():
...     return generator_function()
>>> gencoro = coroutine_function()
>>> getgeneratorstate(gencoro)
Traceback (most recent call last):
AttributeError: '_GeneratorWrapper' object has no attribute 'gi_suspended'
>>> getcoroutinestate(gencoro)
Traceback (most recent call last):
AttributeError: '_GeneratorWrapper' object has no attribute 'cr_suspended'

Interestingly, there was already an attempt to add *_suspended in #133373 (see https://github.com/python/cpython/issues/133372#issuecomment-2849166854), but the PR was closed in favor of removing the TODO comment, and this side issue was apparently overlooked.

Related: cython/cython#7448.

CPython versions tested on:

3.11, 3.12, 3.13, 3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-143609

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

Empieza por las líneas 255-289 de Lib/types.py y, después, inspecciona inspect.getgeneratorstate() e inspect.getcoroutinestate(), que el informe identifica como puntos de entrada afectados. Compara los atributos expuestos por el wrapper con GeneratorType y CoroutineType; se considera terminado cuando ambas funciones de inspect funcionan para los objetos envueltos y las propiedades de suspensión y origen indicadas son coherentes.

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

Evaluación

Stack tecnológico
python
Área
backend
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
28/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.