tempfile.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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, bytes):
  73. if return_type is str:
  74. raise TypeError("Can't mix bytes and non-bytes in "
  75. "path components.")
  76. return_type = bytes
  77. else:
  78. if return_type is bytes:
  79. raise TypeError("Can't mix bytes and non-bytes in "
  80. "path components.")
  81. return_type = str
  82. if return_type is None:
  83. return str # tempfile APIs return a str by default.
  84. return return_type
  85. def _sanitize_params(prefix, suffix, dir):
  86. """Common parameter processing for most APIs in this module."""
  87. output_type = _infer_return_type(prefix, suffix, dir)
  88. if suffix is None:
  89. suffix = output_type()
  90. if prefix is None:
  91. if output_type is str:
  92. prefix = template
  93. else:
  94. prefix = _os.fsencode(template)
  95. if dir is None:
  96. if output_type is str:
  97. dir = gettempdir()
  98. else:
  99. dir = gettempdirb()
  100. return prefix, suffix, dir, output_type
  101. class _RandomNameSequence:
  102. """An instance of _RandomNameSequence generates an endless
  103. sequence of unpredictable strings which can safely be incorporated
  104. into file names. Each string is eight characters long. Multiple
  105. threads can safely use the same instance at the same time.
  106. _RandomNameSequence is an iterator."""
  107. characters = "abcdefghijklmnopqrstuvwxyz0123456789_"
  108. @property
  109. def rng(self):
  110. cur_pid = _os.getpid()
  111. if cur_pid != getattr(self, '_rng_pid', None):
  112. self._rng = _Random()
  113. self._rng_pid = cur_pid
  114. return self._rng
  115. def __iter__(self):
  116. return self
  117. def __next__(self):
  118. c = self.characters
  119. choose = self.rng.choice
  120. letters = [choose(c) for dummy in range(8)]
  121. return ''.join(letters)
  122. def _candidate_tempdir_list():
  123. """Generate a list of candidate temporary directories which
  124. _get_default_tempdir will try."""
  125. dirlist = []
  126. # First, try the environment.
  127. for envname in 'TMPDIR', 'TEMP', 'TMP':
  128. dirname = _os.getenv(envname)
  129. if dirname: dirlist.append(dirname)
  130. # Failing that, try OS-specific locations.
  131. if _os.name == 'nt':
  132. dirlist.extend([ _os.path.expanduser(r'~\AppData\Local\Temp'),
  133. _os.path.expandvars(r'%SYSTEMROOT%\Temp'),
  134. r'c:\temp', r'c:\tmp', r'\temp', r'\tmp' ])
  135. else:
  136. dirlist.extend([ '/tmp', '/var/tmp', '/usr/tmp' ])
  137. # As a last resort, the current directory.
  138. try:
  139. dirlist.append(_os.getcwd())
  140. except (AttributeError, OSError):
  141. dirlist.append(_os.curdir)
  142. return dirlist
  143. def _get_default_tempdir():
  144. """Calculate the default directory to use for temporary files.
  145. This routine should be called exactly once.
  146. We determine whether or not a candidate temp dir is usable by
  147. trying to create and write to a file in that directory. If this
  148. is successful, the test file is deleted. To prevent denial of
  149. service, the name of the test file must be randomized."""
  150. namer = _RandomNameSequence()
  151. dirlist = _candidate_tempdir_list()
  152. for dir in dirlist:
  153. if dir != _os.curdir:
  154. dir = _os.path.abspath(dir)
  155. # Try only a few names per directory.
  156. for seq in range(100):
  157. name = next(namer)
  158. filename = _os.path.join(dir, name)
  159. try:
  160. fd = _os.open(filename, _bin_openflags, 0o600)
  161. try:
  162. try:
  163. with _io.open(fd, 'wb', closefd=False) as fp:
  164. fp.write(b'blat')
  165. finally:
  166. _os.close(fd)
  167. finally:
  168. _os.unlink(filename)
  169. return dir
  170. except FileExistsError:
  171. pass
  172. except PermissionError:
  173. # This exception is thrown when a directory with the chosen name
  174. # already exists on windows.
  175. if (_os.name == 'nt' and _os.path.isdir(dir) and
  176. _os.access(dir, _os.W_OK)):
  177. continue
  178. break # no point trying more names in this directory
  179. except OSError:
  180. break # no point trying more names in this directory
  181. raise FileNotFoundError(_errno.ENOENT,
  182. "No usable temporary directory found in %s" %
  183. dirlist)
  184. _name_sequence = None
  185. def _get_candidate_names():
  186. """Common setup sequence for all user-callable interfaces."""
  187. global _name_sequence
  188. if _name_sequence is None:
  189. _once_lock.acquire()
  190. try:
  191. if _name_sequence is None:
  192. _name_sequence = _RandomNameSequence()
  193. finally:
  194. _once_lock.release()
  195. return _name_sequence
  196. def _mkstemp_inner(dir, pre, suf, flags, output_type):
  197. """Code common to mkstemp, TemporaryFile, and NamedTemporaryFile."""
  198. names = _get_candidate_names()
  199. if output_type is bytes:
  200. names = map(_os.fsencode, names)
  201. for seq in range(TMP_MAX):
  202. name = next(names)
  203. file = _os.path.join(dir, pre + name + suf)
  204. _sys.audit("tempfile.mkstemp", file)
  205. try:
  206. fd = _os.open(file, flags, 0o600)
  207. except FileExistsError:
  208. continue # try again
  209. except PermissionError:
  210. # This exception is thrown when a directory with the chosen name
  211. # already exists on windows.
  212. if (_os.name == 'nt' and _os.path.isdir(dir) and
  213. _os.access(dir, _os.W_OK)):
  214. continue
  215. else:
  216. raise
  217. return (fd, _os.path.abspath(file))
  218. raise FileExistsError(_errno.EEXIST,
  219. "No usable temporary file name found")
  220. # User visible interfaces.
  221. def gettempprefix():
  222. """The default prefix for temporary directories."""
  223. return template
  224. def gettempprefixb():
  225. """The default prefix for temporary directories as bytes."""
  226. return _os.fsencode(gettempprefix())
  227. tempdir = None
  228. def gettempdir():
  229. """Accessor for tempfile.tempdir."""
  230. global tempdir
  231. if tempdir is None:
  232. _once_lock.acquire()
  233. try:
  234. if tempdir is None:
  235. tempdir = _get_default_tempdir()
  236. finally:
  237. _once_lock.release()
  238. return tempdir
  239. def gettempdirb():
  240. """A bytes version of tempfile.gettempdir()."""
  241. return _os.fsencode(gettempdir())
  242. def mkstemp(suffix=None, prefix=None, dir=None, text=False):
  243. """User-callable function to create and return a unique temporary
  244. file. The return value is a pair (fd, name) where fd is the
  245. file descriptor returned by os.open, and name is the filename.
  246. If 'suffix' is not None, the file name will end with that suffix,
  247. otherwise there will be no suffix.
  248. If 'prefix' is not None, the file name will begin with that prefix,
  249. otherwise a default prefix is used.
  250. If 'dir' is not None, the file will be created in that directory,
  251. otherwise a default directory is used.
  252. If 'text' is specified and true, the file is opened in text
  253. mode. Else (the default) the file is opened in binary mode.
  254. If any of 'suffix', 'prefix' and 'dir' are not None, they must be the
  255. same type. If they are bytes, the returned name will be bytes; str
  256. otherwise.
  257. The file is readable and writable only by the creating user ID.
  258. If the operating system uses permission bits to indicate whether a
  259. file is executable, the file is executable by no one. The file
  260. descriptor is not inherited by children of this process.
  261. Caller is responsible for deleting the file when done with it.
  262. """
  263. prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir)
  264. if text:
  265. flags = _text_openflags
  266. else:
  267. flags = _bin_openflags
  268. return _mkstemp_inner(dir, prefix, suffix, flags, output_type)
  269. def mkdtemp(suffix=None, prefix=None, dir=None):
  270. """User-callable function to create and return a unique temporary
  271. directory. The return value is the pathname of the directory.
  272. Arguments are as for mkstemp, except that the 'text' argument is
  273. not accepted.
  274. The directory is readable, writable, and searchable only by the
  275. creating user.
  276. Caller is responsible for deleting the directory when done with it.
  277. """
  278. prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir)
  279. names = _get_candidate_names()
  280. if output_type is bytes:
  281. names = map(_os.fsencode, names)
  282. for seq in range(TMP_MAX):
  283. name = next(names)
  284. file = _os.path.join(dir, prefix + name + suffix)
  285. _sys.audit("tempfile.mkdtemp", file)
  286. try:
  287. _os.mkdir(file, 0o700)
  288. except FileExistsError:
  289. continue # try again
  290. except PermissionError:
  291. # This exception is thrown when a directory with the chosen name
  292. # already exists on windows.
  293. if (_os.name == 'nt' and _os.path.isdir(dir) and
  294. _os.access(dir, _os.W_OK)):
  295. continue
  296. else:
  297. raise
  298. return file
  299. raise FileExistsError(_errno.EEXIST,
  300. "No usable temporary directory name found")
  301. def mktemp(suffix="", prefix=template, dir=None):
  302. """User-callable function to return a unique temporary file name. The
  303. file is not created.
  304. Arguments are similar to mkstemp, except that the 'text' argument is
  305. not accepted, and suffix=None, prefix=None and bytes file names are not
  306. supported.
  307. THIS FUNCTION IS UNSAFE AND SHOULD NOT BE USED. The file name may
  308. refer to a file that did not exist at some point, but by the time
  309. you get around to creating it, someone else may have beaten you to
  310. the punch.
  311. """
  312. ## from warnings import warn as _warn
  313. ## _warn("mktemp is a potential security risk to your program",
  314. ## RuntimeWarning, stacklevel=2)
  315. if dir is None:
  316. dir = gettempdir()
  317. names = _get_candidate_names()
  318. for seq in range(TMP_MAX):
  319. name = next(names)
  320. file = _os.path.join(dir, prefix + name + suffix)
  321. if not _exists(file):
  322. return file
  323. raise FileExistsError(_errno.EEXIST,
  324. "No usable temporary filename found")
  325. class _TemporaryFileCloser:
  326. """A separate object allowing proper closing of a temporary file's
  327. underlying file object, without adding a __del__ method to the
  328. temporary file."""
  329. file = None # Set here since __del__ checks it
  330. close_called = False
  331. def __init__(self, file, name, delete=True):
  332. self.file = file
  333. self.name = name
  334. self.delete = delete
  335. # NT provides delete-on-close as a primitive, so we don't need
  336. # the wrapper to do anything special. We still use it so that
  337. # file.name is useful (i.e. not "(fdopen)") with NamedTemporaryFile.
  338. if _os.name != 'nt':
  339. # Cache the unlinker so we don't get spurious errors at
  340. # shutdown when the module-level "os" is None'd out. Note
  341. # that this must be referenced as self.unlink, because the
  342. # name TemporaryFileWrapper may also get None'd out before
  343. # __del__ is called.
  344. def close(self, unlink=_os.unlink):
  345. if not self.close_called and self.file is not None:
  346. self.close_called = True
  347. try:
  348. self.file.close()
  349. finally:
  350. if self.delete:
  351. unlink(self.name)
  352. # Need to ensure the file is deleted on __del__
  353. def __del__(self):
  354. self.close()
  355. else:
  356. def close(self):
  357. if not self.close_called:
  358. self.close_called = True
  359. self.file.close()
  360. class _TemporaryFileWrapper:
  361. """Temporary file wrapper
  362. This class provides a wrapper around files opened for
  363. temporary use. In particular, it seeks to automatically
  364. remove the file when it is no longer needed.
  365. """
  366. def __init__(self, file, name, delete=True):
  367. self.file = file
  368. self.name = name
  369. self.delete = delete
  370. self._closer = _TemporaryFileCloser(file, name, delete)
  371. def __getattr__(self, name):
  372. # Attribute lookups are delegated to the underlying file
  373. # and cached for non-numeric results
  374. # (i.e. methods are cached, closed and friends are not)
  375. file = self.__dict__['file']
  376. a = getattr(file, name)
  377. if hasattr(a, '__call__'):
  378. func = a
  379. @_functools.wraps(func)
  380. def func_wrapper(*args, **kwargs):
  381. return func(*args, **kwargs)
  382. # Avoid closing the file as long as the wrapper is alive,
  383. # see issue #18879.
  384. func_wrapper._closer = self._closer
  385. a = func_wrapper
  386. if not isinstance(a, int):
  387. setattr(self, name, a)
  388. return a
  389. # The underlying __enter__ method returns the wrong object
  390. # (self.file) so override it to return the wrapper
  391. def __enter__(self):
  392. self.file.__enter__()
  393. return self
  394. # Need to trap __exit__ as well to ensure the file gets
  395. # deleted when used in a with statement
  396. def __exit__(self, exc, value, tb):
  397. result = self.file.__exit__(exc, value, tb)
  398. self.close()
  399. return result
  400. def close(self):
  401. """
  402. Close the temporary file, possibly deleting it.
  403. """
  404. self._closer.close()
  405. # iter() doesn't use __getattr__ to find the __iter__ method
  406. def __iter__(self):
  407. # Don't return iter(self.file), but yield from it to avoid closing
  408. # file as long as it's being used as iterator (see issue #23700). We
  409. # can't use 'yield from' here because iter(file) returns the file
  410. # object itself, which has a close method, and thus the file would get
  411. # closed when the generator is finalized, due to PEP380 semantics.
  412. for line in self.file:
  413. yield line
  414. def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None,
  415. newline=None, suffix=None, prefix=None,
  416. dir=None, delete=True, *, errors=None):
  417. """Create and return a temporary file.
  418. Arguments:
  419. 'prefix', 'suffix', 'dir' -- as for mkstemp.
  420. 'mode' -- the mode argument to io.open (default "w+b").
  421. 'buffering' -- the buffer size argument to io.open (default -1).
  422. 'encoding' -- the encoding argument to io.open (default None)
  423. 'newline' -- the newline argument to io.open (default None)
  424. 'delete' -- whether the file is deleted on close (default True).
  425. 'errors' -- the errors argument to io.open (default None)
  426. The file is created as mkstemp() would do it.
  427. Returns an object with a file-like interface; the name of the file
  428. is accessible as its 'name' attribute. The file will be automatically
  429. deleted when it is closed unless the 'delete' argument is set to False.
  430. """
  431. prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir)
  432. flags = _bin_openflags
  433. # Setting O_TEMPORARY in the flags causes the OS to delete
  434. # the file when it is closed. This is only supported by Windows.
  435. if _os.name == 'nt' and delete:
  436. flags |= _os.O_TEMPORARY
  437. (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type)
  438. try:
  439. file = _io.open(fd, mode, buffering=buffering,
  440. newline=newline, encoding=encoding, errors=errors)
  441. return _TemporaryFileWrapper(file, name, delete)
  442. except BaseException:
  443. _os.unlink(name)
  444. _os.close(fd)
  445. raise
  446. if _os.name != 'posix' or _sys.platform == 'cygwin':
  447. # On non-POSIX and Cygwin systems, assume that we cannot unlink a file
  448. # while it is open.
  449. TemporaryFile = NamedTemporaryFile
  450. else:
  451. # Is the O_TMPFILE flag available and does it work?
  452. # The flag is set to False if os.open(dir, os.O_TMPFILE) raises an
  453. # IsADirectoryError exception
  454. _O_TMPFILE_WORKS = hasattr(_os, 'O_TMPFILE')
  455. def TemporaryFile(mode='w+b', buffering=-1, encoding=None,
  456. newline=None, suffix=None, prefix=None,
  457. dir=None, *, errors=None):
  458. """Create and return a temporary file.
  459. Arguments:
  460. 'prefix', 'suffix', 'dir' -- as for mkstemp.
  461. 'mode' -- the mode argument to io.open (default "w+b").
  462. 'buffering' -- the buffer size argument to io.open (default -1).
  463. 'encoding' -- the encoding argument to io.open (default None)
  464. 'newline' -- the newline argument to io.open (default None)
  465. 'errors' -- the errors argument to io.open (default None)
  466. The file is created as mkstemp() would do it.
  467. Returns an object with a file-like interface. The file has no
  468. name, and will cease to exist when it is closed.
  469. """
  470. global _O_TMPFILE_WORKS
  471. prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir)
  472. flags = _bin_openflags
  473. if _O_TMPFILE_WORKS:
  474. try:
  475. flags2 = (flags | _os.O_TMPFILE) & ~_os.O_CREAT
  476. fd = _os.open(dir, flags2, 0o600)
  477. except IsADirectoryError:
  478. # Linux kernel older than 3.11 ignores the O_TMPFILE flag:
  479. # O_TMPFILE is read as O_DIRECTORY. Trying to open a directory
  480. # with O_RDWR|O_DIRECTORY fails with IsADirectoryError, a
  481. # directory cannot be open to write. Set flag to False to not
  482. # try again.
  483. _O_TMPFILE_WORKS = False
  484. except OSError:
  485. # The filesystem of the directory does not support O_TMPFILE.
  486. # For example, OSError(95, 'Operation not supported').
  487. #
  488. # On Linux kernel older than 3.11, trying to open a regular
  489. # file (or a symbolic link to a regular file) with O_TMPFILE
  490. # fails with NotADirectoryError, because O_TMPFILE is read as
  491. # O_DIRECTORY.
  492. pass
  493. else:
  494. try:
  495. return _io.open(fd, mode, buffering=buffering,
  496. newline=newline, encoding=encoding,
  497. errors=errors)
  498. except:
  499. _os.close(fd)
  500. raise
  501. # Fallback to _mkstemp_inner().
  502. (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type)
  503. try:
  504. _os.unlink(name)
  505. return _io.open(fd, mode, buffering=buffering,
  506. newline=newline, encoding=encoding, errors=errors)
  507. except:
  508. _os.close(fd)
  509. raise
  510. class SpooledTemporaryFile:
  511. """Temporary file wrapper, specialized to switch from BytesIO
  512. or StringIO to a real file when it exceeds a certain size or
  513. when a fileno is needed.
  514. """
  515. _rolled = False
  516. def __init__(self, max_size=0, mode='w+b', buffering=-1,
  517. encoding=None, newline=None,
  518. suffix=None, prefix=None, dir=None, *, errors=None):
  519. if 'b' in mode:
  520. self._file = _io.BytesIO()
  521. else:
  522. self._file = _io.TextIOWrapper(_io.BytesIO(),
  523. encoding=encoding, errors=errors,
  524. newline=newline)
  525. self._max_size = max_size
  526. self._rolled = False
  527. self._TemporaryFileArgs = {'mode': mode, 'buffering': buffering,
  528. 'suffix': suffix, 'prefix': prefix,
  529. 'encoding': encoding, 'newline': newline,
  530. 'dir': dir, 'errors': errors}
  531. __class_getitem__ = classmethod(_types.GenericAlias)
  532. def _check(self, file):
  533. if self._rolled: return
  534. max_size = self._max_size
  535. if max_size and file.tell() > max_size:
  536. self.rollover()
  537. def rollover(self):
  538. if self._rolled: return
  539. file = self._file
  540. newfile = self._file = TemporaryFile(**self._TemporaryFileArgs)
  541. del self._TemporaryFileArgs
  542. pos = file.tell()
  543. if hasattr(newfile, 'buffer'):
  544. newfile.buffer.write(file.detach().getvalue())
  545. else:
  546. newfile.write(file.getvalue())
  547. newfile.seek(pos, 0)
  548. self._rolled = True
  549. # The method caching trick from NamedTemporaryFile
  550. # won't work here, because _file may change from a
  551. # BytesIO/StringIO instance to a real file. So we list
  552. # all the methods directly.
  553. # Context management protocol
  554. def __enter__(self):
  555. if self._file.closed:
  556. raise ValueError("Cannot enter context with closed file")
  557. return self
  558. def __exit__(self, exc, value, tb):
  559. self._file.close()
  560. # file protocol
  561. def __iter__(self):
  562. return self._file.__iter__()
  563. def close(self):
  564. self._file.close()
  565. @property
  566. def closed(self):
  567. return self._file.closed
  568. @property
  569. def encoding(self):
  570. return self._file.encoding
  571. @property
  572. def errors(self):
  573. return self._file.errors
  574. def fileno(self):
  575. self.rollover()
  576. return self._file.fileno()
  577. def flush(self):
  578. self._file.flush()
  579. def isatty(self):
  580. return self._file.isatty()
  581. @property
  582. def mode(self):
  583. try:
  584. return self._file.mode
  585. except AttributeError:
  586. return self._TemporaryFileArgs['mode']
  587. @property
  588. def name(self):
  589. try:
  590. return self._file.name
  591. except AttributeError:
  592. return None
  593. @property
  594. def newlines(self):
  595. return self._file.newlines
  596. def read(self, *args):
  597. return self._file.read(*args)
  598. def readline(self, *args):
  599. return self._file.readline(*args)
  600. def readlines(self, *args):
  601. return self._file.readlines(*args)
  602. def seek(self, *args):
  603. return self._file.seek(*args)
  604. def tell(self):
  605. return self._file.tell()
  606. def truncate(self, size=None):
  607. if size is None:
  608. self._file.truncate()
  609. else:
  610. if size > self._max_size:
  611. self.rollover()
  612. self._file.truncate(size)
  613. def write(self, s):
  614. file = self._file
  615. rv = file.write(s)
  616. self._check(file)
  617. return rv
  618. def writelines(self, iterable):
  619. file = self._file
  620. rv = file.writelines(iterable)
  621. self._check(file)
  622. return rv
  623. class TemporaryDirectory(object):
  624. """Create and return a temporary directory. This has the same
  625. behavior as mkdtemp but can be used as a context manager. For
  626. example:
  627. with TemporaryDirectory() as tmpdir:
  628. ...
  629. Upon exiting the context, the directory and everything contained
  630. in it are removed.
  631. """
  632. def __init__(self, suffix=None, prefix=None, dir=None):
  633. self.name = mkdtemp(suffix, prefix, dir)
  634. self._finalizer = _weakref.finalize(
  635. self, self._cleanup, self.name,
  636. warn_message="Implicitly cleaning up {!r}".format(self))
  637. @classmethod
  638. def _rmtree(cls, name):
  639. def onerror(func, path, exc_info):
  640. if issubclass(exc_info[0], PermissionError):
  641. def resetperms(path):
  642. try:
  643. _os.chflags(path, 0)
  644. except AttributeError:
  645. pass
  646. _os.chmod(path, 0o700)
  647. try:
  648. if path != name:
  649. resetperms(_os.path.dirname(path))
  650. resetperms(path)
  651. try:
  652. _os.unlink(path)
  653. # PermissionError is raised on FreeBSD for directories
  654. except (IsADirectoryError, PermissionError):
  655. cls._rmtree(path)
  656. except FileNotFoundError:
  657. pass
  658. elif issubclass(exc_info[0], FileNotFoundError):
  659. pass
  660. else:
  661. raise
  662. _shutil.rmtree(name, onerror=onerror)
  663. @classmethod
  664. def _cleanup(cls, name, warn_message):
  665. cls._rmtree(name)
  666. _warnings.warn(warn_message, ResourceWarning)
  667. def __repr__(self):
  668. return "<{} {!r}>".format(self.__class__.__name__, self.name)
  669. def __enter__(self):
  670. return self.name
  671. def __exit__(self, exc, value, tb):
  672. self.cleanup()
  673. def cleanup(self):
  674. if self._finalizer.detach():
  675. self._rmtree(self.name)
  676. __class_getitem__ = classmethod(_types.GenericAlias)