Destruction of generator objects

Alex Martelli aleax at mac.com
Sat Aug 11 01:36:16 EDT 2007


Stefan Bellon <sbellon at sbellon.de> wrote:

> On Thu, 09 Aug, Graham Dumpleton wrote:
> 
> >     result = application(environ, start_response)
> >     try:
> >         for data in result:
> >             if data:    # don't send headers until body appears
> >                 write(data)
> >         if not headers_sent:
> >             write('')   # send headers now if body was empty
> >     finally:
> >         if hasattr(result,'close'):
> >             result.close()
> 
> Hm, not what I hoped for ...
> 
> Isn't it possible to add some __del__ method to the generator object
> via some decorator or somehow else in a way that works even with Python
> 2.4 and can then be nicely written without cluttering up the logic
> between consumer and producer?

No, you cannot do what you want in Python 2.4.  If you can't upgrade to
2.5 or better, whatever the reason may be, you will have to live with
2.4's limitations (there ARE reasons we keep making new releases, after
all...:-).


Alex



More information about the Python-list mailing list