Skip to content

generator throw delivered to incorrect generator under trace #105162

@graingert

Description

@graingert

Bug report

With the following demo code, a subgenerator yield from from a parent generator main(), that catches exceptions and returns "good":

class Inner: def send(self, v): return None def __iter__(self): return self def __next__(self): return self.send(None) def throw(self, *exc_info): raise StopIteration("good") def main(): return (yield from Inner()) def run(coro): coro.send(None) try: coro.throw(Exception) except StopIteration as e: print(e.value) run(main())

when run with python -m trace, the exception is delivered to the outer generator main() instead of being suppressed

graingert@conscientious  ~/projects/cpython   main  ./python -m trace --count -C . demo.py Traceback (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main File "<frozen runpy>", line 88, in _run_code File "/home/graingert/projects/cpython/Lib/trace.py", line 741, in <module> main() File "/home/graingert/projects/cpython/Lib/trace.py", line 729, in main t.runctx(code, globs, globs) File "/home/graingert/projects/cpython/Lib/trace.py", line 451, in runctx exec(cmd, globals, locals) File "demo.py", line 26, in <module> run(main()) File "demo.py", line 21, in run coro.throw(Exception) File "demo.py", line 16, in main return (yield from Inner()) ^^^^^^^^^^^^^^^^^^ Exception ✘ graingert@conscientious  ~/projects/cpython   main ± ./python demo.py good 

the problem also occurs with the equivalent generator syntax, eg:

def Inner(): try: while True: yield None except: return "good"

Your environment

  • CPython versions tested on: c05c31d 3.12b1
  • Operating system and architecture: Ubuntu 22.04 x86_64

see also

urllib3/urllib3#3049 (comment)
nedbat/coveragepy#1635

Linked PRs

Metadata

Metadata

Assignees

Labels

3.12only security fixes3.13bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)release-blockertype-bugAn unexpected behavior, bug, or error

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions