[Python-Dev] patch: try/finally in generators
Neil Schemenauer
nas@python.ca
Mon, 29 Jul 2002 13:41:12 -0700
I wrote:
> The proposed patch is not correct since it doesn't handle "finally"
> code that creates a new reference to the generator.
It looks like that's not actually a problem since you can't get a hold
of a reference to the generator. However, here's another bit of
nastiness:
$ cat > bad.py
import sys
import gc
def g():
global gen
self = gen
try:
yield 1
finally:
gen = self
gen = g()
gen.next()
del gen
gc.collect()
print gen
$ ./python bad.py
Segmentation fault (core dumped)
Basically, the GC has to be taught that generators can have finalizers
and it may not be safe to collect them. If we allow try/finally in
generators then they can cause uncollectible garbage. It's not a show
stopper but something else to take into consideration.
Neil