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

Nick Coghlan ncoghlan at gmail.com
Sun Apr 19 16:04:15 CEST 2009


Jacob Holm wrote:
> First a minor nit.  There is no need to use getattr if the name is
> constant and you are going to catch AttributeError anyway.  Just use
> _i.close and _i.throw.

Yep, good idea.

> Next a more serious issue.  The current use of *sys.exc_info() in the
> throw handling is actually wrong.  Getting the "throw" attribute may run
> arbitrary python code which could easily replace the exception.

Good catch (and I agree with your first suggested fix of grabbing the
exception details before retrieving the method - the with statement
expansion in PEP 343 had to do something similar for similar reasons).

> Alternatively, we could get the signature of throw() fixed so that it
> matches the "raise" statement again.  Then we can drop the use of
> sys.exc_info(), and just use:
> 
>   _y = _m(_e)
> 
> But that is probably out of scope for this PEP.

Yep (using sys.exc_info() also translates more cleanly back to Python 2.x)

> FWIW I still consider an expansion using functools.partial to be more
> readable because it centralizes the StopIteration handling and reduces
> the code nesting.  Here is an updated version of such an expansion that
> is semantically equivalent to the PEP rev 13 + my suggested fix for the
> sys.exc_info() issue:

Nobody would ever implement it that way though - using partial like that
in the formal semantic definition implies a whole heap of temporary
objects that just won't exist in practice.

Better to use the more verbose expansion that is much closer in spirit
to the way it would actually be implemented.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------



More information about the Python-ideas mailing list