diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2010-12-12 15:24:21 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2010-12-12 15:24:21 (GMT) |
commit | 6b22f3fa172c61c76077265cbf742bbcb67d7af2 (patch) | |
tree | 73cb339eca119bb37a20b93ae26431d923fcbfbd /Lib/tempfile.py | |
parent | 0e65cf0b6ab397f3f67c2b516af7b9fba05604bc (diff) | |
download | cpython-6b22f3fa172c61c76077265cbf742bbcb67d7af2.zip cpython-6b22f3fa172c61c76077265cbf742bbcb67d7af2.tar.gz cpython-6b22f3fa172c61c76077265cbf742bbcb67d7af2.tar.bz2 |
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Diffstat (limited to 'Lib/tempfile.py')
-rw-r--r-- | Lib/tempfile.py | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 7e03784..b28d91f 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -29,6 +29,8 @@ __all__ = [ # Imports. +import warnings as _warnings +import sys as _sys import io as _io import os as _os import errno as _errno @@ -617,24 +619,40 @@ class TemporaryDirectory(object): """ def __init__(self, suffix="", prefix=template, dir=None): - # cleanup() needs this and is called even when mkdtemp fails - self._closed = True - self.name = mkdtemp(suffix, prefix, dir) self._closed = False + self.name = None # Handle mkdtemp throwing an exception + self.name = mkdtemp(suffix, prefix, dir) + + def __repr__(self): + return "<{} {!r}>".format(self.__class__.__name__, self.name) def __enter__(self): return self.name - def cleanup(self): - if not self._closed: - self._rmtree(self.name) + def cleanup(self, _warn=False): + if self.name and not self._closed: + try: + self._rmtree(self.name) + except (TypeError, AttributeError) as ex: + # Issue #10188: Emit a warning on stderr + # if the directory could not be cleaned + # up due to missing globals + if "None" not in str(ex): + raise + print("ERROR: {!r} while cleaning up {!r}".format(ex, self,), + file=_sys.stderr) + return self._closed = True + if _warn: + self._warn("Implicitly cleaning up {!r}".format(self), + ResourceWarning) def __exit__(self, exc, value, tb): self.cleanup() - __del__ = cleanup - + def __del__(self): + # Issue a ResourceWarning if implicit cleanup needed + self.cleanup(_warn=True) # XXX (ncoghlan): The following code attempts to make # this class tolerant of the module nulling out process @@ -646,6 +664,7 @@ class TemporaryDirectory(object): _remove = staticmethod(_os.remove) _rmdir = staticmethod(_os.rmdir) _os_error = _os.error + _warn = _warnings.warn def _rmtree(self, path): # Essentially a stripped down version of shutil.rmtree. We can't |