[Python-Dev] yield back-and-forth?
Nick Coghlan
ncoghlan at gmail.com
Sat Jan 21 05:55:19 CET 2006
Nick Coghlan wrote:
> Exception propagation is a different story. What do you want to propagate? All
> exceptions from the body of the for loop? Or just those from the yield statement?
>
> Well, isn't factoring out exception processing part of what PEP 343 is for?
> # We can even limit the propagated exceptions to those
> # from the outside world and leave the rest alone
> def main_generator():
> ...
> gen = sub_generator()
> for value in gen:
> with throw_to(gen):
> input = yield value
> continue input
A point I should have made here (but didn't, despite using it in a later
example) is that the "with" versions don't allow the nested generator to
suppress the exception.
A small refinement to gen.throw() that makes the first argument optional
(similar to the raise statement itself) would make it straightforward to allow
the generator to suppress passed in exceptions:
def main_generator():
...
gen = sub_generator()
for value in gen:
try:
input = yield value
except:
gen.throw() # Default to sys.exc_info(), just like raise
continue input
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
More information about the Python-Dev
mailing list