lib64
/
python3.8
/
Go to Home Directory
+
Upload
Create File
root@0UT1S:~$
Execute
By Order of Mr.0UT1S
[DIR] ..
N/A
[DIR] __pycache__
N/A
[DIR] asyncio
N/A
[DIR] collections
N/A
[DIR] concurrent
N/A
[DIR] config-3.8-x86_64-linux-gnu
N/A
[DIR] ctypes
N/A
[DIR] curses
N/A
[DIR] dbm
N/A
[DIR] distutils
N/A
[DIR] email
N/A
[DIR] encodings
N/A
[DIR] ensurepip
N/A
[DIR] html
N/A
[DIR] http
N/A
[DIR] importlib
N/A
[DIR] json
N/A
[DIR] lib-dynload
N/A
[DIR] lib2to3
N/A
[DIR] logging
N/A
[DIR] multiprocessing
N/A
[DIR] pydoc_data
N/A
[DIR] site-packages
N/A
[DIR] sqlite3
N/A
[DIR] tkinter
N/A
[DIR] turtledemo
N/A
[DIR] unittest
N/A
[DIR] urllib
N/A
[DIR] venv
N/A
[DIR] wsgiref
N/A
[DIR] xml
N/A
[DIR] xmlrpc
N/A
LICENSE.txt
13.61 KB
Rename
Delete
__future__.py
5.03 KB
Rename
Delete
__phello__.foo.py
64 bytes
Rename
Delete
_bootlocale.py
1.76 KB
Rename
Delete
_collections_abc.py
25.49 KB
Rename
Delete
_compat_pickle.py
8.54 KB
Rename
Delete
_compression.py
5.21 KB
Rename
Delete
_dummy_thread.py
5.89 KB
Rename
Delete
_markupbase.py
14.26 KB
Rename
Delete
_osx_support.py
21.26 KB
Rename
Delete
_py_abc.py
6.04 KB
Rename
Delete
_pydecimal.py
223.31 KB
Rename
Delete
_pyio.py
90.99 KB
Rename
Delete
_sitebuiltins.py
3.04 KB
Rename
Delete
_strptime.py
24.68 KB
Rename
Delete
_sysconfigdata__linux_x86_64-linux-gnu.py
37.61 KB
Rename
Delete
_sysconfigdata_d_linux_x86_64-linux-gnu.py
37.34 KB
Rename
Delete
_threading_local.py
7.05 KB
Rename
Delete
_weakrefset.py
5.60 KB
Rename
Delete
abc.py
4.38 KB
Rename
Delete
aifc.py
32.04 KB
Rename
Delete
antigravity.py
477 bytes
Rename
Delete
argparse.py
93.76 KB
Rename
Delete
ast.py
18.78 KB
Rename
Delete
asynchat.py
11.06 KB
Rename
Delete
asyncore.py
19.62 KB
Rename
Delete
base64.py
19.90 KB
Rename
Delete
bdb.py
31.30 KB
Rename
Delete
binhex.py
13.63 KB
Rename
Delete
bisect.py
2.16 KB
Rename
Delete
bz2.py
12.26 KB
Rename
Delete
cProfile.py
6.85 KB
Rename
Delete
calendar.py
24.25 KB
Rename
Delete
cgi.py
33.14 KB
Rename
Delete
cgitb.py
11.81 KB
Rename
Delete
chunk.py
5.31 KB
Rename
Delete
cmd.py
14.51 KB
Rename
Delete
code.py
10.37 KB
Rename
Delete
codecs.py
35.81 KB
Rename
Delete
codeop.py
6.18 KB
Rename
Delete
colorsys.py
3.97 KB
Rename
Delete
compileall.py
13.36 KB
Rename
Delete
configparser.py
53.10 KB
Rename
Delete
contextlib.py
24.41 KB
Rename
Delete
contextvars.py
129 bytes
Rename
Delete
copy.py
8.46 KB
Rename
Delete
copyreg.py
6.97 KB
Rename
Delete
crypt.py
3.53 KB
Rename
Delete
csv.py
15.77 KB
Rename
Delete
dataclasses.py
48.80 KB
Rename
Delete
datetime.py
86.22 KB
Rename
Delete
decimal.py
320 bytes
Rename
Delete
difflib.py
82.09 KB
Rename
Delete
dis.py
20.09 KB
Rename
Delete
doctest.py
102.09 KB
Rename
Delete
dummy_threading.py
2.75 KB
Rename
Delete
enum.py
37.24 KB
Rename
Delete
filecmp.py
9.60 KB
Rename
Delete
fileinput.py
14.36 KB
Rename
Delete
fnmatch.py
3.98 KB
Rename
Delete
formatter.py
14.79 KB
Rename
Delete
fractions.py
23.76 KB
Rename
Delete
ftplib.py
34.31 KB
Rename
Delete
functools.py
36.53 KB
Rename
Delete
genericpath.py
4.86 KB
Rename
Delete
getopt.py
7.31 KB
Rename
Delete
getpass.py
5.85 KB
Rename
Delete
gettext.py
26.50 KB
Rename
Delete
glob.py
5.56 KB
Rename
Delete
gzip.py
20.91 KB
Rename
Delete
hashlib.py
8.14 KB
Rename
Delete
heapq.py
22.34 KB
Rename
Delete
hmac.py
7.67 KB
Rename
Delete
imaplib.py
52.35 KB
Rename
Delete
imghdr.py
3.72 KB
Rename
Delete
imp.py
10.29 KB
Rename
Delete
inspect.py
115.77 KB
Rename
Delete
io.py
3.46 KB
Rename
Delete
ipaddress.py
69.96 KB
Rename
Delete
keyword.py
945 bytes
Rename
Delete
linecache.py
5.21 KB
Rename
Delete
locale.py
76.36 KB
Rename
Delete
lzma.py
12.68 KB
Rename
Delete
mailbox.py
76.82 KB
Rename
Delete
mailcap.py
8.85 KB
Rename
Delete
mimetypes.py
21.16 KB
Rename
Delete
modulefinder.py
23.86 KB
Rename
Delete
netrc.py
5.44 KB
Rename
Delete
nntplib.py
42.25 KB
Rename
Delete
ntpath.py
27.08 KB
Rename
Delete
nturl2path.py
2.82 KB
Rename
Delete
numbers.py
10.00 KB
Rename
Delete
opcode.py
5.67 KB
Rename
Delete
operator.py
10.46 KB
Rename
Delete
optparse.py
58.95 KB
Rename
Delete
os.py
38.08 KB
Rename
Delete
pathlib.py
51.38 KB
Rename
Delete
pdb.py
61.27 KB
Rename
Delete
pickle.py
62.96 KB
Rename
Delete
pickletools.py
91.29 KB
Rename
Delete
pipes.py
8.71 KB
Rename
Delete
pkgutil.py
21.00 KB
Rename
Delete
platform.py
39.48 KB
Rename
Delete
plistlib.py
31.46 KB
Rename
Delete
poplib.py
14.72 KB
Rename
Delete
posixpath.py
15.26 KB
Rename
Delete
pprint.py
20.98 KB
Rename
Delete
profile.py
22.99 KB
Rename
Delete
pstats.py
26.70 KB
Rename
Delete
pty.py
4.69 KB
Rename
Delete
py_compile.py
8.01 KB
Rename
Delete
pyclbr.py
14.90 KB
Rename
Delete
pydoc.py
104.20 KB
Rename
Delete
queue.py
11.09 KB
Rename
Delete
quopri.py
7.08 KB
Rename
Delete
random.py
28.13 KB
Rename
Delete
re.py
15.49 KB
Rename
Delete
reprlib.py
5.14 KB
Rename
Delete
rlcompleter.py
6.93 KB
Rename
Delete
runpy.py
11.77 KB
Rename
Delete
sched.py
6.29 KB
Rename
Delete
secrets.py
1.99 KB
Rename
Delete
selectors.py
18.13 KB
Rename
Delete
shelve.py
8.33 KB
Rename
Delete
shlex.py
13.01 KB
Rename
Delete
shutil.py
50.55 KB
Rename
Delete
signal.py
2.22 KB
Rename
Delete
site.py
21.33 KB
Rename
Delete
smtpd.py
33.90 KB
Rename
Delete
smtplib.py
43.95 KB
Rename
Delete
sndhdr.py
6.93 KB
Rename
Delete
socket.py
34.42 KB
Rename
Delete
socketserver.py
26.66 KB
Rename
Delete
sre_compile.py
26.07 KB
Rename
Delete
sre_constants.py
6.99 KB
Rename
Delete
sre_parse.py
39.29 KB
Rename
Delete
ssl.py
49.57 KB
Rename
Delete
stat.py
5.36 KB
Rename
Delete
statistics.py
38.76 KB
Rename
Delete
string.py
10.29 KB
Rename
Delete
stringprep.py
12.61 KB
Rename
Delete
struct.py
257 bytes
Rename
Delete
subprocess.py
76.42 KB
Rename
Delete
sunau.py
17.94 KB
Rename
Delete
symbol.py
2.06 KB
Rename
Delete
symtable.py
7.83 KB
Rename
Delete
sysconfig.py
24.31 KB
Rename
Delete
tabnanny.py
11.14 KB
Rename
Delete
tarfile.py
103.98 KB
Rename
Delete
telnetlib.py
22.71 KB
Rename
Delete
tempfile.py
26.89 KB
Rename
Delete
textwrap.py
18.95 KB
Rename
Delete
this.py
1003 bytes
Rename
Delete
threading.py
49.63 KB
Rename
Delete
timeit.py
13.16 KB
Rename
Delete
token.py
2.31 KB
Rename
Delete
tokenize.py
25.24 KB
Rename
Delete
trace.py
29.17 KB
Rename
Delete
traceback.py
23.06 KB
Rename
Delete
tracemalloc.py
16.68 KB
Rename
Delete
tty.py
879 bytes
Rename
Delete
turtle.py
140.35 KB
Rename
Delete
types.py
9.49 KB
Rename
Delete
typing.py
67.35 KB
Rename
Delete
uu.py
7.11 KB
Rename
Delete
uuid.py
29.80 KB
Rename
Delete
warnings.py
19.23 KB
Rename
Delete
wave.py
17.80 KB
Rename
Delete
weakref.py
20.89 KB
Rename
Delete
webbrowser.py
23.52 KB
Rename
Delete
xdrlib.py
5.77 KB
Rename
Delete
zipapp.py
7.36 KB
Rename
Delete
zipfile.py
85.67 KB
Rename
Delete
zipimport.py
30.04 KB
Rename
Delete
"""Temporary files. This module provides generic, low- and high-level interfaces for creating temporary files and directories. All of the interfaces provided by this module can be used without fear of race conditions except for 'mktemp'. 'mktemp' is subject to race conditions and should not be used; it is provided for backward compatibility only. The default path names are returned as str. If you supply bytes as input, all return values will be in bytes. Ex: >>> tempfile.mkstemp() (4, '/tmp/tmptpu9nin8') >>> tempfile.mkdtemp(suffix=b'') b'/tmp/tmppbi8f0hy' This module also provides some data items to the user: TMP_MAX - maximum number of names that will be tried before giving up. 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 "SpooledTemporaryFile", "TemporaryDirectory", "mkstemp", "mkdtemp", # low level safe interfaces "mktemp", # deprecated unsafe interface "TMP_MAX", "gettempprefix", # constants "tempdir", "gettempdir", "gettempprefixb", "gettempdirb", ] # Imports. import functools as _functools import warnings as _warnings import io as _io import os as _os import shutil as _shutil import errno as _errno from random import Random as _Random import sys as _sys import weakref as _weakref import _thread _allocate_lock = _thread.allocate_lock _text_openflags = _os.O_RDWR | _os.O_CREAT | _os.O_EXCL if hasattr(_os, 'O_NOFOLLOW'): _text_openflags |= _os.O_NOFOLLOW _bin_openflags = _text_openflags if hasattr(_os, 'O_BINARY'): _bin_openflags |= _os.O_BINARY if hasattr(_os, 'TMP_MAX'): TMP_MAX = _os.TMP_MAX else: TMP_MAX = 10000 # This variable _was_ unused for legacy reasons, see issue 10354. # But as of 3.5 we actually use it at runtime so changing it would # have a possibly desirable side effect... But we do not want to support # that as an API. It is undocumented on purpose. Do not depend on this. template = "tmp" # Internal routines. _once_lock = _allocate_lock() def _exists(fn): try: _os.lstat(fn) except OSError: return False else: return True def _infer_return_type(*args): """Look at the type of all args and divine their implied return type.""" return_type = None for arg in args: if arg is None: continue if isinstance(arg, bytes): if return_type is str: raise TypeError("Can't mix bytes and non-bytes in " "path components.") return_type = bytes else: if return_type is bytes: raise TypeError("Can't mix bytes and non-bytes in " "path components.") return_type = str if return_type is None: return str # tempfile APIs return a str by default. return return_type def _sanitize_params(prefix, suffix, dir): """Common parameter processing for most APIs in this module.""" output_type = _infer_return_type(prefix, suffix, dir) if suffix is None: suffix = output_type() if prefix is None: if output_type is str: prefix = template else: prefix = _os.fsencode(template) if dir is None: if output_type is str: dir = gettempdir() else: dir = gettempdirb() return prefix, suffix, dir, output_type class _RandomNameSequence: """An instance of _RandomNameSequence generates an endless sequence of unpredictable strings which can safely be incorporated into file names. Each string is eight characters long. Multiple threads can safely use the same instance at the same time. _RandomNameSequence is an iterator.""" characters = "abcdefghijklmnopqrstuvwxyz0123456789_" @property def rng(self): cur_pid = _os.getpid() if cur_pid != getattr(self, '_rng_pid', None): self._rng = _Random() self._rng_pid = cur_pid return self._rng def __iter__(self): return self def __next__(self): c = self.characters choose = self.rng.choice letters = [choose(c) for dummy in range(8)] return ''.join(letters) def _candidate_tempdir_list(): """Generate a list of candidate temporary directories which _get_default_tempdir will try.""" dirlist = [] # First, try the environment. for envname in 'TMPDIR', 'TEMP', 'TMP': dirname = _os.getenv(envname) if dirname: dirlist.append(dirname) # Failing that, try OS-specific locations. if _os.name == 'nt': dirlist.extend([ _os.path.expanduser(r'~\AppData\Local\Temp'), _os.path.expandvars(r'%SYSTEMROOT%\Temp'), 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, OSError): dirlist.append(_os.curdir) return dirlist def _get_default_tempdir(): """Calculate the default directory to use for temporary files. This routine should be called exactly once. 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() for dir in dirlist: if dir != _os.curdir: dir = _os.path.abspath(dir) # Try only a few names per directory. for seq in range(100): name = next(namer) filename = _os.path.join(dir, name) try: fd = _os.open(filename, _bin_openflags, 0o600) try: try: with _io.open(fd, 'wb', closefd=False) as fp: fp.write(b'blat') finally: _os.close(fd) finally: _os.unlink(filename) return dir except FileExistsError: pass except PermissionError: # This exception is thrown when a directory with the chosen name # already exists on windows. if (_os.name == 'nt' and _os.path.isdir(dir) and _os.access(dir, _os.W_OK)): continue break # no point trying more names in this directory except OSError: break # no point trying more names in this directory raise FileNotFoundError(_errno.ENOENT, "No usable temporary directory found in %s" % dirlist) _name_sequence = None def _get_candidate_names(): """Common setup sequence for all user-callable interfaces.""" global _name_sequence if _name_sequence is None: _once_lock.acquire() try: if _name_sequence is None: _name_sequence = _RandomNameSequence() finally: _once_lock.release() return _name_sequence def _mkstemp_inner(dir, pre, suf, flags, output_type): """Code common to mkstemp, TemporaryFile, and NamedTemporaryFile.""" names = _get_candidate_names() if output_type is bytes: names = map(_os.fsencode, names) for seq in range(TMP_MAX): name = next(names) file = _os.path.join(dir, pre + name + suf) _sys.audit("tempfile.mkstemp", file) try: fd = _os.open(file, flags, 0o600) except FileExistsError: continue # try again except PermissionError: # This exception is thrown when a directory with the chosen name # already exists on windows. if (_os.name == 'nt' and _os.path.isdir(dir) and _os.access(dir, _os.W_OK)): continue else: raise return (fd, _os.path.abspath(file)) raise FileExistsError(_errno.EEXIST, "No usable temporary file name found") # User visible interfaces. def gettempprefix(): """The default prefix for temporary directories.""" return template def gettempprefixb(): """The default prefix for temporary directories as bytes.""" return _os.fsencode(gettempprefix()) tempdir = None def gettempdir(): """Accessor for tempfile.tempdir.""" global tempdir if tempdir is None: _once_lock.acquire() try: if tempdir is None: tempdir = _get_default_tempdir() finally: _once_lock.release() return tempdir def gettempdirb(): """A bytes version of tempfile.gettempdir().""" return _os.fsencode(gettempdir()) def mkstemp(suffix=None, prefix=None, dir=None, text=False): """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 not None, the file name will end with that suffix, otherwise there will be no suffix. If 'prefix' is not None, the file name will begin with that prefix, otherwise a default prefix is used. If 'dir' is not None, the file will be created in that directory, otherwise a default directory is used. If 'text' is specified and true, the file is opened in text mode. Else (the default) the file is opened in binary mode. If any of 'suffix', 'prefix' and 'dir' are not None, they must be the same type. If they are bytes, the returned name will be bytes; str otherwise. 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. Caller is responsible for deleting the file when done with it. """ prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir) if text: flags = _text_openflags else: flags = _bin_openflags return _mkstemp_inner(dir, prefix, suffix, flags, output_type) def mkdtemp(suffix=None, prefix=None, dir=None): """User-callable function to create and return a unique temporary directory. The return value is the pathname of the directory. Arguments are as for mkstemp, except that the 'text' argument is 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. """ prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir) names = _get_candidate_names() if output_type is bytes: names = map(_os.fsencode, names) for seq in range(TMP_MAX): name = next(names) file = _os.path.join(dir, prefix + name + suffix) _sys.audit("tempfile.mkdtemp", file) try: _os.mkdir(file, 0o700) except FileExistsError: continue # try again except PermissionError: # This exception is thrown when a directory with the chosen name # already exists on windows. if (_os.name == 'nt' and _os.path.isdir(dir) and _os.access(dir, _os.W_OK)): continue else: raise return file raise FileExistsError(_errno.EEXIST, "No usable temporary directory name found") def mktemp(suffix="", prefix=template, dir=None): """User-callable function to return a unique temporary file name. The file is not created. Arguments are similar to mkstemp, except that the 'text' argument is not accepted, and suffix=None, prefix=None and bytes file names are not supported. THIS FUNCTION IS UNSAFE AND SHOULD NOT BE USED. The file name may refer 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. """ ## from warnings import warn as _warn ## _warn("mktemp is a potential security risk to your program", ## RuntimeWarning, stacklevel=2) if dir is None: dir = gettempdir() names = _get_candidate_names() for seq in range(TMP_MAX): name = next(names) file = _os.path.join(dir, prefix + name + suffix) if not _exists(file): return file raise FileExistsError(_errno.EEXIST, "No usable temporary filename found") class _TemporaryFileCloser: """A separate object allowing proper closing of a temporary file's underlying file object, without adding a __del__ method to the temporary file.""" file = None # Set here since __del__ checks it close_called = False def __init__(self, file, name, delete=True): self.file = file self.name = name self.delete = delete # 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. def close(self, unlink=_os.unlink): if not self.close_called and self.file is not None: self.close_called = True try: self.file.close() finally: if self.delete: unlink(self.name) # Need to ensure the file is deleted on __del__ def __del__(self): self.close() else: def close(self): if not self.close_called: self.close_called = True self.file.close() 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. """ def __init__(self, file, name, delete=True): self.file = file self.name = name self.delete = delete self._closer = _TemporaryFileCloser(file, name, delete) def __getattr__(self, name): # Attribute lookups are delegated to the underlying file # and cached for non-numeric results # (i.e. methods are cached, closed and friends are not) file = self.__dict__['file'] a = getattr(file, name) if hasattr(a, '__call__'): func = a @_functools.wraps(func) def func_wrapper(*args, **kwargs): return func(*args, **kwargs) # Avoid closing the file as long as the wrapper is alive, # see issue #18879. func_wrapper._closer = self._closer a = func_wrapper if not isinstance(a, int): setattr(self, name, a) return a # The underlying __enter__ method returns the wrong object # (self.file) so override it to return the wrapper def __enter__(self): self.file.__enter__() return self # Need to trap __exit__ as well to ensure the file gets # deleted when used in a with statement def __exit__(self, exc, value, tb): result = self.file.__exit__(exc, value, tb) self.close() return result def close(self): """ Close the temporary file, possibly deleting it. """ self._closer.close() # iter() doesn't use __getattr__ to find the __iter__ method def __iter__(self): # Don't return iter(self.file), but yield from it to avoid closing # file as long as it's being used as iterator (see issue #23700). We # can't use 'yield from' here because iter(file) returns the file # object itself, which has a close method, and thus the file would get # closed when the generator is finalized, due to PEP380 semantics. for line in self.file: yield line def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True, *, errors=None): """Create and return a temporary file. Arguments: 'prefix', 'suffix', 'dir' -- as for mkstemp. 'mode' -- the mode argument to io.open (default "w+b"). 'buffering' -- the buffer size argument to io.open (default -1). 'encoding' -- the encoding argument to io.open (default None) 'newline' -- the newline argument to io.open (default None) 'delete' -- whether the file is deleted on close (default True). 'errors' -- the errors argument to io.open (default None) 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 its 'name' attribute. The file will be automatically deleted when it is closed unless the 'delete' argument is set to False. """ prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir) flags = _bin_openflags # 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' and delete: flags |= _os.O_TEMPORARY (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type) try: file = _io.open(fd, mode, buffering=buffering, newline=newline, encoding=encoding, errors=errors) return _TemporaryFileWrapper(file, name, delete) except BaseException: _os.unlink(name) _os.close(fd) raise if _os.name != 'posix' or _sys.platform == 'cygwin': # On non-POSIX and Cygwin systems, assume that we cannot unlink a file # while it is open. TemporaryFile = NamedTemporaryFile else: # Is the O_TMPFILE flag available and does it work? # The flag is set to False if os.open(dir, os.O_TMPFILE) raises an # IsADirectoryError exception _O_TMPFILE_WORKS = hasattr(_os, 'O_TMPFILE') def TemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None): """Create and return a temporary file. Arguments: 'prefix', 'suffix', 'dir' -- as for mkstemp. 'mode' -- the mode argument to io.open (default "w+b"). 'buffering' -- the buffer size argument to io.open (default -1). 'encoding' -- the encoding argument to io.open (default None) 'newline' -- the newline argument to io.open (default None) 'errors' -- the errors argument to io.open (default None) The file is created as mkstemp() would do it. Returns an object with a file-like interface. The file has no name, and will cease to exist when it is closed. """ global _O_TMPFILE_WORKS prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir) flags = _bin_openflags if _O_TMPFILE_WORKS: try: flags2 = (flags | _os.O_TMPFILE) & ~_os.O_CREAT fd = _os.open(dir, flags2, 0o600) except IsADirectoryError: # Linux kernel older than 3.11 ignores the O_TMPFILE flag: # O_TMPFILE is read as O_DIRECTORY. Trying to open a directory # with O_RDWR|O_DIRECTORY fails with IsADirectoryError, a # directory cannot be open to write. Set flag to False to not # try again. _O_TMPFILE_WORKS = False except OSError: # The filesystem of the directory does not support O_TMPFILE. # For example, OSError(95, 'Operation not supported'). # # On Linux kernel older than 3.11, trying to open a regular # file (or a symbolic link to a regular file) with O_TMPFILE # fails with NotADirectoryError, because O_TMPFILE is read as # O_DIRECTORY. pass else: try: return _io.open(fd, mode, buffering=buffering, newline=newline, encoding=encoding, errors=errors) except: _os.close(fd) raise # Fallback to _mkstemp_inner(). (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type) try: _os.unlink(name) return _io.open(fd, mode, buffering=buffering, newline=newline, encoding=encoding, errors=errors) except: _os.close(fd) raise class SpooledTemporaryFile: """Temporary file wrapper, specialized to switch from BytesIO or StringIO to a real file when it exceeds a certain size or when a fileno is needed. """ _rolled = False def __init__(self, max_size=0, mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None): if 'b' in mode: self._file = _io.BytesIO() else: self._file = _io.TextIOWrapper(_io.BytesIO(), encoding=encoding, errors=errors, newline=newline) self._max_size = max_size self._rolled = False self._TemporaryFileArgs = {'mode': mode, 'buffering': buffering, 'suffix': suffix, 'prefix': prefix, 'encoding': encoding, 'newline': newline, 'dir': dir, 'errors': errors} def _check(self, file): if self._rolled: return max_size = self._max_size if max_size and file.tell() > max_size: self.rollover() def rollover(self): if self._rolled: return file = self._file newfile = self._file = TemporaryFile(**self._TemporaryFileArgs) del self._TemporaryFileArgs pos = file.tell() if hasattr(newfile, 'buffer'): newfile.buffer.write(file.detach().getvalue()) else: newfile.write(file.getvalue()) newfile.seek(pos, 0) self._rolled = True # The method caching trick from NamedTemporaryFile # won't work here, because _file may change from a # BytesIO/StringIO instance to a real file. So we list # all the methods directly. # Context management protocol def __enter__(self): if self._file.closed: raise ValueError("Cannot enter context with closed file") return self def __exit__(self, exc, value, tb): self._file.close() # file protocol def __iter__(self): return self._file.__iter__() def close(self): self._file.close() @property def closed(self): return self._file.closed @property def encoding(self): return self._file.encoding @property def errors(self): return self._file.errors def fileno(self): self.rollover() return self._file.fileno() def flush(self): self._file.flush() def isatty(self): return self._file.isatty() @property def mode(self): try: return self._file.mode except AttributeError: return self._TemporaryFileArgs['mode'] @property def name(self): try: return self._file.name except AttributeError: return None @property def newlines(self): return self._file.newlines def read(self, *args): return self._file.read(*args) def readline(self, *args): return self._file.readline(*args) def readlines(self, *args): return self._file.readlines(*args) def seek(self, *args): return self._file.seek(*args) @property def softspace(self): return self._file.softspace def tell(self): return self._file.tell() def truncate(self, size=None): if size is None: self._file.truncate() else: if size > self._max_size: self.rollover() self._file.truncate(size) def write(self, s): file = self._file rv = file.write(s) self._check(file) return rv def writelines(self, iterable): file = self._file rv = file.writelines(iterable) self._check(file) return rv class TemporaryDirectory(object): """Create and return a temporary directory. This has the same behavior as mkdtemp but can be used as a context manager. For example: with TemporaryDirectory() as tmpdir: ... Upon exiting the context, the directory and everything contained in it are removed. """ def __init__(self, suffix=None, prefix=None, dir=None): self.name = mkdtemp(suffix, prefix, dir) self._finalizer = _weakref.finalize( self, self._cleanup, self.name, warn_message="Implicitly cleaning up {!r}".format(self)) @classmethod def _rmtree(cls, name): def onerror(func, path, exc_info): if issubclass(exc_info[0], PermissionError): def resetperms(path): try: _os.chflags(path, 0) except AttributeError: pass _os.chmod(path, 0o700) try: if path != name: resetperms(_os.path.dirname(path)) resetperms(path) try: _os.unlink(path) # PermissionError is raised on FreeBSD for directories except (IsADirectoryError, PermissionError): cls._rmtree(path) except FileNotFoundError: pass elif issubclass(exc_info[0], FileNotFoundError): pass else: raise _shutil.rmtree(name, onerror=onerror) @classmethod def _cleanup(cls, name, warn_message): cls._rmtree(name) _warnings.warn(warn_message, ResourceWarning) def __repr__(self): return "<{} {!r}>".format(self.__class__.__name__, self.name) def __enter__(self): return self.name def __exit__(self, exc, value, tb): self.cleanup() def cleanup(self): if self._finalizer.detach(): self._rmtree(self.name)
Save