Yield inside try...finally
Alan Kennedy
alanmk at hotmail.com
Tue Jun 3 14:42:39 EDT 2003
Michael Sparks wrote:
> 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
>
OK, I think this one works now. I've coded and successfully run a dummy
version which reacts properly, even when exceptions are explicitly raised.
The code below obviously won't run, because the "yield"s are used outside of a
function definition. But it is a direct modification of the code you said you
would like to have.
You will still have to *guarantee* that the generator is resumed for this to
work.
#--------------------------------------------------
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 Exception, x:
result = s.shutdown(1) ; yield stage.done(3)
raise x
except Exception, x:
sock.close() ; yield stage.done(4)
raise x
except error, e:
# Would handle error more interestingly here
print stage.lastdone, e
except Finality:
pass
#--------------------------------------------------
fingers crossed behind back, alan hopes he won't embarass
himself again, and gives a nervous smilie :-}
--
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