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