[Python-Dev] GeneratorExit inheriting from Exception

Nick Coghlan ncoghlan at iinet.net.au
Sat Mar 18 13:53:28 CET 2006


Should GeneratorExit inherit from Exception or BaseException?

Currently, a generator that catches Exception and continues on to yield 
another value can't be closed properly (you get a runtime error pointing out 
that the generator ignored GeneratorExit).

The only decent reference I could find to it in the old PEP 348/352 
discussions is Guido writing [1]:

> when GeneratorExit or StopIteration
> reach the outer level of an app, it's a bug like all the others that
> bare 'except:' WANTS to catch.

(at that point in the conversation, I believe bare except was considered the 
equivalent of "except Exception:")

While I agree with what Guido says about GeneratorExit being a bug if it 
reaches the outer level of an app, it seems like a bit of a trap that a 
correctly written generator can't write "except Exception:" without preceding 
it with an "except GeneratorExit:" that reraises the exception. Isn't that 
exactly the idiom we're trying to get rid of for SystemExit and KeyboardInterrupt?

Regards,
Nick.

[1] http://mail.python.org/pipermail/python-dev/2005-August/055173.html
-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-Dev mailing list