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

Ron Adam ron3200 at gmail.com
Sat Nov 19 23:57:51 CET 2011


On Sat, 2011-11-19 at 18:25 +1000, Nick Coghlan wrote:
> 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 was thinking about this today and a few things occurred to me.

You don't want to suspend a regular function.  What would happen if
another function tried to call it in it's suspended state?

To make that work, each invocation would need it's own frame.  That is
why generators return a generator object instance.  To do that with
functions would require making a function call into a
called-function-instance.  To be able to suspend and resume that
instance, you will need a reference to the instance rather than the
function.  So then you get something that is very much like a generator.
The only difference is it doesn't have a yield.  But in order for that
to suspend, it would need some mechanism to suspend itself.  Like a
yield.  And then we are right back to generators.

The other approach is to use thread objects.  Which create instances
that can be suspended.  The advantage of using threads, is that the
thread manager can suspend and resume threads independently of what the
thread is doing.  But that requires more resources to do that and they
may not be as efficient in tight loops.

The two different approaches are completely separate.  So I don't see
how this effects either yield-from or a possible yield-raise feature.

I'm probably missing something, but I can't put my finger on it.

Cheers,
   Ron





More information about the Python-ideas mailing list