Yield inside try...finally

Alan Kennedy alanmk at hotmail.com
Tue Jun 3 13:12:35 EDT 2003


Michael Sparks wrote:
> 
> I've been working with wrapping up network client functionality as a
> generator recently, and hit a "niceness"/"cleanness" speedbump.
> 
> What I'd *like* to do (which doesn't work) is:
> 
> try:
>    sock = socket(AF_INET, SOCK_STREAM);
>    try:
>       sock.setblocking(0); yield stage.done(0)
>       try:
>          sock.connect((self.host, self.port)); yield stage.done(1)
>          yield newComponent([self.setupCSA(sock)])
>          while self.waitCSAClose():
>             yield stage.current(2)
>          yield stage.done(2)
>       finally:
>          result = s.shutdown(1) ; yield stage.done(3)
>    finally:
>       sock.close() ; yield stage.done(4)
> except error, e:
>    # Would handle error more interestingly here
>    print stage.lastdone, e

Hope this doesn't offend your programming sensibilities ;-) I haven't decided
yet if it offends mine.

#------------------------------

class Finality(Exception): pass

try:                                                              
   sock = socket(AF_INET, SOCK_STREAM);                           
   try:                                                           
      sock.setblocking(0); yield stage.done(0)
      try:
         sock.connect((self.host, self.port)); yield stage.done(1)
         yield newComponent([self.setupCSA(sock)])
         while self.waitCSAClose():                               
            yield stage.current(2)                                
         yield stage.done(2)
         raise Finality
      except Finality:                                                    
         result = s.shutdown(1) ; yield stage.done(3)
         raise Finality
   except Finality:                                                       
      sock.close() ; yield stage.done(4)
      raise Finality
except error, e:
   # Would handle error more interestingly here
   print stage.lastdone, e
except Finality:
   pass




-- 
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan




More information about the Python-list mailing list