Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(16314)

Delta Between Two Patch Sets: Lib/tempfile.py

Issue 5178: Add context manager for temporary directory
Left Patch Set: Created 8 years, 11 months ago
Right Patch Set: Created 8 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Doc/library/tempfile.rst ('k') | Lib/test/test_bsddb185.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 """Temporary files. 1 """Temporary files.
2 2
3 This module provides generic, low- and high-level interfaces for 3 This module provides generic, low- and high-level interfaces for
4 creating temporary files and directories. The interfaces listed 4 creating temporary files and directories. The interfaces listed
5 as "safe" just below can be used without fear of race conditions. 5 as "safe" just below can be used without fear of race conditions.
6 Those listed as "unsafe" cannot, and are provided for backward 6 Those listed as "unsafe" cannot, and are provided for backward
7 compatibility only. 7 compatibility only.
8 8
9 This module also provides some data items to the user: 9 This module also provides some data items to the user:
10 10
(...skipping 14 matching lines...) Expand all
25 "TMP_MAX", "gettempprefix", # constants 25 "TMP_MAX", "gettempprefix", # constants
26 "tempdir", "gettempdir" 26 "tempdir", "gettempdir"
27 ] 27 ]
28 28
29 29
30 # Imports. 30 # Imports.
31 31
32 import os as _os 32 import os as _os
33 import errno as _errno 33 import errno as _errno
34 from random import Random as _Random 34 from random import Random as _Random
35 from shutil import rmtree as _rmtree
36 35
37 try: 36 try:
38 from cStringIO import StringIO as _StringIO 37 from cStringIO import StringIO as _StringIO
39 except ImportError: 38 except ImportError:
40 from StringIO import StringIO as _StringIO 39 from StringIO import StringIO as _StringIO
41 40
42 try: 41 try:
43 import fcntl as _fcntl 42 import fcntl as _fcntl
44 except ImportError: 43 except ImportError:
45 def _set_cloexec(fd): 44 def _set_cloexec(fd):
(...skipping 551 matching lines...) Expand 10 before | Expand all | Expand 10 after
597 def writelines(self, iterable): 596 def writelines(self, iterable):
598 file = self._file 597 file = self._file
599 rv = file.writelines(iterable) 598 rv = file.writelines(iterable)
600 self._check(file) 599 self._check(file)
601 return rv 600 return rv
602 601
603 def xreadlines(self, *args): 602 def xreadlines(self, *args):
604 return self._file.xreadlines(*args) 603 return self._file.xreadlines(*args)
605 604
606 605
607 class TemporaryDirectory: 606 class TemporaryDirectory(object):
608 """Create and return a temporary directory. This has the same 607 """Create and return a temporary directory. This has the same
609 behavior as mkdtemp but can be used as a context manager. For 608 behavior as mkdtemp but can be used as a context manager. For
610 example: 609 example:
611 610
612 with TemporaryDirectory() as tmpdir: 611 with TemporaryDirectory() as tmpdir:
613 ... 612 ...
614 613
615 Upon exiting the context, the directory and everthing contained 614 Upon exiting the context, the directory and everthing contained
616 in it are removed. 615 in it are removed.
617 """ 616 """
618 617
619 def __init__(self, suffix="", prefix=template, dir=None): 618 def __init__(self, suffix="", prefix=template, dir=None):
620 self.name = mkdtemp(suffix, prefix, dir) 619 self.name = mkdtemp(suffix, prefix, dir)
620 self._closed = False
621 621
622 def __enter__(self): 622 def __enter__(self):
623 return self.name 623 return self.name
624 624
625 def cleanup(self): 625 def cleanup(self):
626 if _exists(self.name): 626 if not self._closed:
627 _rmtree(self.name) 627 self._rmtree(self.name)
628 self._closed = True
628 629
629 def __exit__(self, exc, value, tb): 630 def __exit__(self, exc, value, tb):
630 self.cleanup() 631 self.cleanup()
632
633 _listdir = _os.listdir
634 _path_join = _os.path.join
635 _isdir = _os.path.isdir
636 _remove = _os.remove
637 _rmdir = _os.rmdir
638 _os_error = _os.error
639
640 def _rmtree(self, path):
641 # Essentially a stripped down version of shutil.rmtree. We can't
642 # use globals because they may be None'ed out at shutdown.
643 for name in self._listdir(path):
644 fullname = self._path_join(path, name)
645 try:
646 isdir = self._isdir(fullname)
647 except self._os_error:
648 isdir = False
649 if isdir:
650 self._rmtree(fullname)
651 else:
652 try:
653 self._remove(fullname)
654 except self._os_error:
655 pass
656 try:
657 self._rmdir(path)
658 except self._os_error:
659 pass
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+