Destruction of generator objects

Kay Schluehr kay.schluehr at gmx.net
Sat Aug 11 04:02:45 EDT 2007


On Aug 9, 1:14 am, Stefan Bellon <sbel... at sbellon.de> wrote:
> On Wed, 08 Aug, MRAB wrote:
> > Simple! :-)
>
> Sorry, I forgot to mention that I am forced to using Python 2.4.
>
> --
> Stefan Bellon

It doesn't matter. You can use try...finally as well in Python 2.4.
It's just not possible to use except and finally clauses in one
statement such as:

try:
    1/0
except ZeroDivisionError:
    print "incident!"
finally:
    print "cleanup"

However in theory you don't really need finally but you can simulate
it using nested try...except statements:

-----

try:
    try:
        1/0
        try:
            print "cleanup"
        except Exception:
            raise FinallyException( sys.exc_info() )
    except ZeroDivisionError:
        print "incident!"
        try:
            print "cleanup"
        except Exception:
            raise FinallyException( sys.exc_info() )
except Exception:
    exc_cls, exc_value, exc_tb = sys.exc_info()
    if exc_cls == FinallyException:
        fin_exc_cls, fin_exc_value, fin_exc_tb = exc_value[0]
        raise fin_exc_cls, fin_exc_value, fin_exc_tb
    else:
        print "cleanup"
    except Exception:
        raise FinallyException( sys.exc_info() )
    raise exc_cls, exc_value, exc_tb

-------

Note that this expression is regenerated from the above
try...except..finally statement using Py25Lite ( see [1],[2] ) which
is a tool used to provide some Python 2.5 constructs for programmers
working with Python 2.4.

[1] http://www.fiber-space.de/EasyExtend/doc/EE.html
[2] http://www.fiber-space.de/EasyExtend/doc/Py25Lite/Py25Lite.html




More information about the Python-list mailing list