[Python-Dev] Exception chaining and generator finalisation
Greg Ewing
greg.ewing at canterbury.ac.nz
Sun Aug 1 03:01:32 CEST 2010
While updating my yield-from impementation for Python
3.1.2, I came across a quirk in the way that the new
exception chaining feature interacts with generators.
If you close() a generator, and it raises an exception
inside a finally clause, you get a double-barrelled
traceback that first reports a GeneratorExit, then
"During handling of the above exception, another
exception occurred", followed by the traceback for
the exception raised by the generator.
To my mind, the fact that GeneratorExit is involved
is an implementation detail that shouldn't be leaking
through like this.
Does anyone think this ought to be fixed, and if so,
how? Should GeneratorExit be exempt from being
implicitly set as the context of another exception?
Should any other exceptions also be exempt?
Demonstration follows:
Python 3.1.2 (r312:79147, Jul 31 2010, 21:23:14)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def g():
... try:
... yield 1
... finally:
... raise ValueError("Spanish inquisition")
...
>>> gi = g()
>>> next(gi)
1
>>> gi.close()
Traceback (most recent call last):
File "<stdin>", line 3, in g
GeneratorExit
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in g
ValueError: Spanish inquisition
--
Greg
More information about the Python-Dev
mailing list