[Python-Dev] GeneratorExit inheriting from Exception
Nick Coghlan
ncoghlan at gmail.com
Sun Mar 26 04:59:29 CEST 2006
Guido van Rossum wrote:
> On 3/25/06, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> The kind of code I'm talking about would be an *existing* Python 2.4 generator
>> that happens to do something like:
>>
>> def gen(tasks):
>> """yield the results of a bunch of task functions"""
>> for task in tasks:
>> try:
>> yield (task, task())
>> except Exception, ex:
>> yield ExceptionOccurred(task, ex)
>
> This is purely hypothetical. It doesn't look like good style at all.
>
>> If you run such a generator on Python 2.5, but don't run it to completion
>> before it is garbage collected, you will get an error message printed on
>> stderr saying that an exception was ignored when this generator was cleaned
>> up. If you use the new PEP 342 features to try to explicitly close it before
>> it is garbage collected, you'll get the exception directly.
>
> I think this is fine. The code breaks with the new yield semantics.
> But that's because the except clause was overly broad. It's easy to
> rewrite it like this, which is better style anyway because the scope
> of the try/except is limited.
>
> try:
> value = (task, task())
> except Exception, ex:
> value = ExceptionOccurred(task, ex)
> yield value
Works for me. Consider the issue dropped :)
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
More information about the Python-Dev
mailing list