2002-08-10 00:14:33 +08:00
|
|
|
"""Temporary files.
|
2000-02-04 23:28:42 +08:00
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
This module provides generic, low- and high-level interfaces for
|
|
|
|
creating temporary files and directories. The interfaces listed
|
|
|
|
as "safe" just below can be used without fear of race conditions.
|
|
|
|
Those listed as "unsafe" cannot, and are provided for backward
|
|
|
|
compatibility only.
|
1991-11-12 23:38:08 +08:00
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
This module also provides some data items to the user:
|
1991-11-12 23:38:08 +08:00
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
TMP_MAX - maximum number of names that will be tried before
|
|
|
|
giving up.
|
|
|
|
template - the default prefix for all temporary names.
|
|
|
|
You may change this to control the default prefix.
|
|
|
|
tempdir - If this is set to a string before the first use of
|
|
|
|
any routine from this module, it will be considered as
|
|
|
|
another candidate location to store temporary files.
|
|
|
|
"""
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
"NamedTemporaryFile", "TemporaryFile", # high level safe interfaces
|
|
|
|
"mkstemp", "mkdtemp", # low level safe interfaces
|
|
|
|
"mktemp", # deprecated unsafe interface
|
|
|
|
"TMP_MAX", "gettempprefix", # constants
|
|
|
|
"tempdir", "gettempdir"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
# Imports.
|
|
|
|
|
|
|
|
import os as _os
|
|
|
|
import errno as _errno
|
|
|
|
from random import Random as _Random
|
|
|
|
|
|
|
|
if _os.name == 'mac':
|
2003-03-21 20:55:38 +08:00
|
|
|
import Carbon.Folder as _Folder
|
|
|
|
import Carbon.Folders as _Folders
|
2002-08-10 00:14:33 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
import fcntl as _fcntl
|
2004-07-19 07:58:17 +08:00
|
|
|
except ImportError:
|
2003-07-22 10:50:01 +08:00
|
|
|
def _set_cloexec(fd):
|
|
|
|
pass
|
|
|
|
else:
|
2002-08-10 00:14:33 +08:00
|
|
|
def _set_cloexec(fd):
|
2004-07-19 07:58:17 +08:00
|
|
|
try:
|
|
|
|
flags = _fcntl.fcntl(fd, _fcntl.F_GETFD, 0)
|
|
|
|
except IOError:
|
|
|
|
pass
|
2003-11-10 00:44:09 +08:00
|
|
|
else:
|
2002-08-10 00:14:33 +08:00
|
|
|
# flags read successfully, modify
|
|
|
|
flags |= _fcntl.FD_CLOEXEC
|
|
|
|
_fcntl.fcntl(fd, _fcntl.F_SETFD, flags)
|
2003-07-22 10:50:01 +08:00
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
import thread as _thread
|
2002-12-31 06:36:09 +08:00
|
|
|
except ImportError:
|
|
|
|
import dummy_thread as _thread
|
|
|
|
_allocate_lock = _thread.allocate_lock
|
2002-08-10 00:14:33 +08:00
|
|
|
|
|
|
|
_text_openflags = _os.O_RDWR | _os.O_CREAT | _os.O_EXCL
|
2002-08-10 02:01:01 +08:00
|
|
|
if hasattr(_os, 'O_NOINHERIT'):
|
|
|
|
_text_openflags |= _os.O_NOINHERIT
|
|
|
|
if hasattr(_os, 'O_NOFOLLOW'):
|
|
|
|
_text_openflags |= _os.O_NOFOLLOW
|
2002-08-10 00:14:33 +08:00
|
|
|
|
|
|
|
_bin_openflags = _text_openflags
|
2002-08-10 02:01:01 +08:00
|
|
|
if hasattr(_os, 'O_BINARY'):
|
|
|
|
_bin_openflags |= _os.O_BINARY
|
2002-08-10 00:14:33 +08:00
|
|
|
|
|
|
|
if hasattr(_os, 'TMP_MAX'):
|
|
|
|
TMP_MAX = _os.TMP_MAX
|
|
|
|
else:
|
|
|
|
TMP_MAX = 10000
|
|
|
|
|
2002-08-14 07:33:56 +08:00
|
|
|
template = "tmp"
|
2001-03-01 12:27:19 +08:00
|
|
|
|
1992-04-01 03:02:01 +08:00
|
|
|
tempdir = None
|
1992-01-15 02:31:56 +08:00
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
# Internal routines.
|
|
|
|
|
|
|
|
_once_lock = _allocate_lock()
|
|
|
|
|
2003-11-10 10:16:36 +08:00
|
|
|
if hasattr(_os, "lstat"):
|
|
|
|
_stat = _os.lstat
|
|
|
|
elif hasattr(_os, "stat"):
|
|
|
|
_stat = _os.stat
|
|
|
|
else:
|
|
|
|
# Fallback. All we need is something that raises os.error if the
|
|
|
|
# file doesn't exist.
|
|
|
|
def _stat(fn):
|
|
|
|
try:
|
|
|
|
f = open(fn)
|
|
|
|
except IOError:
|
|
|
|
raise _os.error
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def _exists(fn):
|
|
|
|
try:
|
|
|
|
_stat(fn)
|
|
|
|
except _os.error:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
class _RandomNameSequence:
|
|
|
|
"""An instance of _RandomNameSequence generates an endless
|
|
|
|
sequence of unpredictable strings which can safely be incorporated
|
|
|
|
into file names. Each string is six characters long. Multiple
|
|
|
|
threads can safely use the same instance at the same time.
|
|
|
|
|
|
|
|
_RandomNameSequence is an iterator."""
|
|
|
|
|
2002-11-21 23:59:59 +08:00
|
|
|
characters = ("abcdefghijklmnopqrstuvwxyz" +
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
|
|
|
"0123456789-_")
|
2002-08-10 00:14:33 +08:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.mutex = _allocate_lock()
|
|
|
|
self.rng = _Random()
|
|
|
|
self.normcase = _os.path.normcase
|
2002-11-21 23:59:59 +08:00
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
m = self.mutex
|
|
|
|
c = self.characters
|
2002-11-21 23:59:59 +08:00
|
|
|
choose = self.rng.choice
|
2002-01-29 07:11:23 +08:00
|
|
|
|
2002-11-21 23:59:59 +08:00
|
|
|
m.acquire()
|
1998-03-27 05:13:24 +08:00
|
|
|
try:
|
2002-11-21 23:59:59 +08:00
|
|
|
letters = [choose(c) for dummy in "123456"]
|
2002-08-10 00:14:33 +08:00
|
|
|
finally:
|
|
|
|
m.release()
|
|
|
|
|
2002-11-21 23:59:59 +08:00
|
|
|
return self.normcase(''.join(letters))
|
2002-08-10 00:14:33 +08:00
|
|
|
|
|
|
|
def _candidate_tempdir_list():
|
|
|
|
"""Generate a list of candidate temporary directories which
|
|
|
|
_get_default_tempdir will try."""
|
|
|
|
|
|
|
|
dirlist = []
|
|
|
|
|
|
|
|
# First, try the environment.
|
1997-08-13 02:00:12 +08:00
|
|
|
for envname in 'TMPDIR', 'TEMP', 'TMP':
|
2002-08-10 00:14:33 +08:00
|
|
|
dirname = _os.getenv(envname)
|
|
|
|
if dirname: dirlist.append(dirname)
|
|
|
|
|
|
|
|
# Failing that, try OS-specific locations.
|
|
|
|
if _os.name == 'mac':
|
1998-03-27 05:13:24 +08:00
|
|
|
try:
|
2003-03-21 20:55:38 +08:00
|
|
|
fsr = _Folder.FSFindFolder(_Folders.kOnSystemDisk,
|
|
|
|
_Folders.kTemporaryFolderType, 1)
|
|
|
|
dirname = fsr.as_pathname()
|
2002-08-10 00:14:33 +08:00
|
|
|
dirlist.append(dirname)
|
2003-03-21 20:55:38 +08:00
|
|
|
except _Folder.error:
|
2002-08-10 00:14:33 +08:00
|
|
|
pass
|
|
|
|
elif _os.name == 'riscos':
|
|
|
|
dirname = _os.getenv('Wimp$ScrapDir')
|
|
|
|
if dirname: dirlist.append(dirname)
|
|
|
|
elif _os.name == 'nt':
|
|
|
|
dirlist.extend([ r'c:\temp', r'c:\tmp', r'\temp', r'\tmp' ])
|
|
|
|
else:
|
|
|
|
dirlist.extend([ '/tmp', '/var/tmp', '/usr/tmp' ])
|
|
|
|
|
|
|
|
# As a last resort, the current directory.
|
|
|
|
try:
|
|
|
|
dirlist.append(_os.getcwd())
|
|
|
|
except (AttributeError, _os.error):
|
|
|
|
dirlist.append(_os.curdir)
|
|
|
|
|
|
|
|
return dirlist
|
2002-08-10 02:01:01 +08:00
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
def _get_default_tempdir():
|
|
|
|
"""Calculate the default directory to use for temporary files.
|
2002-08-17 22:50:24 +08:00
|
|
|
This routine should be called exactly once.
|
2002-08-10 00:14:33 +08:00
|
|
|
|
|
|
|
We determine whether or not a candidate temp dir is usable by
|
|
|
|
trying to create and write to a file in that directory. If this
|
|
|
|
is successful, the test file is deleted. To prevent denial of
|
|
|
|
service, the name of the test file must be randomized."""
|
|
|
|
|
|
|
|
namer = _RandomNameSequence()
|
|
|
|
dirlist = _candidate_tempdir_list()
|
|
|
|
flags = _text_openflags
|
|
|
|
|
|
|
|
for dir in dirlist:
|
|
|
|
if dir != _os.curdir:
|
|
|
|
dir = _os.path.normcase(_os.path.abspath(dir))
|
|
|
|
# Try only a few names per directory.
|
|
|
|
for seq in xrange(100):
|
|
|
|
name = namer.next()
|
|
|
|
filename = _os.path.join(dir, name)
|
|
|
|
try:
|
|
|
|
fd = _os.open(filename, flags, 0600)
|
|
|
|
fp = _os.fdopen(fd, 'w')
|
2001-01-15 11:26:36 +08:00
|
|
|
fp.write('blat')
|
|
|
|
fp.close()
|
2002-08-10 00:14:33 +08:00
|
|
|
_os.unlink(filename)
|
|
|
|
del fp, fd
|
|
|
|
return dir
|
|
|
|
except (OSError, IOError), e:
|
|
|
|
if e[0] != _errno.EEXIST:
|
|
|
|
break # no point trying more names in this directory
|
|
|
|
pass
|
|
|
|
raise IOError, (_errno.ENOENT,
|
|
|
|
("No usable temporary directory found in %s" % dirlist))
|
1992-04-01 03:02:01 +08:00
|
|
|
|
2002-08-17 22:50:24 +08:00
|
|
|
_name_sequence = None
|
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
def _get_candidate_names():
|
|
|
|
"""Common setup sequence for all user-callable interfaces."""
|
1992-04-01 03:02:01 +08:00
|
|
|
|
2002-08-17 22:50:24 +08:00
|
|
|
global _name_sequence
|
|
|
|
if _name_sequence is None:
|
|
|
|
_once_lock.acquire()
|
|
|
|
try:
|
|
|
|
if _name_sequence is None:
|
|
|
|
_name_sequence = _RandomNameSequence()
|
|
|
|
finally:
|
|
|
|
_once_lock.release()
|
2002-08-10 00:14:33 +08:00
|
|
|
return _name_sequence
|
|
|
|
|
|
|
|
|
|
|
|
def _mkstemp_inner(dir, pre, suf, flags):
|
|
|
|
"""Code common to mkstemp, TemporaryFile, and NamedTemporaryFile."""
|
|
|
|
|
|
|
|
names = _get_candidate_names()
|
|
|
|
|
|
|
|
for seq in xrange(TMP_MAX):
|
|
|
|
name = names.next()
|
|
|
|
file = _os.path.join(dir, pre + name + suf)
|
|
|
|
try:
|
|
|
|
fd = _os.open(file, flags, 0600)
|
|
|
|
_set_cloexec(fd)
|
2003-10-13 01:37:01 +08:00
|
|
|
return (fd, _os.path.abspath(file))
|
2002-08-10 00:14:33 +08:00
|
|
|
except OSError, e:
|
|
|
|
if e.errno == _errno.EEXIST:
|
|
|
|
continue # try again
|
|
|
|
raise
|
|
|
|
|
|
|
|
raise IOError, (_errno.EEXIST, "No usable temporary file name found")
|
2002-08-10 02:01:01 +08:00
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
|
|
|
|
# User visible interfaces.
|
1998-10-15 04:27:05 +08:00
|
|
|
|
1992-04-01 03:02:01 +08:00
|
|
|
def gettempprefix():
|
2002-08-10 00:14:33 +08:00
|
|
|
"""Accessor for tempdir.template."""
|
|
|
|
return template
|
|
|
|
|
2002-08-17 22:50:24 +08:00
|
|
|
tempdir = None
|
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
def gettempdir():
|
|
|
|
"""Accessor for tempdir.tempdir."""
|
2002-08-17 22:50:24 +08:00
|
|
|
global tempdir
|
|
|
|
if tempdir is None:
|
|
|
|
_once_lock.acquire()
|
|
|
|
try:
|
|
|
|
if tempdir is None:
|
|
|
|
tempdir = _get_default_tempdir()
|
|
|
|
finally:
|
|
|
|
_once_lock.release()
|
2002-08-10 00:14:33 +08:00
|
|
|
return tempdir
|
|
|
|
|
2002-08-17 22:50:24 +08:00
|
|
|
def mkstemp(suffix="", prefix=template, dir=None, text=False):
|
2002-08-14 23:41:26 +08:00
|
|
|
"""mkstemp([suffix, [prefix, [dir, [text]]]])
|
2002-08-10 00:14:33 +08:00
|
|
|
User-callable function to create and return a unique temporary
|
|
|
|
file. The return value is a pair (fd, name) where fd is the
|
|
|
|
file descriptor returned by os.open, and name is the filename.
|
|
|
|
|
|
|
|
If 'suffix' is specified, the file name will end with that suffix,
|
|
|
|
otherwise there will be no suffix.
|
|
|
|
|
|
|
|
If 'prefix' is specified, the file name will begin with that prefix,
|
|
|
|
otherwise a default prefix is used.
|
|
|
|
|
|
|
|
If 'dir' is specified, the file will be created in that directory,
|
|
|
|
otherwise a default directory is used.
|
|
|
|
|
2002-08-14 23:41:26 +08:00
|
|
|
If 'text' is specified and true, the file is opened in text
|
|
|
|
mode. Else (the default) the file is opened in binary mode. On
|
|
|
|
some operating systems, this makes no difference.
|
2002-08-10 00:14:33 +08:00
|
|
|
|
|
|
|
The file is readable and writable only by the creating user ID.
|
|
|
|
If the operating system uses permission bits to indicate whether a
|
|
|
|
file is executable, the file is executable by no one. The file
|
|
|
|
descriptor is not inherited by children of this process.
|
2001-01-13 11:04:02 +08:00
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
Caller is responsible for deleting the file when done with it.
|
2001-01-13 11:04:02 +08:00
|
|
|
"""
|
|
|
|
|
2002-08-17 22:50:24 +08:00
|
|
|
if dir is None:
|
|
|
|
dir = gettempdir()
|
|
|
|
|
2002-08-14 23:41:26 +08:00
|
|
|
if text:
|
2002-08-10 00:14:33 +08:00
|
|
|
flags = _text_openflags
|
2002-08-14 23:41:26 +08:00
|
|
|
else:
|
|
|
|
flags = _bin_openflags
|
1992-01-15 02:31:56 +08:00
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
return _mkstemp_inner(dir, prefix, suffix, flags)
|
1991-11-12 23:38:08 +08:00
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
|
2002-08-17 22:50:24 +08:00
|
|
|
def mkdtemp(suffix="", prefix=template, dir=None):
|
2002-08-10 00:14:33 +08:00
|
|
|
"""mkdtemp([suffix, [prefix, [dir]]])
|
|
|
|
User-callable function to create and return a unique temporary
|
|
|
|
directory. The return value is the pathname of the directory.
|
|
|
|
|
2002-08-14 23:41:26 +08:00
|
|
|
Arguments are as for mkstemp, except that the 'text' argument is
|
2002-08-10 00:14:33 +08:00
|
|
|
not accepted.
|
|
|
|
|
|
|
|
The directory is readable, writable, and searchable only by the
|
|
|
|
creating user.
|
|
|
|
|
|
|
|
Caller is responsible for deleting the directory when done with it.
|
|
|
|
"""
|
|
|
|
|
2002-08-17 22:50:24 +08:00
|
|
|
if dir is None:
|
|
|
|
dir = gettempdir()
|
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
names = _get_candidate_names()
|
2002-08-10 02:01:01 +08:00
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
for seq in xrange(TMP_MAX):
|
|
|
|
name = names.next()
|
|
|
|
file = _os.path.join(dir, prefix + name + suffix)
|
|
|
|
try:
|
|
|
|
_os.mkdir(file, 0700)
|
1998-03-27 05:13:24 +08:00
|
|
|
return file
|
2002-08-10 00:14:33 +08:00
|
|
|
except OSError, e:
|
|
|
|
if e.errno == _errno.EEXIST:
|
|
|
|
continue # try again
|
|
|
|
raise
|
1997-08-13 02:00:12 +08:00
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
raise IOError, (_errno.EEXIST, "No usable temporary directory name found")
|
1997-08-13 02:00:12 +08:00
|
|
|
|
2002-08-17 22:50:24 +08:00
|
|
|
def mktemp(suffix="", prefix=template, dir=None):
|
2002-08-10 00:14:33 +08:00
|
|
|
"""mktemp([suffix, [prefix, [dir]]])
|
|
|
|
User-callable function to return a unique temporary file name. The
|
|
|
|
file is not created.
|
1997-08-13 02:00:12 +08:00
|
|
|
|
2002-08-14 23:41:26 +08:00
|
|
|
Arguments are as for mkstemp, except that the 'text' argument is
|
2002-08-10 00:14:33 +08:00
|
|
|
not accepted.
|
|
|
|
|
|
|
|
This function is unsafe and should not be used. The file name
|
|
|
|
refers to a file that did not exist at some point, but by the time
|
|
|
|
you get around to creating it, someone else may have beaten you to
|
|
|
|
the punch.
|
1997-08-13 02:00:12 +08:00
|
|
|
"""
|
2001-12-19 06:32:40 +08:00
|
|
|
|
2002-11-22 23:56:29 +08:00
|
|
|
## from warnings import warn as _warn
|
|
|
|
## _warn("mktemp is a potential security risk to your program",
|
|
|
|
## RuntimeWarning, stacklevel=2)
|
2001-12-19 06:32:40 +08:00
|
|
|
|
2002-08-17 22:50:24 +08:00
|
|
|
if dir is None:
|
|
|
|
dir = gettempdir()
|
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
names = _get_candidate_names()
|
|
|
|
for seq in xrange(TMP_MAX):
|
|
|
|
name = names.next()
|
|
|
|
file = _os.path.join(dir, prefix + name + suffix)
|
2003-11-10 10:16:36 +08:00
|
|
|
if not _exists(file):
|
2002-08-10 00:14:33 +08:00
|
|
|
return file
|
|
|
|
|
|
|
|
raise IOError, (_errno.EEXIST, "No usable temporary filename found")
|
1997-08-13 02:00:12 +08:00
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
class _TemporaryFileWrapper:
|
|
|
|
"""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.
|
|
|
|
"""
|
1997-08-13 02:00:12 +08:00
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
def __init__(self, file, name):
|
|
|
|
self.file = file
|
|
|
|
self.name = name
|
2002-11-21 23:48:33 +08:00
|
|
|
self.close_called = False
|
1997-08-13 02:00:12 +08:00
|
|
|
|
|
|
|
def __getattr__(self, name):
|
1998-03-27 05:13:24 +08:00
|
|
|
file = self.__dict__['file']
|
|
|
|
a = getattr(file, name)
|
1999-06-02 02:55:36 +08:00
|
|
|
if type(a) != type(0):
|
|
|
|
setattr(self, name, a)
|
1998-03-27 05:13:24 +08:00
|
|
|
return a
|
1997-08-13 02:00:12 +08:00
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
# 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:
|
2002-11-21 23:48:33 +08:00
|
|
|
self.close_called = True
|
2002-08-10 00:14:33 +08:00
|
|
|
self.file.close()
|
|
|
|
self.unlink(self.name)
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
def NamedTemporaryFile(mode='w+b', bufsize=-1, suffix="",
|
2002-08-17 22:50:24 +08:00
|
|
|
prefix=template, dir=None):
|
2002-08-10 00:14:33 +08:00
|
|
|
"""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).
|
|
|
|
The file is created as mkstemp() would do it.
|
|
|
|
|
2005-01-11 23:33:03 +08:00
|
|
|
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.
|
2002-08-10 00:14:33 +08:00
|
|
|
"""
|
2001-01-12 18:02:46 +08:00
|
|
|
|
2002-08-17 22:50:24 +08:00
|
|
|
if dir is None:
|
|
|
|
dir = gettempdir()
|
|
|
|
|
2002-08-14 07:36:01 +08:00
|
|
|
if 'b' in mode:
|
|
|
|
flags = _bin_openflags
|
|
|
|
else:
|
|
|
|
flags = _text_openflags
|
2001-01-12 18:02:46 +08:00
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
# 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':
|
|
|
|
flags |= _os.O_TEMPORARY
|
2001-01-12 18:02:46 +08:00
|
|
|
|
2002-08-10 00:14:33 +08:00
|
|
|
(fd, name) = _mkstemp_inner(dir, prefix, suffix, flags)
|
|
|
|
file = _os.fdopen(fd, mode, bufsize)
|
|
|
|
return _TemporaryFileWrapper(file, name)
|
2001-01-12 18:02:46 +08:00
|
|
|
|
Patch #595014: Cygwin tempfile patch
Although Cygwin attempts to be as Posix compliant
as possible, it has difficulties unlinking open
files. This is not surprising given that Cygwin is
dependent on Win32 which in turn has this problem
itself.
The attached tempfile patch acknowledges this
Cygwin limitation. Without this patch, Cygwin
fails test_tempfile (i.e., test_has_no_name) as
follows:
$ ./python -E -tt ../Lib/test/regrtest.py -l test_tempfile
test_tempfile
test test_tempfile failed -- Traceback (most recent call last):
File "/home/jt/src/PythonCvs/Lib/test/test_tempfile.py", line 689, in test_has_no_name
self.failOnException("rmdir", ei)
File "/home/jt/src/PythonCvs/Lib/test/test_tempfile.py", line 33, in failOnException
self.fail("%s raised %s: %s" % (what, ei[0], ei[1]))
File "/home/jt/src/PythonCvs/Lib/unittest.py", line 260, in fail
raise self.failureException, msg
AssertionError: rmdir raised exceptions.OSError: [Errno 90] Directory not empty: '/mnt/c/DOCUME~1/jatis/LOCALS~1/Temp/tmpM_z8nj'
2002-08-14 23:10:09 +08:00
|
|
|
if _os.name != 'posix' or _os.sys.platform == 'cygwin':
|
|
|
|
# On non-POSIX and Cygwin systems, assume that we cannot unlink a file
|
|
|
|
# while it is open.
|
2002-08-10 00:14:33 +08:00
|
|
|
TemporaryFile = NamedTemporaryFile
|
2001-01-12 18:02:46 +08:00
|
|
|
|
|
|
|
else:
|
2002-08-10 00:14:33 +08:00
|
|
|
def TemporaryFile(mode='w+b', bufsize=-1, suffix="",
|
2002-08-17 22:50:24 +08:00
|
|
|
prefix=template, dir=None):
|
2002-08-10 00:14:33 +08:00
|
|
|
"""Create and return a temporary file.
|
|
|
|
Arguments:
|
|
|
|
'prefix', 'suffix', 'directory' -- as for mkstemp.
|
|
|
|
'mode' -- the mode argument to os.fdopen (default "w+b").
|
|
|
|
'bufsize' -- the buffer size argument to os.fdopen (default -1).
|
|
|
|
The file is created as mkstemp() would do it.
|
|
|
|
|
2005-01-11 23:33:03 +08:00
|
|
|
Returns an object with a file-like interface. The file has no
|
|
|
|
name, and will cease to exist when it is closed.
|
2002-08-10 00:14:33 +08:00
|
|
|
"""
|
|
|
|
|
2002-08-17 22:50:24 +08:00
|
|
|
if dir is None:
|
|
|
|
dir = gettempdir()
|
|
|
|
|
2002-08-14 07:36:01 +08:00
|
|
|
if 'b' in mode:
|
|
|
|
flags = _bin_openflags
|
|
|
|
else:
|
|
|
|
flags = _text_openflags
|
2002-08-10 00:14:33 +08:00
|
|
|
|
|
|
|
(fd, name) = _mkstemp_inner(dir, prefix, suffix, flags)
|
|
|
|
try:
|
|
|
|
_os.unlink(name)
|
|
|
|
return _os.fdopen(fd, mode, bufsize)
|
|
|
|
except:
|
|
|
|
_os.close(fd)
|
|
|
|
raise
|