Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2....
File: tempfile.py
"""Temporary files.
[0] Fix | Delete
[1] Fix | Delete
This module provides generic, low- and high-level interfaces for
[2] Fix | Delete
creating temporary files and directories. All of the interfaces
[3] Fix | Delete
provided by this module can be used without fear of race conditions
[4] Fix | Delete
except for 'mktemp'. 'mktemp' is subject to race conditions and
[5] Fix | Delete
should not be used; it is provided for backward compatibility only.
[6] Fix | Delete
[7] Fix | Delete
This module also provides some data items to the user:
[8] Fix | Delete
[9] Fix | Delete
TMP_MAX - maximum number of names that will be tried before
[10] Fix | Delete
giving up.
[11] Fix | Delete
template - the default prefix for all temporary names.
[12] Fix | Delete
You may change this to control the default prefix.
[13] Fix | Delete
tempdir - If this is set to a string before the first use of
[14] Fix | Delete
any routine from this module, it will be considered as
[15] Fix | Delete
another candidate location to store temporary files.
[16] Fix | Delete
"""
[17] Fix | Delete
[18] Fix | Delete
__all__ = [
[19] Fix | Delete
"NamedTemporaryFile", "TemporaryFile", # high level safe interfaces
[20] Fix | Delete
"SpooledTemporaryFile",
[21] Fix | Delete
"mkstemp", "mkdtemp", # low level safe interfaces
[22] Fix | Delete
"mktemp", # deprecated unsafe interface
[23] Fix | Delete
"TMP_MAX", "gettempprefix", # constants
[24] Fix | Delete
"tempdir", "gettempdir"
[25] Fix | Delete
]
[26] Fix | Delete
[27] Fix | Delete
[28] Fix | Delete
# Imports.
[29] Fix | Delete
[30] Fix | Delete
import io as _io
[31] Fix | Delete
import os as _os
[32] Fix | Delete
import errno as _errno
[33] Fix | Delete
from random import Random as _Random
[34] Fix | Delete
[35] Fix | Delete
try:
[36] Fix | Delete
from cStringIO import StringIO as _StringIO
[37] Fix | Delete
except ImportError:
[38] Fix | Delete
from StringIO import StringIO as _StringIO
[39] Fix | Delete
[40] Fix | Delete
try:
[41] Fix | Delete
import fcntl as _fcntl
[42] Fix | Delete
except ImportError:
[43] Fix | Delete
def _set_cloexec(fd):
[44] Fix | Delete
pass
[45] Fix | Delete
else:
[46] Fix | Delete
def _set_cloexec(fd):
[47] Fix | Delete
try:
[48] Fix | Delete
flags = _fcntl.fcntl(fd, _fcntl.F_GETFD, 0)
[49] Fix | Delete
except IOError:
[50] Fix | Delete
pass
[51] Fix | Delete
else:
[52] Fix | Delete
# flags read successfully, modify
[53] Fix | Delete
flags |= _fcntl.FD_CLOEXEC
[54] Fix | Delete
_fcntl.fcntl(fd, _fcntl.F_SETFD, flags)
[55] Fix | Delete
[56] Fix | Delete
[57] Fix | Delete
try:
[58] Fix | Delete
import thread as _thread
[59] Fix | Delete
except ImportError:
[60] Fix | Delete
import dummy_thread as _thread
[61] Fix | Delete
_allocate_lock = _thread.allocate_lock
[62] Fix | Delete
[63] Fix | Delete
_text_openflags = _os.O_RDWR | _os.O_CREAT | _os.O_EXCL
[64] Fix | Delete
if hasattr(_os, 'O_NOINHERIT'):
[65] Fix | Delete
_text_openflags |= _os.O_NOINHERIT
[66] Fix | Delete
if hasattr(_os, 'O_NOFOLLOW'):
[67] Fix | Delete
_text_openflags |= _os.O_NOFOLLOW
[68] Fix | Delete
[69] Fix | Delete
_bin_openflags = _text_openflags
[70] Fix | Delete
if hasattr(_os, 'O_BINARY'):
[71] Fix | Delete
_bin_openflags |= _os.O_BINARY
[72] Fix | Delete
[73] Fix | Delete
if hasattr(_os, 'TMP_MAX'):
[74] Fix | Delete
TMP_MAX = _os.TMP_MAX
[75] Fix | Delete
else:
[76] Fix | Delete
TMP_MAX = 10000
[77] Fix | Delete
[78] Fix | Delete
template = "tmp"
[79] Fix | Delete
[80] Fix | Delete
# Internal routines.
[81] Fix | Delete
[82] Fix | Delete
_once_lock = _allocate_lock()
[83] Fix | Delete
[84] Fix | Delete
if hasattr(_os, "lstat"):
[85] Fix | Delete
_stat = _os.lstat
[86] Fix | Delete
elif hasattr(_os, "stat"):
[87] Fix | Delete
_stat = _os.stat
[88] Fix | Delete
else:
[89] Fix | Delete
# Fallback. All we need is something that raises os.error if the
[90] Fix | Delete
# file doesn't exist.
[91] Fix | Delete
def _stat(fn):
[92] Fix | Delete
try:
[93] Fix | Delete
f = open(fn)
[94] Fix | Delete
except IOError:
[95] Fix | Delete
raise _os.error
[96] Fix | Delete
f.close()
[97] Fix | Delete
[98] Fix | Delete
def _exists(fn):
[99] Fix | Delete
try:
[100] Fix | Delete
_stat(fn)
[101] Fix | Delete
except _os.error:
[102] Fix | Delete
return False
[103] Fix | Delete
else:
[104] Fix | Delete
return True
[105] Fix | Delete
[106] Fix | Delete
class _RandomNameSequence:
[107] Fix | Delete
"""An instance of _RandomNameSequence generates an endless
[108] Fix | Delete
sequence of unpredictable strings which can safely be incorporated
[109] Fix | Delete
into file names. Each string is six characters long. Multiple
[110] Fix | Delete
threads can safely use the same instance at the same time.
[111] Fix | Delete
[112] Fix | Delete
_RandomNameSequence is an iterator."""
[113] Fix | Delete
[114] Fix | Delete
characters = ("abcdefghijklmnopqrstuvwxyz" +
[115] Fix | Delete
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
[116] Fix | Delete
"0123456789_")
[117] Fix | Delete
[118] Fix | Delete
def __init__(self):
[119] Fix | Delete
self.mutex = _allocate_lock()
[120] Fix | Delete
self.normcase = _os.path.normcase
[121] Fix | Delete
[122] Fix | Delete
@property
[123] Fix | Delete
def rng(self):
[124] Fix | Delete
cur_pid = _os.getpid()
[125] Fix | Delete
if cur_pid != getattr(self, '_rng_pid', None):
[126] Fix | Delete
self._rng = _Random()
[127] Fix | Delete
self._rng_pid = cur_pid
[128] Fix | Delete
return self._rng
[129] Fix | Delete
[130] Fix | Delete
def __iter__(self):
[131] Fix | Delete
return self
[132] Fix | Delete
[133] Fix | Delete
def next(self):
[134] Fix | Delete
m = self.mutex
[135] Fix | Delete
c = self.characters
[136] Fix | Delete
choose = self.rng.choice
[137] Fix | Delete
[138] Fix | Delete
m.acquire()
[139] Fix | Delete
try:
[140] Fix | Delete
letters = [choose(c) for dummy in "123456"]
[141] Fix | Delete
finally:
[142] Fix | Delete
m.release()
[143] Fix | Delete
[144] Fix | Delete
return self.normcase(''.join(letters))
[145] Fix | Delete
[146] Fix | Delete
def _candidate_tempdir_list():
[147] Fix | Delete
"""Generate a list of candidate temporary directories which
[148] Fix | Delete
_get_default_tempdir will try."""
[149] Fix | Delete
[150] Fix | Delete
dirlist = []
[151] Fix | Delete
[152] Fix | Delete
# First, try the environment.
[153] Fix | Delete
for envname in 'TMPDIR', 'TEMP', 'TMP':
[154] Fix | Delete
dirname = _os.getenv(envname)
[155] Fix | Delete
if dirname: dirlist.append(dirname)
[156] Fix | Delete
[157] Fix | Delete
# Failing that, try OS-specific locations.
[158] Fix | Delete
if _os.name == 'riscos':
[159] Fix | Delete
dirname = _os.getenv('Wimp$ScrapDir')
[160] Fix | Delete
if dirname: dirlist.append(dirname)
[161] Fix | Delete
elif _os.name == 'nt':
[162] Fix | Delete
dirlist.extend([ r'c:\temp', r'c:\tmp', r'\temp', r'\tmp' ])
[163] Fix | Delete
else:
[164] Fix | Delete
dirlist.extend([ '/tmp', '/var/tmp', '/usr/tmp' ])
[165] Fix | Delete
[166] Fix | Delete
# As a last resort, the current directory.
[167] Fix | Delete
try:
[168] Fix | Delete
dirlist.append(_os.getcwd())
[169] Fix | Delete
except (AttributeError, _os.error):
[170] Fix | Delete
dirlist.append(_os.curdir)
[171] Fix | Delete
[172] Fix | Delete
return dirlist
[173] Fix | Delete
[174] Fix | Delete
def _get_default_tempdir():
[175] Fix | Delete
"""Calculate the default directory to use for temporary files.
[176] Fix | Delete
This routine should be called exactly once.
[177] Fix | Delete
[178] Fix | Delete
We determine whether or not a candidate temp dir is usable by
[179] Fix | Delete
trying to create and write to a file in that directory. If this
[180] Fix | Delete
is successful, the test file is deleted. To prevent denial of
[181] Fix | Delete
service, the name of the test file must be randomized."""
[182] Fix | Delete
[183] Fix | Delete
namer = _RandomNameSequence()
[184] Fix | Delete
dirlist = _candidate_tempdir_list()
[185] Fix | Delete
flags = _text_openflags
[186] Fix | Delete
[187] Fix | Delete
for dir in dirlist:
[188] Fix | Delete
if dir != _os.curdir:
[189] Fix | Delete
dir = _os.path.normcase(_os.path.abspath(dir))
[190] Fix | Delete
# Try only a few names per directory.
[191] Fix | Delete
for seq in xrange(100):
[192] Fix | Delete
name = namer.next()
[193] Fix | Delete
filename = _os.path.join(dir, name)
[194] Fix | Delete
try:
[195] Fix | Delete
fd = _os.open(filename, flags, 0o600)
[196] Fix | Delete
try:
[197] Fix | Delete
try:
[198] Fix | Delete
with _io.open(fd, 'wb', closefd=False) as fp:
[199] Fix | Delete
fp.write(b'blat')
[200] Fix | Delete
finally:
[201] Fix | Delete
_os.close(fd)
[202] Fix | Delete
finally:
[203] Fix | Delete
_os.unlink(filename)
[204] Fix | Delete
return dir
[205] Fix | Delete
except (OSError, IOError) as e:
[206] Fix | Delete
if e.args[0] == _errno.EEXIST:
[207] Fix | Delete
continue
[208] Fix | Delete
if (_os.name == 'nt' and e.args[0] == _errno.EACCES and
[209] Fix | Delete
_os.path.isdir(dir) and _os.access(dir, _os.W_OK)):
[210] Fix | Delete
# On windows, when a directory with the chosen name already
[211] Fix | Delete
# exists, EACCES error code is returned instead of EEXIST.
[212] Fix | Delete
continue
[213] Fix | Delete
break # no point trying more names in this directory
[214] Fix | Delete
raise IOError, (_errno.ENOENT,
[215] Fix | Delete
("No usable temporary directory found in %s" % dirlist))
[216] Fix | Delete
[217] Fix | Delete
_name_sequence = None
[218] Fix | Delete
[219] Fix | Delete
def _get_candidate_names():
[220] Fix | Delete
"""Common setup sequence for all user-callable interfaces."""
[221] Fix | Delete
[222] Fix | Delete
global _name_sequence
[223] Fix | Delete
if _name_sequence is None:
[224] Fix | Delete
_once_lock.acquire()
[225] Fix | Delete
try:
[226] Fix | Delete
if _name_sequence is None:
[227] Fix | Delete
_name_sequence = _RandomNameSequence()
[228] Fix | Delete
finally:
[229] Fix | Delete
_once_lock.release()
[230] Fix | Delete
return _name_sequence
[231] Fix | Delete
[232] Fix | Delete
[233] Fix | Delete
def _mkstemp_inner(dir, pre, suf, flags):
[234] Fix | Delete
"""Code common to mkstemp, TemporaryFile, and NamedTemporaryFile."""
[235] Fix | Delete
[236] Fix | Delete
names = _get_candidate_names()
[237] Fix | Delete
[238] Fix | Delete
for seq in xrange(TMP_MAX):
[239] Fix | Delete
name = names.next()
[240] Fix | Delete
file = _os.path.join(dir, pre + name + suf)
[241] Fix | Delete
try:
[242] Fix | Delete
fd = _os.open(file, flags, 0600)
[243] Fix | Delete
_set_cloexec(fd)
[244] Fix | Delete
return (fd, _os.path.abspath(file))
[245] Fix | Delete
except OSError, e:
[246] Fix | Delete
if e.errno == _errno.EEXIST:
[247] Fix | Delete
continue # try again
[248] Fix | Delete
if (_os.name == 'nt' and e.errno == _errno.EACCES and
[249] Fix | Delete
_os.path.isdir(dir) and _os.access(dir, _os.W_OK)):
[250] Fix | Delete
# On windows, when a directory with the chosen name already
[251] Fix | Delete
# exists, EACCES error code is returned instead of EEXIST.
[252] Fix | Delete
continue
[253] Fix | Delete
raise
[254] Fix | Delete
[255] Fix | Delete
raise IOError, (_errno.EEXIST, "No usable temporary file name found")
[256] Fix | Delete
[257] Fix | Delete
[258] Fix | Delete
# User visible interfaces.
[259] Fix | Delete
[260] Fix | Delete
def gettempprefix():
[261] Fix | Delete
"""Accessor for tempdir.template."""
[262] Fix | Delete
return template
[263] Fix | Delete
[264] Fix | Delete
tempdir = None
[265] Fix | Delete
[266] Fix | Delete
def gettempdir():
[267] Fix | Delete
"""Accessor for tempfile.tempdir."""
[268] Fix | Delete
global tempdir
[269] Fix | Delete
if tempdir is None:
[270] Fix | Delete
_once_lock.acquire()
[271] Fix | Delete
try:
[272] Fix | Delete
if tempdir is None:
[273] Fix | Delete
tempdir = _get_default_tempdir()
[274] Fix | Delete
finally:
[275] Fix | Delete
_once_lock.release()
[276] Fix | Delete
return tempdir
[277] Fix | Delete
[278] Fix | Delete
def mkstemp(suffix="", prefix=template, dir=None, text=False):
[279] Fix | Delete
"""User-callable function to create and return a unique temporary
[280] Fix | Delete
file. The return value is a pair (fd, name) where fd is the
[281] Fix | Delete
file descriptor returned by os.open, and name is the filename.
[282] Fix | Delete
[283] Fix | Delete
If 'suffix' is specified, the file name will end with that suffix,
[284] Fix | Delete
otherwise there will be no suffix.
[285] Fix | Delete
[286] Fix | Delete
If 'prefix' is specified, the file name will begin with that prefix,
[287] Fix | Delete
otherwise a default prefix is used.
[288] Fix | Delete
[289] Fix | Delete
If 'dir' is specified, the file will be created in that directory,
[290] Fix | Delete
otherwise a default directory is used.
[291] Fix | Delete
[292] Fix | Delete
If 'text' is specified and true, the file is opened in text
[293] Fix | Delete
mode. Else (the default) the file is opened in binary mode. On
[294] Fix | Delete
some operating systems, this makes no difference.
[295] Fix | Delete
[296] Fix | Delete
The file is readable and writable only by the creating user ID.
[297] Fix | Delete
If the operating system uses permission bits to indicate whether a
[298] Fix | Delete
file is executable, the file is executable by no one. The file
[299] Fix | Delete
descriptor is not inherited by children of this process.
[300] Fix | Delete
[301] Fix | Delete
Caller is responsible for deleting the file when done with it.
[302] Fix | Delete
"""
[303] Fix | Delete
[304] Fix | Delete
if dir is None:
[305] Fix | Delete
dir = gettempdir()
[306] Fix | Delete
[307] Fix | Delete
if text:
[308] Fix | Delete
flags = _text_openflags
[309] Fix | Delete
else:
[310] Fix | Delete
flags = _bin_openflags
[311] Fix | Delete
[312] Fix | Delete
return _mkstemp_inner(dir, prefix, suffix, flags)
[313] Fix | Delete
[314] Fix | Delete
[315] Fix | Delete
def mkdtemp(suffix="", prefix=template, dir=None):
[316] Fix | Delete
"""User-callable function to create and return a unique temporary
[317] Fix | Delete
directory. The return value is the pathname of the directory.
[318] Fix | Delete
[319] Fix | Delete
Arguments are as for mkstemp, except that the 'text' argument is
[320] Fix | Delete
not accepted.
[321] Fix | Delete
[322] Fix | Delete
The directory is readable, writable, and searchable only by the
[323] Fix | Delete
creating user.
[324] Fix | Delete
[325] Fix | Delete
Caller is responsible for deleting the directory when done with it.
[326] Fix | Delete
"""
[327] Fix | Delete
[328] Fix | Delete
if dir is None:
[329] Fix | Delete
dir = gettempdir()
[330] Fix | Delete
[331] Fix | Delete
names = _get_candidate_names()
[332] Fix | Delete
[333] Fix | Delete
for seq in xrange(TMP_MAX):
[334] Fix | Delete
name = names.next()
[335] Fix | Delete
file = _os.path.join(dir, prefix + name + suffix)
[336] Fix | Delete
try:
[337] Fix | Delete
_os.mkdir(file, 0700)
[338] Fix | Delete
return file
[339] Fix | Delete
except OSError, e:
[340] Fix | Delete
if e.errno == _errno.EEXIST:
[341] Fix | Delete
continue # try again
[342] Fix | Delete
if (_os.name == 'nt' and e.errno == _errno.EACCES and
[343] Fix | Delete
_os.path.isdir(dir) and _os.access(dir, _os.W_OK)):
[344] Fix | Delete
# On windows, when a directory with the chosen name already
[345] Fix | Delete
# exists, EACCES error code is returned instead of EEXIST.
[346] Fix | Delete
continue
[347] Fix | Delete
raise
[348] Fix | Delete
[349] Fix | Delete
raise IOError, (_errno.EEXIST, "No usable temporary directory name found")
[350] Fix | Delete
[351] Fix | Delete
def mktemp(suffix="", prefix=template, dir=None):
[352] Fix | Delete
"""User-callable function to return a unique temporary file name. The
[353] Fix | Delete
file is not created.
[354] Fix | Delete
[355] Fix | Delete
Arguments are as for mkstemp, except that the 'text' argument is
[356] Fix | Delete
not accepted.
[357] Fix | Delete
[358] Fix | Delete
This function is unsafe and should not be used. The file name
[359] Fix | Delete
refers to a file that did not exist at some point, but by the time
[360] Fix | Delete
you get around to creating it, someone else may have beaten you to
[361] Fix | Delete
the punch.
[362] Fix | Delete
"""
[363] Fix | Delete
[364] Fix | Delete
## from warnings import warn as _warn
[365] Fix | Delete
## _warn("mktemp is a potential security risk to your program",
[366] Fix | Delete
## RuntimeWarning, stacklevel=2)
[367] Fix | Delete
[368] Fix | Delete
if dir is None:
[369] Fix | Delete
dir = gettempdir()
[370] Fix | Delete
[371] Fix | Delete
names = _get_candidate_names()
[372] Fix | Delete
for seq in xrange(TMP_MAX):
[373] Fix | Delete
name = names.next()
[374] Fix | Delete
file = _os.path.join(dir, prefix + name + suffix)
[375] Fix | Delete
if not _exists(file):
[376] Fix | Delete
return file
[377] Fix | Delete
[378] Fix | Delete
raise IOError, (_errno.EEXIST, "No usable temporary filename found")
[379] Fix | Delete
[380] Fix | Delete
[381] Fix | Delete
class _TemporaryFileWrapper:
[382] Fix | Delete
"""Temporary file wrapper
[383] Fix | Delete
[384] Fix | Delete
This class provides a wrapper around files opened for
[385] Fix | Delete
temporary use. In particular, it seeks to automatically
[386] Fix | Delete
remove the file when it is no longer needed.
[387] Fix | Delete
"""
[388] Fix | Delete
[389] Fix | Delete
def __init__(self, file, name, delete=True):
[390] Fix | Delete
self.file = file
[391] Fix | Delete
self.name = name
[392] Fix | Delete
self.close_called = False
[393] Fix | Delete
self.delete = delete
[394] Fix | Delete
[395] Fix | Delete
def __getattr__(self, name):
[396] Fix | Delete
# Attribute lookups are delegated to the underlying file
[397] Fix | Delete
# and cached for non-numeric results
[398] Fix | Delete
# (i.e. methods are cached, closed and friends are not)
[399] Fix | Delete
file = self.__dict__['file']
[400] Fix | Delete
a = getattr(file, name)
[401] Fix | Delete
if not issubclass(type(a), type(0)):
[402] Fix | Delete
setattr(self, name, a)
[403] Fix | Delete
return a
[404] Fix | Delete
[405] Fix | Delete
# The underlying __enter__ method returns the wrong object
[406] Fix | Delete
# (self.file) so override it to return the wrapper
[407] Fix | Delete
def __enter__(self):
[408] Fix | Delete
self.file.__enter__()
[409] Fix | Delete
return self
[410] Fix | Delete
[411] Fix | Delete
# NT provides delete-on-close as a primitive, so we don't need
[412] Fix | Delete
# the wrapper to do anything special. We still use it so that
[413] Fix | Delete
# file.name is useful (i.e. not "(fdopen)") with NamedTemporaryFile.
[414] Fix | Delete
if _os.name != 'nt':
[415] Fix | Delete
# Cache the unlinker so we don't get spurious errors at
[416] Fix | Delete
# shutdown when the module-level "os" is None'd out. Note
[417] Fix | Delete
# that this must be referenced as self.unlink, because the
[418] Fix | Delete
# name TemporaryFileWrapper may also get None'd out before
[419] Fix | Delete
# __del__ is called.
[420] Fix | Delete
unlink = _os.unlink
[421] Fix | Delete
[422] Fix | Delete
def close(self):
[423] Fix | Delete
if not self.close_called:
[424] Fix | Delete
self.close_called = True
[425] Fix | Delete
try:
[426] Fix | Delete
self.file.close()
[427] Fix | Delete
finally:
[428] Fix | Delete
if self.delete:
[429] Fix | Delete
self.unlink(self.name)
[430] Fix | Delete
[431] Fix | Delete
def __del__(self):
[432] Fix | Delete
self.close()
[433] Fix | Delete
[434] Fix | Delete
# Need to trap __exit__ as well to ensure the file gets
[435] Fix | Delete
# deleted when used in a with statement
[436] Fix | Delete
def __exit__(self, exc, value, tb):
[437] Fix | Delete
result = self.file.__exit__(exc, value, tb)
[438] Fix | Delete
self.close()
[439] Fix | Delete
return result
[440] Fix | Delete
else:
[441] Fix | Delete
def __exit__(self, exc, value, tb):
[442] Fix | Delete
self.file.__exit__(exc, value, tb)
[443] Fix | Delete
[444] Fix | Delete
[445] Fix | Delete
def NamedTemporaryFile(mode='w+b', bufsize=-1, suffix="",
[446] Fix | Delete
prefix=template, dir=None, delete=True):
[447] Fix | Delete
"""Create and return a temporary file.
[448] Fix | Delete
Arguments:
[449] Fix | Delete
'prefix', 'suffix', 'dir' -- as for mkstemp.
[450] Fix | Delete
'mode' -- the mode argument to os.fdopen (default "w+b").
[451] Fix | Delete
'bufsize' -- the buffer size argument to os.fdopen (default -1).
[452] Fix | Delete
'delete' -- whether the file is deleted on close (default True).
[453] Fix | Delete
The file is created as mkstemp() would do it.
[454] Fix | Delete
[455] Fix | Delete
Returns an object with a file-like interface; the name of the file
[456] Fix | Delete
is accessible as its 'name' attribute. The file will be automatically
[457] Fix | Delete
deleted when it is closed unless the 'delete' argument is set to False.
[458] Fix | Delete
"""
[459] Fix | Delete
[460] Fix | Delete
if dir is None:
[461] Fix | Delete
dir = gettempdir()
[462] Fix | Delete
[463] Fix | Delete
if 'b' in mode:
[464] Fix | Delete
flags = _bin_openflags
[465] Fix | Delete
else:
[466] Fix | Delete
flags = _text_openflags
[467] Fix | Delete
[468] Fix | Delete
# Setting O_TEMPORARY in the flags causes the OS to delete
[469] Fix | Delete
# the file when it is closed. This is only supported by Windows.
[470] Fix | Delete
if _os.name == 'nt' and delete:
[471] Fix | Delete
flags |= _os.O_TEMPORARY
[472] Fix | Delete
[473] Fix | Delete
(fd, name) = _mkstemp_inner(dir, prefix, suffix, flags)
[474] Fix | Delete
try:
[475] Fix | Delete
file = _os.fdopen(fd, mode, bufsize)
[476] Fix | Delete
return _TemporaryFileWrapper(file, name, delete)
[477] Fix | Delete
except BaseException:
[478] Fix | Delete
_os.unlink(name)
[479] Fix | Delete
_os.close(fd)
[480] Fix | Delete
raise
[481] Fix | Delete
[482] Fix | Delete
if _os.name != 'posix' or _os.sys.platform == 'cygwin':
[483] Fix | Delete
# On non-POSIX and Cygwin systems, assume that we cannot unlink a file
[484] Fix | Delete
# while it is open.
[485] Fix | Delete
TemporaryFile = NamedTemporaryFile
[486] Fix | Delete
[487] Fix | Delete
else:
[488] Fix | Delete
def TemporaryFile(mode='w+b', bufsize=-1, suffix="",
[489] Fix | Delete
prefix=template, dir=None):
[490] Fix | Delete
"""Create and return a temporary file.
[491] Fix | Delete
Arguments:
[492] Fix | Delete
'prefix', 'suffix', 'dir' -- as for mkstemp.
[493] Fix | Delete
'mode' -- the mode argument to os.fdopen (default "w+b").
[494] Fix | Delete
'bufsize' -- the buffer size argument to os.fdopen (default -1).
[495] Fix | Delete
The file is created as mkstemp() would do it.
[496] Fix | Delete
[497] Fix | Delete
Returns an object with a file-like interface. The file has no
[498] Fix | Delete
name, and will cease to exist when it is closed.
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function