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