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

Side by Side Diff: Lib/tempfile.py

Issue 5178: Add context manager for temporary directory
Patch Set: Created 9 years, 1 month 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 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
35 36
36 try: 37 try:
37 from cStringIO import StringIO as _StringIO 38 from cStringIO import StringIO as _StringIO
38 except ImportError: 39 except ImportError:
39 from StringIO import StringIO as _StringIO 40 from StringIO import StringIO as _StringIO
40 41
41 try: 42 try:
42 import fcntl as _fcntl 43 import fcntl as _fcntl
43 except ImportError: 44 except ImportError:
44 def _set_cloexec(fd): 45 def _set_cloexec(fd):
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after
594 return rv 595 return rv
595 596
596 def writelines(self, iterable): 597 def writelines(self, iterable):
597 file = self._file 598 file = self._file
598 rv = file.writelines(iterable) 599 rv = file.writelines(iterable)
599 self._check(file) 600 self._check(file)
600 return rv 601 return rv
601 602
602 def xreadlines(self, *args): 603 def xreadlines(self, *args):
603 return self._file.xreadlines(*args) 604 return self._file.xreadlines(*args)
605
606
607 class TemporaryDirectory:
608 """Create and return a temporary directory. This has the same
609 behavior as mkdtemp but can be used as a context manager. For
610 example:
611
612 with TemporaryDirectory() as tmpdir:
613 ...
614
615 Upon exiting the context, the directory and everthing contained
616 in it are removed.
617 """
618
619 def __init__(self, suffix="", prefix=template, dir=None):
620 self.name = mkdtemp(suffix, prefix, dir)
621
622 def __enter__(self):
623 return self.name
624
625 def cleanup(self):
626 if _exists(self.name):
627 _rmtree(self.name)
628
629 def __exit__(self, exc, value, tb):
630 self.cleanup()
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+