468,288 Members | 1,989 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,288 developers. It's quick & easy.

`yield` in a `try/finally` block, pre-Python 2.5

I'm trying to emulate the Python 2.5 behaviour (PEP 342) of generator
functions where the `yield` statement is in a `try/finally` block.
Basically, where the `finally` block is guaranteed to run even if the
generator doesn't finish running: it simply runs when the generator is
garbage-collected. Does anyone know a good way of doing this? I'm
looking to see if there's a way to bring about an exception in another
frame in pure Python, but I haven't found anything yet.

Jun 4 '07 #1
2 2200

I had the same problem, you can see: http://aspn.activestate.com/ASPN/Coo.../Recipe/130004
for a solution.

Happy hacking,

Yuce

On Jun 4, 8:23 am, Adam Atlas <a...@atlas.stwrote:
I'm trying to emulate the Python 2.5 behaviour (PEP 342) of generator
functions where the `yield` statement is in a `try/finally` block.
Basically, where the `finally` block is guaranteed to run even if the
generator doesn't finish running: it simply runs when the generator is
garbage-collected. Does anyone know a good way of doing this? I'm
looking to see if there's a way to bring about an exception in another
frame in pure Python, but I haven't found anything yet.

Jun 4 '07 #2
On Jun 4, 1:49 am, yuce <yucete...@gmail.comwrote:
I had the same problem, you can see:http://aspn.activestate.com/ASPN/Coo.../Recipe/130004
for a solution.

Happy hacking,

Yuce
Thanks. I thought of doing something like that, but in my program,
it's important that the order follow the actual nesting order. That
is, I have a few nested generator each of which has its own 'finally',
and I need the innermost ones to run first. How can I deal with that?

Jun 4 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Brian Alexander | last post: by
11 posts views Thread by Pohihihi | last post: by
2 posts views Thread by Abubakar | last post: by
23 posts views Thread by VB Programmer | last post: by
4 posts views Thread by Cameron Frasnelly | last post: by
18 posts views Thread by Simon | last post: by
7 posts views Thread by John Dann | last post: by
5 posts views Thread by Morten Snedker | last post: by
3 posts views Thread by Jeff | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.