yield in try/finally case
Random832
random832 at fastmail.com
Thu Mar 3 10:12:19 EST 2016
On Thu, Mar 3, 2016, at 08:47, Peter Otten wrote:
> This is because the last generator uf = upperfile(...) is not garbage-
> collected and wasn't explicitly closed either.
But the program hasn't ended yet when you run your assertion.
import sys
_open = open
files = []
def myclose(self):
print("--- closed " + self.name)
self._close()
def open(*args, **kw):
f = _open(*args, **kw)
f._close = f.close
f.close = lambda: myclose(f)
files.append(f)
return f
def upperfile(filename):
with open(filename) as f:
for line in f:
yield line.upper()
for uf in map(upperfile, sys.argv[1:]):
for line in uf:
print(line, end="")
break
print("--- end of program")
====
FOO
--- closed tmp1.txt
HAMS
--- end of program
--- closed tmp2.txt
More information about the Python-list
mailing list