summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2007-03-13 18:31:49 (GMT)
committerGeorg Brandl <georg@python.org>2007-03-13 18:31:49 (GMT)
commit35ef9c19fe0f66d9d68b41100e85edc9feb45b1e (patch)
tree2e199adeacb7b794ec18c9ea79e57ba58e659cd3
parent6c104f6906bc7203f39a15bcc8b3d3905c7333e8 (diff)
downloadcpython-35ef9c19fe0f66d9d68b41100e85edc9feb45b1e.zip
cpython-35ef9c19fe0f66d9d68b41100e85edc9feb45b1e.tar.gz
cpython-35ef9c19fe0f66d9d68b41100e85edc9feb45b1e.tar.bz2
Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
which can be set to False to prevent the default delete-on-close behavior.
-rw-r--r--Doc/lib/libtempfile.tex5
-rw-r--r--Lib/tempfile.py15
-rw-r--r--Lib/test/test_tempfile.py21
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS4
5 files changed, 37 insertions, 9 deletions
diff --git a/Doc/lib/libtempfile.tex b/Doc/lib/libtempfile.tex
index 9b4d848..cbecb1e 100644
--- a/Doc/lib/libtempfile.tex
+++ b/Doc/lib/libtempfile.tex
@@ -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{,
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index 7809552..ce03bb7 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -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
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py
index 2047a63..82f1ea3 100644
--- a/Lib/test/test_tempfile.py
+++ b/Lib/test/test_tempfile.py
@@ -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
diff --git a/Misc/ACKS b/Misc/ACKS
index 177e524..e110632 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -430,6 +430,7 @@ Mike Meyer
Steven Miale
Trent Mick
Chad Miller
+Damien Miller
Roman Milner
Dom Mitchell
Doug Moen
diff --git a/Misc/NEWS b/Misc/NEWS
index 52c3c81..d03c4b5 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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.