python / python/cpython

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

オープン
#126,091 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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. リポジトリをフォークし、ブランチを切って変更します。
  4. 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 を短くまとめたダイジェスト。