tempfile.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. """Temporary files.
  2. This module provides generic, low- and high-level interfaces for
  3. creating temporary files and directories. All of the interfaces
  4. provided by this module can be used without fear of race conditions
  5. except for 'mktemp'. 'mktemp' is subject to race conditions and
  6. should not be used; it is provided for backward compatibility only.
  7. The default path names are returned as str. If you supply bytes as
  8. input, all return values will be in bytes. Ex:
  9. >>> tempfile.mkstemp()
  10. (4, '/tmp/tmptpu9nin8')
  11. >>> tempfile.mkdtemp(suffix=b'')
  12. b'/tmp/tmppbi8f0hy'
  13. This module also provides some data items to the user:
  14. TMP_MAX - maximum number of names that will be tried before
  15. giving up.
  16. tempdir - If this is set to a string before the first use of
  17. any routine from this module, it will be considered as
  18. another candidate location to store temporary files.
  19. """
  20. __all__ = [
  21. "NamedTemporaryFile", "TemporaryFile", # high level safe interfaces
  22. "SpooledTemporaryFile", "TemporaryDirectory",
  23. "mkstemp", "mkdtemp", # low level safe interfaces
  24. "mktemp", # deprecated unsafe interface
  25. "TMP_MAX", "gettempprefix", # constants
  26. "tempdir", "gettempdir",
  27. "gettempprefixb", "gettempdirb",
  28. ]
  29. # Imports.
  30. import functools as _functools
  31. import warnings as _warnings
  32. import io as _io
  33. import os as _os
  34. import shutil as _shutil
  35. import errno as _errno
  36. from random import Random as _Random
  37. import sys as _sys
  38. import types as _types
  39. import weakref as _weakref
  40. import _thread
  41. _allocate_lock = _thread.allocate_lock
  42. _text_openflags = _os.O_RDWR | _os.O_CREAT | _os.O_EXCL
  43. if hasattr(_os, 'O_NOFOLLOW'):
  44. _text_openflags |= _os.O_NOFOLLOW
  45. _bin_openflags = _text_openflags
  46. if hasattr(_os, 'O_BINARY'):
  47. _bin_openflags |= _os.O_BINARY
  48. if hasattr(_os, 'TMP_MAX'):
  49. TMP_MAX = _os.TMP_MAX
  50. else:
  51. TMP_MAX = 10000
  52. # This variable _was_ unused for legacy reasons, see issue 10354.
  53. # But as of 3.5 we actually use it at runtime so changing it would
  54. # have a possibly desirable side effect... But we do not want to support
  55. # that as an API. It is undocumented on purpose. Do not depend on this.
  56. template = "tmp"
  57. # Internal routines.
  58. _once_lock = _allocate_lock()
  59. def _exists(fn):
  60. try:
  61. _os.lstat(fn)
  62. except OSError:
  63. return False
  64. else:
  65. return True
  66. def _infer_return_type(*args):
  67. """Look at the type of all args and divine their implied return type."""
  68. return_type = None
  69. for arg in args:
  70. if arg is None:
  71. continue
  72. if isinstance(arg, _os.PathLike):
  73. arg = _os.fspath(arg)
  74. if isinstance(arg, bytes):
  75. if return_type is str:
  76. raise TypeError("Can't mix bytes and non-bytes in "
  77. "path components.")
  78. return_type = bytes
  79. else:
  80. if return_type is bytes:
  81. raise TypeError("Can't mix bytes and non-bytes in "
  82. "path components.")
  83. return_type = str
  84. if return_type is None:
  85. if tempdir is None or isinstance(tempdir, str):
  86. return str # tempfile APIs return a str by default.
  87. else:
  88. # we could check for bytes but it'll fail later on anyway
  89. return bytes
  90. return return_type
  91. def _sanitize_params(prefix, suffix, dir):
  92. """Common parameter processing for most APIs in this module."""
  93. output_type = _infer_return_type(prefix, suffix, dir)
  94. if suffix is None:
  95. suffix = output_type()
  96. if prefix is None:
  97. if output_type is str:
  98. prefix = template
  99. else:
  100. prefix = _os.fsencode(template)
  101. if dir is None:
  102. if output_type is str:
  103. dir = gettempdir()
  104. else:
  105. dir = gettempdirb()
  106. return prefix, suffix, dir, output_type
  107. class _RandomNameSequence:
  108. """An instance of _RandomNameSequence generates an endless
  109. sequence of unpredictable strings which can safely be incorporated
  110. into file names. Each string is eight characters long. Multiple
  111. threads can safely use the same instance at the same time.
  112. _RandomNameSequence is an iterator."""
  113. characters = "abcdefghijklmnopqrstuvwxyz0123456789_"
  114. @property
  115. def rng(self):
  116. cur_pid = _os.getpid()
  117. if cur_pid != getattr(self, '_rng_pid', None):
  118. self._rng = _Random()
  119. self._rng_pid = cur_pid
  120. return self._rng
  121. def __iter__(self):
  122. return self
  123. def __next__(self):
  124. return ''.join(self.rng.choices(self.characters, k=8))
  125. def _candidate_tempdir_list():
  126. """Generate a list of candidate temporary directories which
  127. _get_default_tempdir will try."""
  128. dirlist = []
  129. # First, try the environment.
  130. for envname in 'TMPDIR', 'TEMP', 'TMP':
  131. dirname = _os.getenv(envname)
  132. if dirname: dirlist.append(dirname)
  133. # Failing that, try OS-specific locations.
  134. if _os.name == 'nt':
  135. dirlist.extend([ _os.path.expanduser(r'~\AppData\Local\Temp'),
  136. _os.path.expandvars(r'%SYSTEMROOT%\Temp'),
  137. r'c:\temp', r'c:\tmp', r'\temp', r'\tmp' ])
  138. else:
  139. dirlist.extend([ '/tmp', '/var/tmp', '/usr/tmp' ])
  140. # As a last resort, the current directory.
  141. try:
  142. dirlist.append(_os.getcwd())
  143. except (AttributeError, OSError):
  144. dirlist.append(_os.curdir)
  145. return dirlist
  146. def _get_default_tempdir():
  147. """Calculate the default directory to use for temporary files.
  148. This routine should be called exactly once.
  149. We determine whether or not a candidate temp dir is usable by
  150. trying to create and write to a file in that directory. If this
  151. is successful, the test file is deleted. To prevent denial of
  152. service, the name of the test file must be randomized."""
  153. namer = _RandomNameSequence()
  154. dirlist = _candidate_tempdir_list()
  155. for dir in dirlist:
  156. if dir != _os.curdir:
  157. dir = _os.path.abspath(dir)
  158. # Try only a few names per directory.
  159. for seq in range(100):
  160. name = next(namer)
  161. filename = _os.path.join(dir, name)
  162. try:
  163. fd = _os.open(filename, _bin_openflags, 0o600)
  164. try:
  165. try:
  166. _os.write(fd, b'blat')
  167. finally:
  168. _os.close(fd)
  169. finally:
  170. _os.unlink(filename)
  171. return dir
  172. except FileExistsError:
  173. pass
  174. except PermissionError:
  175. # This exception is thrown when a directory with the chosen name
  176. # already exists on windows.
  177. if (_os.name == 'nt' and _os.path.isdir(dir) and
  178. _os.access(dir, _os.W_OK)):
  179. continue
  180. break # no point trying more names in this directory
  181. except OSError:
  182. break # no point trying more names in this directory
  183. raise FileNotFoundError(_errno.ENOENT,
  184. "No usable temporary directory found in %s" %
  185. dirlist)
  186. _name_sequence = None
  187. def _get_candidate_names():
  188. """Common setup sequence for all user-callable interfaces."""
  189. global _name_sequence
  190. if _name_sequence is None:
  191. _once_lock.acquire()
  192. try:
  193. if _name_sequence is None:
  194. _name_sequence = _RandomNameSequence()
  195. finally:
  196. _once_lock.release()
  197. return _name_sequence
  198. def _mkstemp_inner(dir, pre, suf, flags, output_type):
  199. """Code common to mkstemp, TemporaryFile, and NamedTemporaryFile."""
  200. dir = _os.path.abspath(dir)
  201. names = _get_candidate_names()
  202. if output_type is bytes:
  203. names = map(_os.fsencode, names)
  204. for seq in range(TMP_MAX):
  205. name = next(names)
  206. file = _os.path.join(dir, pre + name + suf)
  207. _sys.audit("tempfile.mkstemp", file)
  208. try:
  209. fd = _os.open(file, flags, 0o600)
  210. except FileExistsError:
  211. continue # try again
  212. except PermissionError:
  213. # This exception is thrown when a directory with the chosen name
  214. # already exists on windows.
  215. if (_os.name == 'nt' and _os.path.isdir(dir) and
  216. _os.access(dir, _os.W_OK)):
  217. continue
  218. else:
  219. raise
  220. return fd, file
  221. raise FileExistsError(_errno.EEXIST,
  222. "No usable temporary file name found")
  223. def _dont_follow_symlinks(func, path, *args):
  224. # Pass follow_symlinks=False, unless not supported on this platform.
  225. if func in _os.supports_follow_symlinks:
  226. func(path, *args, follow_symlinks=False)
  227. elif _os.name == 'nt' or not _os.path.islink(path):
  228. func(path, *args)
  229. def _resetperms(path):
  230. try:
  231. chflags = _os.chflags
  232. except AttributeError:
  233. pass
  234. else:
  235. _dont_follow_symlinks(chflags, path, 0)
  236. _dont_follow_symlinks(_os.chmod, path, 0o700)
  237. # User visible interfaces.
  238. def gettempprefix():
  239. """The default prefix for temporary directories as string."""
  240. return _os.fsdecode(template)
  241. def gettempprefixb():
  242. """The default prefix for temporary directories as bytes."""
  243. return _os.fsencode(template)
  244. tempdir = None
  245. def _gettempdir():
  246. """Private accessor for tempfile.tempdir."""
  247. global tempdir
  248. if tempdir is None:
  249. _once_lock.acquire()
  250. try:
  251. if tempdir is None:
  252. tempdir = _get_default_tempdir()
  253. finally:
  254. _once_lock.release()
  255. return tempdir
  256. def gettempdir():
  257. """Returns tempfile.tempdir as str."""
  258. return _os.fsdecode(_gettempdir())
  259. def gettempdirb():
  260. """Returns tempfile.tempdir as bytes."""
  261. return _os.fsencode(_gettempdir())
  262. def mkstemp(suffix=None, prefix=None, dir=None, text=False):
  263. """User-callable function to create and return a unique temporary
  264. file. The return value is a pair (fd, name) where fd is the
  265. file descriptor returned by os.open, and name is the filename.
  266. If 'suffix' is not None, the file name will end with that suffix,
  267. otherwise there will be no suffix.
  268. If 'prefix' is not None, the file name will begin with that prefix,
  269. otherwise a default prefix is used.
  270. If 'dir' is not None, the file will be created in that directory,
  271. otherwise a default directory is used.
  272. If 'text' is specified and true, the file is opened in text
  273. mode. Else (the default) the file is opened in binary mode.
  274. If any of 'suffix', 'prefix' and 'dir' are not None, they must be the
  275. same type. If they are bytes, the returned name will be bytes; str
  276. otherwise.
  277. The file is readable and writable only by the creating user ID.
  278. If the operating system uses permission bits to indicate whether a
  279. file is executable, the file is executable by no one. The file
  280. descriptor is not inherited by children of this process.
  281. Caller is responsible for deleting the file when done with it.
  282. """
  283. prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir)
  284. if text:
  285. flags = _text_openflags
  286. else:
  287. flags = _bin_openflags
  288. return _mkstemp_inner(dir, prefix, suffix, flags, output_type)
  289. def mkdtemp(suffix=None, prefix=None, dir=None):
  290. """User-callable function to create and return a unique temporary
  291. directory. The return value is the pathname of the directory.
  292. Arguments are as for mkstemp, except that the 'text' argument is
  293. not accepted.
  294. The directory is readable, writable, and searchable only by the
  295. creating user.
  296. Caller is responsible for deleting the directory when done with it.
  297. """
  298. prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir)
  299. names = _get_candidate_names()
  300. if output_type is bytes:
  301. names = map(_os.fsencode, names)
  302. for seq in range(TMP_MAX):
  303. name = next(names)
  304. file = _os.path.join(dir, prefix + name + suffix)
  305. _sys.audit("tempfile.mkdtemp", file)
  306. try:
  307. _os.mkdir(file, 0o700)
  308. except FileExistsError:
  309. continue # try again
  310. except PermissionError:
  311. # This exception is thrown when a directory with the chosen name
  312. # already exists on windows.
  313. if (_os.name == 'nt' and _os.path.isdir(dir) and
  314. _os.access(dir, _os.W_OK)):
  315. continue
  316. else:
  317. raise
  318. return _os.path.abspath(file)
  319. raise FileExistsError(_errno.EEXIST,
  320. "No usable temporary directory name found")
  321. def mktemp(suffix="", prefix=template, dir=None):
  322. """User-callable function to return a unique temporary file name. The
  323. file is not created.
  324. Arguments are similar to mkstemp, except that the 'text' argument is
  325. not accepted, and suffix=None, prefix=None and bytes file names are not
  326. supported.
  327. THIS FUNCTION IS UNSAFE AND SHOULD NOT BE USED. The file name may
  328. refer to a file that did not exist at some point, but by the time
  329. you get around to creating it, someone else may have beaten you to
  330. the punch.
  331. """
  332. ## from warnings import warn as _warn
  333. ## _warn("mktemp is a potential security risk to your program",
  334. ## RuntimeWarning, stacklevel=2)
  335. if dir is None:
  336. dir = gettempdir()
  337. names = _get_candidate_names()
  338. for seq in range(TMP_MAX):
  339. name = next(names)
  340. file = _os.path.join(dir, prefix + name + suffix)
  341. if not _exists(file):
  342. return file
  343. raise FileExistsError(_errno.EEXIST,
  344. "No usable temporary filename found")
  345. class _TemporaryFileCloser:
  346. """A separate object allowing proper closing of a temporary file's
  347. underlying file object, without adding a __del__ method to the
  348. temporary file."""
  349. cleanup_called = False
  350. close_called = False
  351. def __init__(self, file, name, delete=True, delete_on_close=True):
  352. self.file = file
  353. self.name = name
  354. self.delete = delete
  355. self.delete_on_close = delete_on_close
  356. def cleanup(self, windows=(_os.name == 'nt'), unlink=_os.unlink):
  357. if not self.cleanup_called:
  358. self.cleanup_called = True
  359. try:
  360. if not self.close_called:
  361. self.close_called = True
  362. self.file.close()
  363. finally:
  364. # Windows provides delete-on-close as a primitive, in which
  365. # case the file was deleted by self.file.close().
  366. if self.delete and not (windows and self.delete_on_close):
  367. try:
  368. unlink(self.name)
  369. except FileNotFoundError:
  370. pass
  371. def close(self):
  372. if not self.close_called:
  373. self.close_called = True
  374. try:
  375. self.file.close()
  376. finally:
  377. if self.delete and self.delete_on_close:
  378. self.cleanup()
  379. def __del__(self):
  380. self.cleanup()
  381. class _TemporaryFileWrapper:
  382. """Temporary file wrapper
  383. This class provides a wrapper around files opened for
  384. temporary use. In particular, it seeks to automatically
  385. remove the file when it is no longer needed.
  386. """
  387. def __init__(self, file, name, delete=True, delete_on_close=True):
  388. self.file = file
  389. self.name = name
  390. self._closer = _TemporaryFileCloser(file, name, delete,
  391. delete_on_close)
  392. def __getattr__(self, name):
  393. # Attribute lookups are delegated to the underlying file
  394. # and cached for non-numeric results
  395. # (i.e. methods are cached, closed and friends are not)
  396. file = self.__dict__['file']
  397. a = getattr(file, name)
  398. if hasattr(a, '__call__'):
  399. func = a
  400. @_functools.wraps(func)
  401. def func_wrapper(*args, **kwargs):
  402. return func(*args, **kwargs)
  403. # Avoid closing the file as long as the wrapper is alive,
  404. # see issue #18879.
  405. func_wrapper._closer = self._closer
  406. a = func_wrapper
  407. if not isinstance(a, int):
  408. setattr(self, name, a)
  409. return a
  410. # The underlying __enter__ method returns the wrong object
  411. # (self.file) so override it to return the wrapper
  412. def __enter__(self):
  413. self.file.__enter__()
  414. return self
  415. # Need to trap __exit__ as well to ensure the file gets
  416. # deleted when used in a with statement
  417. def __exit__(self, exc, value, tb):
  418. result = self.file.__exit__(exc, value, tb)
  419. self._closer.cleanup()
  420. return result
  421. def close(self):
  422. """
  423. Close the temporary file, possibly deleting it.
  424. """
  425. self._closer.close()
  426. # iter() doesn't use __getattr__ to find the __iter__ method
  427. def __iter__(self):
  428. # Don't return iter(self.file), but yield from it to avoid closing
  429. # file as long as it's being used as iterator (see issue #23700). We
  430. # can't use 'yield from' here because iter(file) returns the file
  431. # object itself, which has a close method, and thus the file would get
  432. # closed when the generator is finalized, due to PEP380 semantics.
  433. for line in self.file:
  434. yield line
  435. def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None,
  436. newline=None, suffix=None, prefix=None,
  437. dir=None, delete=True, *, errors=None,
  438. delete_on_close=True):
  439. """Create and return a temporary file.
  440. Arguments:
  441. 'prefix', 'suffix', 'dir' -- as for mkstemp.
  442. 'mode' -- the mode argument to io.open (default "w+b").
  443. 'buffering' -- the buffer size argument to io.open (default -1).
  444. 'encoding' -- the encoding argument to io.open (default None)
  445. 'newline' -- the newline argument to io.open (default None)
  446. 'delete' -- whether the file is automatically deleted (default True).
  447. 'delete_on_close' -- if 'delete', whether the file is deleted on close
  448. (default True) or otherwise either on context manager exit
  449. (if context manager was used) or on object finalization. .
  450. 'errors' -- the errors argument to io.open (default None)
  451. The file is created as mkstemp() would do it.
  452. Returns an object with a file-like interface; the name of the file
  453. is accessible as its 'name' attribute. The file will be automatically
  454. deleted when it is closed unless the 'delete' argument is set to False.
  455. On POSIX, NamedTemporaryFiles cannot be automatically deleted if
  456. the creating process is terminated abruptly with a SIGKILL signal.
  457. Windows can delete the file even in this case.
  458. """
  459. prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir)
  460. flags = _bin_openflags
  461. # Setting O_TEMPORARY in the flags causes the OS to delete
  462. # the file when it is closed. This is only supported by Windows.
  463. if _os.name == 'nt' and delete and delete_on_close:
  464. flags |= _os.O_TEMPORARY
  465. if "b" not in mode:
  466. encoding = _io.text_encoding(encoding)
  467. name = None
  468. def opener(*args):
  469. nonlocal name
  470. fd, name = _mkstemp_inner(dir, prefix, suffix, flags, output_type)
  471. return fd
  472. try:
  473. file = _io.open(dir, mode, buffering=buffering,
  474. newline=newline, encoding=encoding, errors=errors,
  475. opener=opener)
  476. try:
  477. raw = getattr(file, 'buffer', file)
  478. raw = getattr(raw, 'raw', raw)
  479. raw.name = name
  480. return _TemporaryFileWrapper(file, name, delete, delete_on_close)
  481. except:
  482. file.close()
  483. raise
  484. except:
  485. if name is not None and not (
  486. _os.name == 'nt' and delete and delete_on_close):
  487. _os.unlink(name)
  488. raise
  489. if _os.name != 'posix' or _sys.platform == 'cygwin':
  490. # On non-POSIX and Cygwin systems, assume that we cannot unlink a file
  491. # while it is open.
  492. TemporaryFile = NamedTemporaryFile
  493. else:
  494. # Is the O_TMPFILE flag available and does it work?
  495. # The flag is set to False if os.open(dir, os.O_TMPFILE) raises an
  496. # IsADirectoryError exception
  497. _O_TMPFILE_WORKS = hasattr(_os, 'O_TMPFILE')
  498. def TemporaryFile(mode='w+b', buffering=-1, encoding=None,
  499. newline=None, suffix=None, prefix=None,
  500. dir=None, *, errors=None):
  501. """Create and return a temporary file.
  502. Arguments:
  503. 'prefix', 'suffix', 'dir' -- as for mkstemp.
  504. 'mode' -- the mode argument to io.open (default "w+b").
  505. 'buffering' -- the buffer size argument to io.open (default -1).
  506. 'encoding' -- the encoding argument to io.open (default None)
  507. 'newline' -- the newline argument to io.open (default None)
  508. 'errors' -- the errors argument to io.open (default None)
  509. The file is created as mkstemp() would do it.
  510. Returns an object with a file-like interface. The file has no
  511. name, and will cease to exist when it is closed.
  512. """
  513. global _O_TMPFILE_WORKS
  514. if "b" not in mode:
  515. encoding = _io.text_encoding(encoding)
  516. prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir)
  517. flags = _bin_openflags
  518. if _O_TMPFILE_WORKS:
  519. fd = None
  520. def opener(*args):
  521. nonlocal fd
  522. flags2 = (flags | _os.O_TMPFILE) & ~_os.O_CREAT
  523. fd = _os.open(dir, flags2, 0o600)
  524. return fd
  525. try:
  526. file = _io.open(dir, mode, buffering=buffering,
  527. newline=newline, encoding=encoding,
  528. errors=errors, opener=opener)
  529. raw = getattr(file, 'buffer', file)
  530. raw = getattr(raw, 'raw', raw)
  531. raw.name = fd
  532. return file
  533. except IsADirectoryError:
  534. # Linux kernel older than 3.11 ignores the O_TMPFILE flag:
  535. # O_TMPFILE is read as O_DIRECTORY. Trying to open a directory
  536. # with O_RDWR|O_DIRECTORY fails with IsADirectoryError, a
  537. # directory cannot be open to write. Set flag to False to not
  538. # try again.
  539. _O_TMPFILE_WORKS = False
  540. except OSError:
  541. # The filesystem of the directory does not support O_TMPFILE.
  542. # For example, OSError(95, 'Operation not supported').
  543. #
  544. # On Linux kernel older than 3.11, trying to open a regular
  545. # file (or a symbolic link to a regular file) with O_TMPFILE
  546. # fails with NotADirectoryError, because O_TMPFILE is read as
  547. # O_DIRECTORY.
  548. pass
  549. # Fallback to _mkstemp_inner().
  550. fd = None
  551. def opener(*args):
  552. nonlocal fd
  553. fd, name = _mkstemp_inner(dir, prefix, suffix, flags, output_type)
  554. try:
  555. _os.unlink(name)
  556. except BaseException as e:
  557. _os.close(fd)
  558. raise
  559. return fd
  560. file = _io.open(dir, mode, buffering=buffering,
  561. newline=newline, encoding=encoding, errors=errors,
  562. opener=opener)
  563. raw = getattr(file, 'buffer', file)
  564. raw = getattr(raw, 'raw', raw)
  565. raw.name = fd
  566. return file
  567. class SpooledTemporaryFile(_io.IOBase):
  568. """Temporary file wrapper, specialized to switch from BytesIO
  569. or StringIO to a real file when it exceeds a certain size or
  570. when a fileno is needed.
  571. """
  572. _rolled = False
  573. def __init__(self, max_size=0, mode='w+b', buffering=-1,
  574. encoding=None, newline=None,
  575. suffix=None, prefix=None, dir=None, *, errors=None):
  576. if 'b' in mode:
  577. self._file = _io.BytesIO()
  578. else:
  579. encoding = _io.text_encoding(encoding)
  580. self._file = _io.TextIOWrapper(_io.BytesIO(),
  581. encoding=encoding, errors=errors,
  582. newline=newline)
  583. self._max_size = max_size
  584. self._rolled = False
  585. self._TemporaryFileArgs = {'mode': mode, 'buffering': buffering,
  586. 'suffix': suffix, 'prefix': prefix,
  587. 'encoding': encoding, 'newline': newline,
  588. 'dir': dir, 'errors': errors}
  589. __class_getitem__ = classmethod(_types.GenericAlias)
  590. def _check(self, file):
  591. if self._rolled: return
  592. max_size = self._max_size
  593. if max_size and file.tell() > max_size:
  594. self.rollover()
  595. def rollover(self):
  596. if self._rolled: return
  597. file = self._file
  598. newfile = self._file = TemporaryFile(**self._TemporaryFileArgs)
  599. del self._TemporaryFileArgs
  600. pos = file.tell()
  601. if hasattr(newfile, 'buffer'):
  602. newfile.buffer.write(file.detach().getvalue())
  603. else:
  604. newfile.write(file.getvalue())
  605. newfile.seek(pos, 0)
  606. self._rolled = True
  607. # The method caching trick from NamedTemporaryFile
  608. # won't work here, because _file may change from a
  609. # BytesIO/StringIO instance to a real file. So we list
  610. # all the methods directly.
  611. # Context management protocol
  612. def __enter__(self):
  613. if self._file.closed:
  614. raise ValueError("Cannot enter context with closed file")
  615. return self
  616. def __exit__(self, exc, value, tb):
  617. self._file.close()
  618. # file protocol
  619. def __iter__(self):
  620. return self._file.__iter__()
  621. def __del__(self):
  622. if not self.closed:
  623. _warnings.warn(
  624. "Unclosed file {!r}".format(self),
  625. ResourceWarning,
  626. stacklevel=2,
  627. source=self
  628. )
  629. self.close()
  630. def close(self):
  631. self._file.close()
  632. @property
  633. def closed(self):
  634. return self._file.closed
  635. @property
  636. def encoding(self):
  637. return self._file.encoding
  638. @property
  639. def errors(self):
  640. return self._file.errors
  641. def fileno(self):
  642. self.rollover()
  643. return self._file.fileno()
  644. def flush(self):
  645. self._file.flush()
  646. def isatty(self):
  647. return self._file.isatty()
  648. @property
  649. def mode(self):
  650. try:
  651. return self._file.mode
  652. except AttributeError:
  653. return self._TemporaryFileArgs['mode']
  654. @property
  655. def name(self):
  656. try:
  657. return self._file.name
  658. except AttributeError:
  659. return None
  660. @property
  661. def newlines(self):
  662. return self._file.newlines
  663. def readable(self):
  664. return self._file.readable()
  665. def read(self, *args):
  666. return self._file.read(*args)
  667. def read1(self, *args):
  668. return self._file.read1(*args)
  669. def readinto(self, b):
  670. return self._file.readinto(b)
  671. def readinto1(self, b):
  672. return self._file.readinto1(b)
  673. def readline(self, *args):
  674. return self._file.readline(*args)
  675. def readlines(self, *args):
  676. return self._file.readlines(*args)
  677. def seekable(self):
  678. return self._file.seekable()
  679. def seek(self, *args):
  680. return self._file.seek(*args)
  681. def tell(self):
  682. return self._file.tell()
  683. def truncate(self, size=None):
  684. if size is None:
  685. return self._file.truncate()
  686. else:
  687. if size > self._max_size:
  688. self.rollover()
  689. return self._file.truncate(size)
  690. def writable(self):
  691. return self._file.writable()
  692. def write(self, s):
  693. file = self._file
  694. rv = file.write(s)
  695. self._check(file)
  696. return rv
  697. def writelines(self, iterable):
  698. file = self._file
  699. rv = file.writelines(iterable)
  700. self._check(file)
  701. return rv
  702. def detach(self):
  703. return self._file.detach()
  704. class TemporaryDirectory:
  705. """Create and return a temporary directory. This has the same
  706. behavior as mkdtemp but can be used as a context manager. For
  707. example:
  708. with TemporaryDirectory() as tmpdir:
  709. ...
  710. Upon exiting the context, the directory and everything contained
  711. in it are removed (unless delete=False is passed or an exception
  712. is raised during cleanup and ignore_cleanup_errors is not True).
  713. Optional Arguments:
  714. suffix - A str suffix for the directory name. (see mkdtemp)
  715. prefix - A str prefix for the directory name. (see mkdtemp)
  716. dir - A directory to create this temp dir in. (see mkdtemp)
  717. ignore_cleanup_errors - False; ignore exceptions during cleanup?
  718. delete - True; whether the directory is automatically deleted.
  719. """
  720. def __init__(self, suffix=None, prefix=None, dir=None,
  721. ignore_cleanup_errors=False, *, delete=True):
  722. self.name = mkdtemp(suffix, prefix, dir)
  723. self._ignore_cleanup_errors = ignore_cleanup_errors
  724. self._delete = delete
  725. self._finalizer = _weakref.finalize(
  726. self, self._cleanup, self.name,
  727. warn_message="Implicitly cleaning up {!r}".format(self),
  728. ignore_errors=self._ignore_cleanup_errors, delete=self._delete)
  729. @classmethod
  730. def _rmtree(cls, name, ignore_errors=False, repeated=False):
  731. def onexc(func, path, exc):
  732. if isinstance(exc, PermissionError):
  733. if repeated and path == name:
  734. if ignore_errors:
  735. return
  736. raise
  737. try:
  738. if path != name:
  739. _resetperms(_os.path.dirname(path))
  740. _resetperms(path)
  741. try:
  742. _os.unlink(path)
  743. except IsADirectoryError:
  744. cls._rmtree(path, ignore_errors=ignore_errors)
  745. except PermissionError:
  746. # The PermissionError handler was originally added for
  747. # FreeBSD in directories, but it seems that it is raised
  748. # on Windows too.
  749. # bpo-43153: Calling _rmtree again may
  750. # raise NotADirectoryError and mask the PermissionError.
  751. # So we must re-raise the current PermissionError if
  752. # path is not a directory.
  753. if not _os.path.isdir(path) or _os.path.isjunction(path):
  754. if ignore_errors:
  755. return
  756. raise
  757. cls._rmtree(path, ignore_errors=ignore_errors,
  758. repeated=(path == name))
  759. except FileNotFoundError:
  760. pass
  761. elif isinstance(exc, FileNotFoundError):
  762. pass
  763. else:
  764. if not ignore_errors:
  765. raise
  766. _shutil.rmtree(name, onexc=onexc)
  767. @classmethod
  768. def _cleanup(cls, name, warn_message, ignore_errors=False, delete=True):
  769. if delete:
  770. cls._rmtree(name, ignore_errors=ignore_errors)
  771. _warnings.warn(warn_message, ResourceWarning)
  772. def __repr__(self):
  773. return "<{} {!r}>".format(self.__class__.__name__, self.name)
  774. def __enter__(self):
  775. return self.name
  776. def __exit__(self, exc, value, tb):
  777. if self._delete:
  778. self.cleanup()
  779. def cleanup(self):
  780. if self._finalizer.detach() or _os.path.exists(self.name):
  781. self._rmtree(self.name, ignore_errors=self._ignore_cleanup_errors)
  782. __class_getitem__ = classmethod(_types.GenericAlias)