I'm stuck on python2.4, so I can't use a finally clause with generators or yield
. Is there any way to work around this?
I can't find any mentions of how to work around this limitation in python 2.4, and I'm not a big fan of the workarounds I've thought of (mainly involving __del__
and trying to make sure it runs within a reasonable time) aren't very appealing.
The only code that's guaranteed to be called when a generator instance is simply abandoned (garbage collected) are the
__del__
methods for its local variables (if no references to those objects exist outside) and the callbacks for weak references to its local variables (ditto). I recommend the weak reference route because it's non-invasive (you don't need a special class with a__del__
-- just anything that's weakly referenceable). E.g.:this does print
finis!
, as desired.You can duplicate code to avoid the finally block:
Becomes:
(I've not tried this on Python 2.4, you may have to look at sys.exc_info instead of the re-raise statement above, as in
raise sys.exc_info[0], sys.exc_info[1], sys.exc_info[2]
.)