[Python-ideas] Exceptions thrown from generators.. patch.

Nick Coghlan ncoghlan at gmail.com
Sat Nov 19 09:25:17 CET 2011


On Sat, Nov 19, 2011 at 5:55 PM, Gregory P. Smith <greg at krypto.org> wrote:
> A double word syntax might make sense here.  It'd be good to keep 'yeild' in
> it to make it clear that this is offering the exception out in a
> non-terminal manner.
>   yield raise MyException(x,y,z)
> or if you've caught an exception from something and want to pass that on to
> the caller without killing the iteration the natural base form of that would
> be:
>   yield raise
> to unset the existing exception and yeild it out instead, mirroring what a
> bare raise within an except: clause does.

Indeed, "yield raise" would be quite appropriate terminology for this
new channel of communication.

However, there's still a potential stack unwinding problem here. For
asymmetric coroutines, we need to be able to communicate an arbitrary
distance up the stack. This idea, as it stands, doesn't provide that -
like an ordinary yielded value, it can only yield control one level
out. So even if the innermost generator is left in a resumable state,
any *external* iterators are still going to be terminated.

It potentially becomes more useful in combination with 'yield from',
since yielded exceptions would be passed up the stack, the same as
yielded values.

So, while it's a neat trick and very cool that it could be implemented
with such a small change, I still don't see how it helps substantially
with the challenge of allowing *any* Python frame on a coroutine
stack, not just generator frames.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list