This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients josh.r, oconnor663, vstinner
Date 2014-09-17.12:12:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1410955945.32.0.160126210638.issue22427@psf.upfronthosting.co.za>
In-reply-to
Content
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()
---
History
Date User Action Args
2014-09-17 12:12:25vstinnersetrecipients: + vstinner, josh.r, oconnor663
2014-09-17 12:12:25vstinnersetmessageid: <1410955945.32.0.160126210638.issue22427@psf.upfronthosting.co.za>
2014-09-17 12:12:25vstinnerlinkissue22427 messages
2014-09-17 12:12:25vstinnercreate