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

Erik Groeneveld erik at cq2.org
Wed Apr 22 18:05:11 CEST 2009


Hi Jacob,

2009/4/22 Jacob Holm <jh at improva.dk>:
> In other words, the ValueError in the following example is swallowed by close():
>
>
>>>> def foo():
> ...     yield 'bar'
> ...     raise ValueError('baz')
>>>> g = foo()
>>>> g.next()
> 'bar'
>>>> g.close()
>

I have the same code here, it does not raise an exception indeed.

> This looks like close() doesn't actually behave as it should according to PEP342.  Interesting...

>From PEP342 (Thanks Jacob, for the reference):

4. Add a close() method for generator-iterators, which raises
       GeneratorExit at the point where the generator was paused.  If
       the generator then raises StopIteration (by exiting normally, or
       due to already being closed) or GeneratorExit (by not catching
       the exception), close() returns to its caller.  If the generator
       yields a value, a RuntimeError is raised.  If the generator
       raises any other exception, it is propagated to the caller.
       close() does nothing if the generator has already exited due to
       an exception or normal exit.


> I would call that a bug.

I agree with that.

> I can't believe that none of us actually tested that...

Someone did!

Erik



More information about the Python-ideas mailing list