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

Side by Side Diff: Lib/tempfile.py

Issue 5178: Add context manager for temporary directory
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:
View unified diff | Download patch
« no previous file with comments | « Doc/library/tempfile.rst ('k') | Lib/test/test_bsddb185.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 583 matching lines...) Expand 10 before | Expand all | Expand 10 after
594 return rv 594 return rv
595 595
596 def writelines(self, iterable): 596 def writelines(self, iterable):
597 file = self._file 597 file = self._file
598 rv = file.writelines(iterable) 598 rv = file.writelines(iterable)
599 self._check(file) 599 self._check(file)
600 return rv 600 return rv
601 601
602 def xreadlines(self, *args): 602 def xreadlines(self, *args):
603 return self._file.xreadlines(*args) 603 return self._file.xreadlines(*args)
604
605
606 class TemporaryDirectory(object):
607 """Create and return a temporary directory. This has the same
608 behavior as mkdtemp but can be used as a context manager. For
609 example:
610
611 with TemporaryDirectory() as tmpdir:
612 ...
613
614 Upon exiting the context, the directory and everthing contained
615 in it are removed.
616 """
617
618 def __init__(self, suffix="", prefix=template, dir=None):
619 self.name = mkdtemp(suffix, prefix, dir)
620 self._closed = False
621
622 def __enter__(self):
623 return self.name
624
625 def cleanup(self):
626 if not self._closed:
627 self._rmtree(self.name)
628 self._closed = True
629
630 def __exit__(self, exc, value, tb):
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
OLDNEW
« no previous file with comments | « Doc/library/tempfile.rst ('k') | Lib/test/test_bsddb185.py » ('j') | no next file with comments »

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