yield in try/finally case

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Mar 3 11:24:38 EST 2016


On 3 March 2016 at 15:12, Random832 <random832 at fastmail.com> wrote:
> 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

If you're happy letting __del__ close the file then why bother with
the context manager in the first place? By the reasoning above we
don't even need to close the file in __del__ since all open files get
closed at process exit anyway. The point is that finally/__exit__ are
not achieving what they are supposed to achieve: the guarantee that
the file is immediately closed when you're done with it.

Try your code under pypy and you'll probably find that your context
manager doesn't fire even at process exit.

--
Oscar



More information about the Python-list mailing list