diff -r b5530669ef70 Lib/tempfile.py --- a/Lib/tempfile.py Wed Sep 04 20:52:14 2013 +0200 +++ b/Lib/tempfile.py Thu Sep 05 15:02:18 2013 +0300 @@ -326,15 +326,12 @@ """Temporary file wrapper This class provides a wrapper around files opened for - temporary use. In particular, it seeks to automatically - remove the file when it is no longer needed. + temporary use. """ - def __init__(self, file, name, delete=True): + def __init__(self, file, name): self.file = file self.name = name - self.close_called = False - self.delete = delete def __getattr__(self, name): # Attribute lookups are delegated to the underlying file @@ -356,36 +353,8 @@ def __iter__(self): return iter(self.file) - # NT provides delete-on-close as a primitive, so we don't need - # the wrapper to do anything special. We still use it so that - # file.name is useful (i.e. not "(fdopen)") with NamedTemporaryFile. - if _os.name != 'nt': - # Cache the unlinker so we don't get spurious errors at - # shutdown when the module-level "os" is None'd out. Note - # 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 - - def close(self): - if not self.close_called: - self.close_called = True - self.file.close() - if self.delete: - self.unlink(self.name) - - def __del__(self): - self.close() - - # Need to trap __exit__ as well to ensure the file gets - # deleted when used in a with statement - def __exit__(self, exc, value, tb): - result = self.file.__exit__(exc, value, tb) - self.close() - return result - else: - def __exit__(self, exc, value, tb): - self.file.__exit__(exc, value, tb) + def __exit__(self, exc, value, tb): + self.file.__exit__(exc, value, tb) def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, @@ -419,8 +388,18 @@ (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags) file = _io.open(fd, mode, buffering=buffering, newline=newline, encoding=encoding) + if _os.name != 'nt' and delete: + oldclose = file.close + def close(): + if not file.closed: + try: + oldclose() + finally: + _os.unlink(name) + file.close = oldclose + file.close = close - return _TemporaryFileWrapper(file, name, delete) + return _TemporaryFileWrapper(file, name) if _os.name != 'posix' or _os.sys.platform == 'cygwin': # On non-POSIX and Cygwin systems, assume that we cannot unlink a file