[issue23700] tempfile.NamedTemporaryFile can close too early if used as iterator

Wolfgang Maier report at bugs.python.org
Fri Mar 20 15:38:16 CET 2015


Wolfgang Maier added the comment:

so let's look at this step-by-step (and I hope I fully understood this myself):

- calling fileobj.__iter__ creates a generator because the method uses yield from

- that generator does not get assigned to any reference so it will be garbage-collected

- when the generator is garbage-collected, the subgenerator specified to the rigth of the yield from is finalized (that is PEP380-mandated behavior) and, in this case, that is iter(self.file)

- for an io module-based fileobject, iter(f) is f and finalizing it means that its close method will be called

So this is not about the file object getting garbage-collected, it is about it getting closed.

Since PEP380 explicitly mentions that problem with yield from and a shared subiterator, I don't think you can call it a bug, but I think it is very problematic behavior as illustrated by this issue because client code is supposed to know whether a particular generator uses yield from or not.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23700>
_______________________________________


More information about the Python-bugs-list mailing list