Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103967
| From | Random832 <random832@fastmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: yield in try/finally case |
| Date | 2016-03-03 10:12 -0500 |
| Message-ID | <mailman.150.1457017942.20602.python-list@python.org> (permalink) |
| References | <84965b86-819b-4924-bca9-e82eed040606@googlegroups.com> <mailman.146.1457007243.20602.python-list@python.org> <de3f3492-063d-469c-8ad2-c93098335a2f@googlegroups.com> <nb9f9r$ngi$1@ger.gmane.org> |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
yield in try/finally case 刘琦帆 <lqf.txx@gmail.com> - 2016-03-03 03:52 -0800
Re: yield in try/finally case Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-03-03 12:13 +0000
Re: yield in try/finally case 刘琦帆 <lqf.txx@gmail.com> - 2016-03-03 04:52 -0800
Re: yield in try/finally case Peter Otten <__peter__@web.de> - 2016-03-03 14:47 +0100
Re: yield in try/finally case Random832 <random832@fastmail.com> - 2016-03-03 10:12 -0500
Re: yield in try/finally case Peter Otten <__peter__@web.de> - 2016-03-03 17:20 +0100
Re: yield in try/finally case Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-03-03 16:24 +0000
Re: yield in try/finally case Random832 <random832@fastmail.com> - 2016-03-03 10:00 -0500
Re: yield in try/finally case Steven D'Aprano <steve@pearwood.info> - 2016-03-04 02:57 +1100
csiph-web