[Python-Dev] Exception chaining and generator finalisation (original) (raw)

Nick Coghlan ncoghlan at gmail.com
Sun Aug 1 03:21:54 CEST 2010


On Sun, Aug 1, 2010 at 11:01 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:

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?

I don't see it as an implementation detail - it's part of the spec of generator finalisation in PEP 342 that GeneratorExit is thrown in to the incomplete generator at the point of the most recent yield. Trying to hide that doesn't benefit anybody.

SystemExit and KeyboardInterrupt behave the same way:

Python 3.2a0 (py3k:82729, Jul 9 2010, 20:26:08) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information.

import sys try: ... sys.exit(1) ... finally: ... raise RuntimeError("Ooops") ... Traceback (most recent call last): File "", line 2, in SystemExit: 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 4, in RuntimeError: Ooops

try: ... input("Hit Ctrl-C now") ... finally: ... raise RuntimeError("Ooops") ... Hit Ctrl-C nowTraceback (most recent call last): File "", line 2, in KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 4, in RuntimeError: Ooops

Cheers, Nick.

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



More information about the Python-Dev mailing list