STINNER Victor added the comment:
On Python 3.5 compiled in debug mode (or probably with python -Wd when compiled
in release mode), I got this warning:
/home/haypo/prog/python/default/Lib/tempfile.py:708: ResourceWarning:
Implicitly cleaning up <TemporaryDirectory '/tmp/tmpcr8u3m8v'>
_warnings.warn(warn_message, ResourceWarning)
The script is really a corner case: the generator is garbage collected whereas
it is not done. At the same time, the TemporaryDirectory object is also garbage
collected. I guess that the exact order of object deletion is not reliable: the
generator may be deleted before or after the TemporaryDirectory.
TemporaryDirectory._cleanup() is called by the finalizer (_weakref.finalize) of
the TemporaryDirectory, which means that the TemporaryDirectory object is
garbage collected.
TemporaryDirectory.cleanup() is called indirectly by
TemporaryDirectory.__exit__().
I'm suprised that deleting a generator calls __exit__().
The following script has a more reliable behaviour:
TemporaryDirectory.__exit__() is called when the generator is deleted, and
TemporaryDirectory.cleanup() is not called.
---
import tempfile
import gc
def generator():
with tempfile.TemporaryDirectory():
print("before yield")
yield
print("after yield")
g = generator()
next(g)
g = None
gc.collect()
---
----------
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue22427>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com