classification
Title: Incorrect stack traces when re-entering a generator/coroutine stack via .throw()
Type: Stage:
Components: Interpreter Core Versions: Python 3.7, Python 3.6, Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: njs
Priority: normal Keywords:

Created on 2017-02-17 14:23 by njs, last changed 2017-04-11 13:02 by Mariatta.

Files
File name Uploaded Description Edit
weird-throw-stack.py njs, 2017-02-17 14:23
Messages (1)
msg288010 - (view) Author: Nathaniel Smith (njs) * Date: 2017-02-17 14:23
The attached script sets up a stack of generators calling each other via 'yield from': f yields from g yield from h. Then the generator at the bottom of the stack yields.

Before they yield, sys._getframe shows the expected stack (or if you put in traceback.print_stack() you get the same thing).

After they yield, it depends: if the generator is resumed via 'gen.send(None)', then the stack looks sensible. But if the generator is resumed via 'gen.throw(...)', then the stack is weird: Objects/genobject.c:_gen_throw implements 'yield from' in an weird manual way, where it first directly resumes the innermost generator frame, and then propagates any result to the next generator frame, etc. So the output I get from the sample script is:

~$ python3.6 weird-throw-stack.py
-- f before yielding --
f
<module>
-- g before yielding --
g
f
<module>
-- h before yielding --
h
g
f
<module>
-- h after yielding --
h
<module>
-- g after yielding --
g
<module>
-- f after yielding --
f
<module>

This causes problems for stack-based profiling (https://github.com/vmprof/vmprof-python/issues/117), debuggers, and other tools that need to introspect the stack.
History
Date User Action Args
2017-04-11 13:02:48Mariattasetcomponents: + Interpreter Core
2017-02-17 14:23:48njscreate