[Python-ideas] Yield-From: Revamped expansion

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Mar 22 00:15:56 CET 2009


I'm thinking about replacing the expansion with the
following, which hopefully fixes a couple of concerns
that were raised recently without breaking anything else.

Can anyone see any remaining ways in which it doesn't
match the textual description in the Proposal section?

(It still isn't *quite* right, because it doesn't
distinguish between a GeneratorExit explicitly thrown
in and one resulting from calling close() on the
delegating generator. I may need to revise the text
and/or my implementation on that point, because I want
the inline-expansion interpretation to hold.)

     _i = iter(EXPR)
     try:
         _u = _i.next()
     except StopIteration, _e:
         _r = _e.value
     else:
         while 1:
             try:
                 _v = yield _u
             except GeneratorExit:
                 _m = getattr(_i, 'close', None)
                 if _m is not None:
                     _m()
                 raise
             except BaseException, _e:
                 _m = getattr(_i, 'throw', None)
                 if _m is not None:
                     _u = _m(_e)
                 else:
                     raise
             else:
                 try:
                     if _v is None:
                         _u = _i.next()
                     else:
                         _u = _i.send(_v)
                 except StopIteration, _e:
                     _r = _e.value
                     break
     RESULT = _r

-- 
Greg



More information about the Python-ideas mailing list