[issue19077] More robust TemporaryDirectory cleanup

Richard Oudkerk report at bugs.python.org
Wed Sep 25 22:07:19 CEST 2013


Richard Oudkerk added the comment:

An alternative would be to use weakref.finalize() which would guarantee that cleanup happens before any purging occurs.  That would allow the use of shutil:

class TemporaryDirectory(object):
    def __init__(self, suffix="", prefix=template, dir=None):
        self.name = mkdtemp(suffix, prefix, dir)
        self._cleanup = weakref.finalize(self, shutil.rmtree, self.name)

    def __enter__(self):
        return self.name

    def __exit__(self, exc, value, tb):
        self._cleanup()

    def cleanup(self):
        self._cleanup()

----------
nosy: +sbt

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


More information about the Python-bugs-list mailing list