[Web-SIG] WSGI, PEP 340, and the close() method

Phillip J. Eby pje at telecommunity.com
Wed May 4 01:38:50 CEST 2005


FYI, Guido has a new PEP under discussion on Python-Dev: PEP 340.  Part of 
this PEP includes a new proposed mechanism for generator finalization that 
would eliminate PEP 325.

Unfortunately, because I originally thought PEP 325 was a shoo-in, I made 
WSGI use a 'close()' method, thinking it would be automatically 
forward-compatible once PEP 325 landed.  Now, however, it looks as though 
you'd have to do something like:

     if hasattr(retval,'__exit__'):
         try: retval.__exit__(StopIteration)
         except StopIteration: pass

In order to "close" a generator.

I don't know how important this actually is to PEP 333; I suspect that the 
number of times generators are being used as WSGI application objects is 
probably quite small, and of course reliance on this feature must currently 
be non-existent.

Guido suggested that a simple decorator around generators used as WSGI 
applications should suffice to implement this behavior, and he's 
right.  This wouldn't require any spec changes, except to clarify that 
generator finalization requires such a decorator, and to perhaps offer 
sample source for one.



More information about the Web-SIG mailing list