python / python/cpython

Incomplete stack traces when throwing into a generator chain that ends in a custom generator

未关闭
#126,091 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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

描述

Bug report

Bug description:

bpo-29590 identified an issue where only the head of a chain of generators was included in stack-traces when processing a thrown-in exception. I think this issue still exists but only in the case where the head of a chain of generators is a custom generator.

Consider the following example in which a custom generator's throw() method does not see its predecessor in the yield from chain (the g() generator), but the builtin in generator does:

$ cat test.py
import sys

class UserGen:
  def throw(self, *args):
    print("Traceback from UserGen:")
    f = sys._getframe()
    while f:
      print(f)
      f = f.f_back
  def __iter__(self):
    return self
  def __next__(self):
    return 42

def real_gen():
  yield 43

def g(target):
  yield from target

gg = g(UserGen())
gg.send(None)
gg.throw(RuntimeError)

print()

gg = g(real_gen())
gg.send(None)
gg.throw(RuntimeError)


$ python3 test.py
Traceback from UserGen:
<frame at 0x10273e230, file 'test.py', line 8, code throw>
<frame at 0x10270d440, file 'test.py', line 26, code <module>>

Traceback (most recent call last):
  File "test.py", line 32, in <module>
    gg.throw(RuntimeError)
  File "test.py", line 22, in g
    yield from target
  File "test.py", line 18, in real_gen
    yield 43
RuntimeError

The fix for bpo-29590 is in #19896 and appears to specifically only link frames together for chains of builtin generators only. I suspect this was just an oversight rather than a deliberate choice though.

I attach a proposed fix in a PR.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs
  • gh-126092

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先运行 test.py 复现程序,并比较 custom-generator 和 real_gen 的堆栈跟踪。检查 gh-126092 中提出的修复,并验证完成后的行为包含 custom-generator 链中的前置元素,同时不会使 builtin-generator 情况发生回归。

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

评估

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

把新 issue 发到你的邮箱

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