Closed
Description
See #242 for background of why generators are important.
In vm.c, there's a comment:
// re-raise exception to higher level
// TODO what to do if this is a generator??
nlr_jump(nlr.ret_val);
And indeed, it's rather unclear how to do exception handling with generators.
First of all, we have 2 directions of exception flow:
- Exception occurring in generator should be propagated back to caller. Fortunately, throwing exception out of generator terminates it, just the same as returning value from it. (Don't have formal reference handy, but here's: http://stackoverflow.com/questions/11366064/handle-an-exception-thrown-in-a-generator , and it makes perfect sense: normal exceptions are not restartable, so why exceptions in generators would be?)
- Exceptions can be injected into generator using generator.throw(). Within generator, they are raised at current yield point.
Then with Py3.3 and yield from
, we apparently have not just 2 contexts of "main" and "generator", but stack of generator contexts. Intuitively, these context are higher-order ones than try/except contexts. Within generator, there can be number of enclosing try blocks, and only when exception escapes from outermost block, we have generator context switch, and need to marshal exception.