diff -r 0fd72636de2b Lib/tempfile.py --- a/Lib/tempfile.py Fri Sep 20 23:28:27 2013 +0300 +++ b/Lib/tempfile.py Mon Sep 23 17:42:02 2013 +0300 @@ -28,9 +28,9 @@ # Imports. import warnings as _warnings -import sys as _sys import io as _io import os as _os +import shutil as _shutil import errno as _errno from random import Random as _Random @@ -337,10 +337,12 @@ remove the file when it is no longer needed. """ + file = None # Set here since __del__ checks it + close_called = False + def __init__(self, file, name, delete=True): self.file = file self.name = name - self.close_called = False self.delete = delete def __getattr__(self, name): @@ -372,10 +374,10 @@ # that this must be referenced as self.unlink, because the # name TemporaryFileWrapper may also get None'd out before # __del__ is called. - unlink = _os.unlink + unlink = staticmethod(_os.unlink) def close(self): - if not self.close_called: + if not self.close_called and self.file is not None: self.close_called = True self.file.close() if self.delete: @@ -625,9 +627,10 @@ in it are removed. """ + name = None # Handle mkdtemp raising an exception + def __init__(self, suffix="", prefix=template, dir=None): self._closed = False - self.name = None # Handle mkdtemp raising an exception self.name = mkdtemp(suffix, prefix, dir) def __repr__(self): @@ -636,60 +639,40 @@ def __enter__(self): return self.name - def cleanup(self, _warn=False): + def cleanup(self, _warn=False, _errors=(TypeError, AttributeError), + _ResourceWarning=ResourceWarning): if self.name and not self._closed: try: + _shutil.rmtree(self.name) + except _errors as ex: + if "None" not in '%s' % (ex,): + raise 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) + _warn("Implicitly cleaning up {!r}".format(self), + _ResourceWarning) def __exit__(self, exc, value, tb): self.cleanup() - def __del__(self): + def __del__(self, _warnings=_warnings): # Issue a ResourceWarning if implicit cleanup needed - self.cleanup(_warn=True) + self.cleanup(_warn=_warnings.warn) - # XXX (ncoghlan): The following code attempts to make - # this class tolerant of the module nulling out process - # that happens during CPython interpreter shutdown - # Alas, it doesn't actually manage it. See issue #10188 - _listdir = staticmethod(_os.listdir) - _path_join = staticmethod(_os.path.join) - _isdir = staticmethod(_os.path.isdir) - _islink = staticmethod(_os.path.islink) - _remove = staticmethod(_os.remove) - _rmdir = staticmethod(_os.rmdir) - _warn = _warnings.warn - - def _rmtree(self, path): + def _rmtree(self, path, _OSError=OSError, _sep=_os.path.sep, + _listdir=_os.listdir, _remove=_os.remove, _rmdir=_os.rmdir): # Essentially a stripped down version of shutil.rmtree. We can't # use globals because they may be None'ed out at shutdown. - for name in self._listdir(path): - fullname = self._path_join(path, name) - try: - isdir = self._isdir(fullname) and not self._islink(fullname) - except OSError: - isdir = False - if isdir: - self._rmtree(fullname) - else: + if path[:0] != '': # byte-like object + _sep = _sep.encode() + try: + for name in _listdir(path): + fullname = path + _sep + name try: - self._remove(fullname) - except OSError: - pass - try: - self._rmdir(path) - except OSError: + _remove(fullname) + except _OSError: + self._rmtree(fullname) + _rmdir(path) + except _OSError: pass diff -r 0fd72636de2b Lib/test/test_tempfile.py --- a/Lib/test/test_tempfile.py Fri Sep 20 23:28:27 2013 +0300 +++ b/Lib/test/test_tempfile.py Mon Sep 23 17:42:02 2013 +0300 @@ -1061,7 +1061,8 @@ self.nameCheck(tmp.name, dir, pre, suf) # Create a subdirectory and some files if recurse: - self.do_create(tmp.name, pre, suf, recurse-1) + d1 = self.do_create(tmp.name, pre, suf, recurse-1) + d1.name = None with open(os.path.join(tmp.name, "test.txt"), "wb") as f: f.write(b"Hello world!") return tmp @@ -1093,7 +1094,7 @@ def test_cleanup_with_symlink_to_a_directory(self): # cleanup() should not follow symlinks to directories (issue #12464) d1 = self.do_create() - d2 = self.do_create() + d2 = self.do_create(recurse=0) # Symlink d1/foo -> d2 os.symlink(d2.name, os.path.join(d1.name, "foo")) @@ -1123,53 +1124,28 @@ finally: os.rmdir(dir) - @unittest.expectedFailure # See issue #10188 def test_del_on_shutdown(self): # A TemporaryDirectory may be cleaned up during shutdown # Make sure it works with the relevant modules nulled out with self.do_create() as dir: - d = self.do_create(dir=dir) + d = self.do_create(dir=dir, recurse=3) # Mimic the nulling out of modules that # occurs during system shutdown modules = [os, os.path] if has_stat: modules.append(stat) - # Currently broken, so suppress the warning - # that is otherwise emitted on stdout - with support.captured_stderr() as err: - with NulledModules(*modules): - d.cleanup() - # Currently broken, so stop spurious exception by - # indicating the object has already been closed - d._closed = True - # And this assert will fail, as expected by the - # unittest decorator... - self.assertFalse(os.path.exists(d.name), - "TemporaryDirectory %s exists after cleanup" % d.name) - - def test_warnings_on_cleanup(self): - # Two kinds of warning on shutdown - # Issue 10888: may write to stderr if modules are nulled out - # ResourceWarning will be triggered by __del__ - with self.do_create() as dir: - if os.sep != '\\': - # Embed a backslash in order to make sure string escaping - # in the displayed error message is dealt with correctly - suffix = '\\check_backslash_handling' - else: - suffix = '' - d = self.do_create(dir=dir, suf=suffix) - - #Check for the Issue 10888 message - modules = [os, os.path] - if has_stat: - modules.append(stat) with support.captured_stderr() as err: with NulledModules(*modules): d.cleanup() - message = err.getvalue().replace('\\\\', '\\') - self.assertIn("while cleaning up", message) - self.assertIn(d.name, message) + # No warnings are emitted on stderr + self.assertFalse(err.getvalue()) + self.assertFalse(os.path.exists(d.name), + "TemporaryDirectory %s exists after cleanup" % d.name) + + def test_warnings_on_cleanup(self): + # ResourceWarning will be triggered by __del__ + with self.do_create() as dir: + d = self.do_create(dir=dir, recurse=3) # Check for the resource warning with support.check_warnings(('Implicitly', ResourceWarning), quiet=False):