[Python-ideas] Yield-From: Finalization guarantees

Nick Coghlan ncoghlan at gmail.com
Sun Mar 29 23:46:15 CEST 2009


Jacob Holm wrote:
> The problem is that any exception thrown into inner is converted to a
> GeneratorReturn, which is then swallowed by the yield-from instead of
> being reraised.

That actually only happens if inner *catches and suppresses* the thrown
in exception. Otherwise throw() will reraise the original exception
automatically:

>>> def gen():
...   try:
...     yield
...   except:
...     print "Suppressed"
...
>>> g = gen()
>>> g.next()
>>> g.throw(AssertionError)
Suppressed
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>> def gen():
...   try:
...     yield
...   finally:
...     print "Not suppressed"
...
>>> g = gen()
>>> g.next()
>>> g.throw(AssertionError)
Not suppressed
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in gen
AssertionError

Cheers,
Nick.

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



More information about the Python-ideas mailing list