python / python/cpython

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

未关闭
#143,552 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 Lib/types.py 第255-289行开始,然后检查 inspect.getgeneratorstate() 和 inspect.getcoroutinestate(),报告将它们标识为受影响的入口点。将 wrapper 暴露的属性与 GeneratorType 和 CoroutineType 进行比较;完成的标准是两个 inspect 函数都能用于被包装的对象,并且列出的挂起和来源属性保持一致。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
28/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。