Yield inside try...finally
Bryan
belred1 at yahoo.com
Tue Jun 3 20:23:51 EDT 2003
i thought i just read in another thread that you couldn't/shouldn't put
yield inside of the try block. does this thread contradict the other
thread? someone please explain.
thanks,
bryan
"Alan Kennedy" <alanmk at hotmail.com> wrote in message
news:3EDCEC1F.96CB0766 at hotmail.com...
> 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