Request for clarification of PEP 380

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Request for clarification of PEP 380

Mark Shannon-3
Hi,

The scenario is this:
A generator, G, has a non-generator sub-iterator, S,
(ie G includes a "yield from S" experssion and S is not a generator)
and either G.close() or G.throw(GeneratorExit) is called.

In the current implementation, S.close() is called and,
if that call raises an exception, then that exception is suppressed.

Should close() be called at all? I know that it helps non-generators
to support the protocol, but there is the problem of iterables that
happen to have a close method. This may cause unwanted side effects.

Why is the exception suppressed?

The text of the PEP seems to implicitly assume that all sub-iterators
will be generators, so it is not clear on the above points.

Cheers,
Mark.
_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%2B1324100855712-1801473%40n6.nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: Request for clarification of PEP 380

Nick Coghlan
On Fri, Mar 9, 2012 at 12:06 AM, Mark Shannon <[hidden email]> wrote:
>
> The text of the PEP seems to implicitly assume that all sub-iterators
> will be generators, so it is not clear on the above points.

On the contrary, this question is explicitly addressed in the PEP:
http://www.python.org/dev/peps/pep-0380/#finalization

If you want to block an inadvertent close() call, you need to either
wrap the object so it doesn't implement unwanted parts of the
generator API (which is the iterator protocol plus send(), throw() and
close()), or else you should continue to use simple iteration rather
than delegation.

Cheers,
Nick.

--
Nick Coghlan   |   [hidden email]   |   Brisbane, Australia
_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%2B1324100855712-1801473%40n6.nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: Request for clarification of PEP 380

Mark Shannon-3
Nick Coghlan wrote:
> On Fri, Mar 9, 2012 at 12:06 AM, Mark Shannon <[hidden email]> wrote:
>> The text of the PEP seems to implicitly assume that all sub-iterators
>> will be generators, so it is not clear on the above points.
>
> On the contrary, this question is explicitly addressed in the PEP:
> http://www.python.org/dev/peps/pep-0380/#finalization

I should have read it more carefully.

>
> If you want to block an inadvertent close() call, you need to either
> wrap the object so it doesn't implement unwanted parts of the
> generator API (which is the iterator protocol plus send(), throw() and
> close()), or else you should continue to use simple iteration rather
> than delegation.
>

What about the exception being suppressed?
That doesn't seem to be mentioned.

Cheers,
Mark.

_______________________________________________
Python-Dev mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/lists%2B1324100855712-1801473%40n6.nabble.com