[Python-Dev] Combining the best of PEP 288 and PEP 325: generator exceptions and cleanup

Michael Sparks Michaels at rd.bbc.co.uk
Wed May 18 18:36:59 CEST 2005

On Wednesday 18 May 2005 17:39, Guido van Rossum wrote:
> I believe that in the discussion about PEP 343 vs. Nick's PEP 3XX
> (http://members.iinet.net.au/~ncoghlan/public/pep-3XX.html, still
> awaiting PEP moderator approval I believe?) the main difference is
> that Nick proposes a way to inject an exception into a generator; and
> I've said a few times that I like that idea.
> I'd like to propose to make that a separate PEP, which can combine
> elements of PEP 288 and PEP 325. Summary:
> - g.throw(type, value, traceback) causes the specified exception to be
> thrown at the place where the generator g is currently suspended. 
> - There's a new exception, GeneratorExit, which can be thrown to cause
> a generator to clean up. A generator should not yield a value in
> response to this exception.
> - g.close() throws a GeneratorExit exception in the generator, and
> catches it (so g.close() itself does not raise an exception).
> g.close() is idempotent -- if the generator is already closed, it is a
> no-op. If the generator, against the rules, yields another value, it
> is nevertheless marked closed.


We're currently using python generators to handle concurrency in a single 
thread, and this allows a simple scheduler to have a clean way of sending 
generators a generator shutdown message. (Currently we have to do it
another way that is specific to the style of wrapped generator)

If you need a volunteer to code this - should it go through, I'm willing to 
have a go at this. (I can't do this though for 3 weeks or so though due to a 
crunch at work, and I might be in over my head in offering this.)

Michael Sparks, Senior R&D Engineer, Digital Media Group
Michael.Sparks at rd.bbc.co.uk, http://kamaelia.sourceforge.net/
British Broadcasting Corporation, Research and Development
Kingswood Warren, Surrey KT20 6NP

This e-mail may contain personal views which are not the views of the BBC.

More information about the Python-Dev mailing list