10000 Incomplete stack traces when throwing into a generator chain that ends in a custom generator · Issue #126091 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jbower-fb opened this issue Oct 28, 2024 · 2 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@jbower-fb
Copy link
Contributor
jbower-fb commented Oct 28, 2024

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

@jbower-fb jbower-fb added the type-bug An unexpected behavior, bug, or error label Oct 28, 2024
@picnixz picnixz added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Oct 28, 2024
@jbower-fb
Copy link
Contributor Author

cc @1st1 , @cjerdonek , @markshannon , and @njsmith who were involved in the first iteration of this issue.

DinoV pushed a commit that referenced this issue Nov 21, 2024
…exception through a yield-from chain (#126092)

Always link generator frames when propagating a thrown-in exception through a yield-from chain.
@ZeroIntensity
Copy link
Member

Is this done? Or was this supposed to get backported?

ebonnal pushed a commit to ebonnal/cpython that referenced this issue Jan 12, 2025
…wn-in exception through a yield-from chain (python#126092)

Always link generator frames when propagating a thrown-in exception through a yield-from chain.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants
0