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

Ron Adam ron3200 at gmail.com
Sat Nov 19 18:48:59 CET 2011


On Sat, 2011-11-19 at 18:25 +1000, Nick Coghlan wrote:
> 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.

I'll see if I can get that to work.


> 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's really no different than gen.throw.() only going one deep.  It
doesn't go all the way to the bottom "implicitly" and then unwinds back
out.


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

Yes, I think it would work nicely with yield-from.


> 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.

I think *any* needs to be qualified in some way.  The frame inside need
to have some mechanism to be suspended or to pass control to someplace
else. (or through it)  That mechanism needs to be explicitly spelled in
some way.

We really need a good example of what that would/should look like.  What
exactly is the behavior you are looking for?

Cheers,
   Ron



























More information about the Python-ideas mailing list