[Python-ideas] Yield-From: Handling of GeneratorExit

Nick Coghlan ncoghlan at gmail.com
Thu Apr 2 13:50:07 CEST 2009

Greg Ewing wrote:
> I've had another idea about this. Suppose the close()
> method of a generator didn't complain about reaching
> a yield after GeneratorExit is raised, but simply
> raised it again, and continued doing so until either
> a return occured or an exception propagated out.
> Seems to me this couldn't do any harm to a well-
> behaved generator, since it has to be prepared to
> deal with a GeneratorExit arising from any of its
> yield points.
> Yield-from would then no longer have the potential
> to create broken generators, we wouldn't have to treat
> GeneratorExit differently from any other exception,
> and Jacob could have his subgenerators that return
> values when you close them.

I think I'd prefer to see some arbitrary limit (500 seems like a nice
round number) on the number of times that GeneratorExit would be thrown
before giving up and raising RuntimeError, just so truly broken
generators that suppressed GeneratorExit in an infinite loop would
eventually trigger an exception rather than just appearing to hang.

The basic idea seems sound though (Jacob's averager example really was
nicer than mine).


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

More information about the Python-ideas mailing list