[Python-ideas] Revised**12 PEP on Yield-From

Erik Groeneveld erik at cq2.org
Wed Apr 22 16:51:17 CEST 2009


Dear Greg,

2009/4/19 Greg Ewing <greg.ewing at canterbury.ac.nz>:
> Draft 13 of the PEP.

>    * Exceptions other than GeneratorExit thrown into the delegating
>      generator are passed to the ``throw()`` method of the iterator.
>      If the call raises StopIteration, the delegating generator is resumed.
>      Any other exception is propagated to the delegating generator.

I have implemented this in Weightless, and changed my implementation
as to work with BaseException instead of Exception.  This works well.
However, I was not able to make an exception for GeneratorExit, see
next point.

>    * If a GeneratorExit exception is thrown into the delegating generator,
>      or the ``close()`` method of the delegating generator is called, then
>      the ``close()`` method of the iterator is called if it has one. If this
>      call results in an exception, it is propagated to the delegating generator.
>      Otherwise, GeneratorExit is raised in the delegating generator.

I tried to implement this, but I failed.  The reason is that a
generator's close() checks for an exception being raised by its
generator.  When no exception has been raised, it will raise the
RuntimeError('generator ignored GeneratorExit').  And when an
exception has been raised (regardless what type), it will exit just
normally.  So the phrase

> If this
>      call results in an exception, it is propagated to the delegating generator.

applies only to the RuntimeError close() might throw.  And the ramaining phrase

>      Otherwise, GeneratorExit is raised in the delegating generator.

makes it behave no different than for all other types of exceptions.

Was this intended?  If Yes, I suggest to make the text clearer and
more specific about it.  If No, then what is the correct expansion?

Best regards
Erik Groeneveld



More information about the Python-ideas mailing list