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

Discussion in 'Python' started by Adam Atlas, Jun 4, 2007.

  1. Adam Atlas

    Adam Atlas Guest

    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.
    Adam Atlas, Jun 4, 2007
    1. Advertisements

  2. Adam Atlas

    yuce Guest

    yuce, Jun 4, 2007
    1. Advertisements

  3. Adam Atlas

    Adam Atlas Guest

    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?
    Adam Atlas, Jun 4, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.