4

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.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
Richard Levasseur
  • 14,562
  • 6
  • 50
  • 63
  • Such an old question, while it applies to my current task: « `Python2.4.3 (#1, Oct 23 2012, (...) [GCC 4.1.2 20080704` » ! – Joël May 28 '19 at 08:02

2 Answers2

7

You can duplicate code to avoid the finally block:

try:
  yield 42
finally:
  do_something()

Becomes:

try:
  yield 42
except:  # bare except, catches *anything*
  do_something()
  raise  # re-raise same exception
do_something()

(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].)

3

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.:

import weakref

def gen():
  x = set()
  def finis(*_):
    print 'finis!'
  y = weakref.ref(x, finis)
  for i in range(99):
    yield i

for i in gen():
  if i>5: break

this does print finis!, as desired.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • Was/is it guaranteed in Python that the `y` local variable won't get cleaned up before the `x` local variable? If `y` is garbage collected first, then there is no weakref anymore by the time `x` is garbage collected, and the callback is never called. Of course this is still a great idea, it just requires storing the weak references elsewhere, and maybe extra logic in the weak reference callback to clean up its weak reference after it was called. – mtraceur Sep 02 '22 at 06:33