Commit 35ef9c19 authored by Georg Brandl's avatar Georg Brandl

Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter

which can be set to False to prevent the default delete-on-close
behavior.
parent 6c104f69
......@@ -53,7 +53,7 @@ The \var{dir}, \var{prefix} and \var{suffix} parameters are passed to
\begin{funcdesc}{NamedTemporaryFile}{\optional{mode=\code{'w+b'}\optional{,
bufsize=\code{-1}\optional{,
suffix\optional{, prefix\optional{,
dir}}}}}}
dir\optional{, delete}}}}}}}
This function operates exactly as \function{TemporaryFile()} does,
except that the file is guaranteed to have a visible name in the file
system (on \UNIX, the directory entry is not unlinked). That name can
......@@ -61,7 +61,10 @@ be retrieved from the \member{name} member of the file object. Whether
the name can be used to open the file a second time, while the
named temporary file is still open, varies across platforms (it can
be so used on \UNIX; it cannot on Windows NT or later).
If \var{delete} is true (the default), the file is deleted as soon as
it is closed.
\versionadded{2.3}
\versionadded[The \var{delete} parameter]{2.6}
\end{funcdesc}
\begin{funcdesc}{mkstemp}{\optional{suffix\optional{,
......
......@@ -372,10 +372,11 @@ class _TemporaryFileWrapper:
remove the file when it is no longer needed.
"""
def __init__(self, file, name):
def __init__(self, file, name, delete=True):
self.file = file
self.name = name
self.close_called = False
self.delete = delete
def __getattr__(self, name):
file = self.__dict__['file']
......@@ -400,23 +401,25 @@ class _TemporaryFileWrapper:
if not self.close_called:
self.close_called = True
self.file.close()
self.unlink(self.name)
if self.delete:
self.unlink(self.name)
def __del__(self):
self.close()
def NamedTemporaryFile(mode='w+b', bufsize=-1, suffix="",
prefix=template, dir=None):
prefix=template, dir=None, delete=True):
"""Create and return a temporary file.
Arguments:
'prefix', 'suffix', 'dir' -- as for mkstemp.
'mode' -- the mode argument to os.fdopen (default "w+b").
'bufsize' -- the buffer size argument to os.fdopen (default -1).
'delete' -- whether the file is deleted on close (default True).
The file is created as mkstemp() would do it.
Returns an object with a file-like interface; the name of the file
is accessible as file.name. The file will be automatically deleted
when it is closed.
when it is closed unless the 'delete' argument is set to False.
"""
if dir is None:
......@@ -429,12 +432,12 @@ def NamedTemporaryFile(mode='w+b', bufsize=-1, suffix="",
# Setting O_TEMPORARY in the flags causes the OS to delete
# the file when it is closed. This is only supported by Windows.
if _os.name == 'nt':
if _os.name == 'nt' and delete:
flags |= _os.O_TEMPORARY
(fd, name) = _mkstemp_inner(dir, prefix, suffix, flags)
file = _os.fdopen(fd, mode, bufsize)
return _TemporaryFileWrapper(file, name)
return _TemporaryFileWrapper(file, name, delete)
if _os.name != 'posix' or _os.sys.platform == 'cygwin':
# On non-POSIX and Cygwin systems, assume that we cannot unlink a file
......
......@@ -561,11 +561,12 @@ test_classes.append(test_mktemp)
class test_NamedTemporaryFile(TC):
"""Test NamedTemporaryFile()."""
def do_create(self, dir=None, pre="", suf=""):
def do_create(self, dir=None, pre="", suf="", delete=True):
if dir is None:
dir = tempfile.gettempdir()
try:
file = tempfile.NamedTemporaryFile(dir=dir, prefix=pre, suffix=suf)
file = tempfile.NamedTemporaryFile(dir=dir, prefix=pre, suffix=suf,
delete=delete)
except:
self.failOnException("NamedTemporaryFile")
......@@ -599,6 +600,22 @@ class test_NamedTemporaryFile(TC):
finally:
os.rmdir(dir)
def test_dis_del_on_close(self):
# Tests that delete-on-close can be disabled
dir = tempfile.mkdtemp()
tmp = None
try:
f = tempfile.NamedTemporaryFile(dir=dir, delete=False)
tmp = f.name
f.write('blat')
f.close()
self.failUnless(os.path.exists(f.name),
"NamedTemporaryFile %s missing after close" % f.name)
finally:
if tmp is not None:
os.unlink(tmp)
os.rmdir(dir)
def test_multiple_close(self):
# A NamedTemporaryFile can be closed many times without error
......
......@@ -430,6 +430,7 @@ Mike Meyer
Steven Miale
Trent Mick
Chad Miller
Damien Miller
Roman Milner
Dom Mitchell
Doug Moen
......
......@@ -168,6 +168,10 @@ Core and builtins
Library
-------
- Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
which can be set to False to prevent the default delete-on-close
behavior.
- Patch #1581073: add a flag to textwrap that prevents the dropping of
whitespace while wrapping.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment