Mercurial > cpython-lru_cache-weakref
annotate Lib/tempfile.py @ 84007:dbfbdf2b5c19 3.3
Fix #17967: For ftp urls CWD to target instead of hopping to each directory
towards target. This fixes a bug where target is accessible, but parent
directories are restricted.
| author | Senthil Kumaran <senthil@uthcode.com> |
|---|---|
| date | Sat, 01 Jun 2013 08:27:06 -0700 |
| parents | 2fb03fe354e3 |
| children | e07f104133d5 fec33725f319 |
| rev | line source |
|---|---|
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
1 """Temporary files. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
2 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
3 This module provides generic, low- and high-level interfaces for |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
4 creating temporary files and directories. The interfaces listed |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
5 as "safe" just below can be used without fear of race conditions. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
6 Those listed as "unsafe" cannot, and are provided for backward |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
7 compatibility only. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
8 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
9 This module also provides some data items to the user: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
10 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
11 TMP_MAX - maximum number of names that will be tried before |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
12 giving up. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
13 tempdir - If this is set to a string before the first use of |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
14 any routine from this module, it will be considered as |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
15 another candidate location to store temporary files. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
16 """ |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
17 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
18 __all__ = [ |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
19 "NamedTemporaryFile", "TemporaryFile", # high level safe interfaces |
|
65618
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
20 "SpooledTemporaryFile", "TemporaryDirectory", |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
21 "mkstemp", "mkdtemp", # low level safe interfaces |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
22 "mktemp", # deprecated unsafe interface |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
23 "TMP_MAX", "gettempprefix", # constants |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
24 "tempdir", "gettempdir" |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
25 ] |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
26 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
27 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
28 # Imports. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
29 |
|
66843
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
30 import warnings as _warnings |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
31 import sys as _sys |
|
42162
a62eccb53f30
Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
32 import io as _io |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
33 import os as _os |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
34 import errno as _errno |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
35 from random import Random as _Random |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
36 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
37 try: |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
38 import fcntl as _fcntl |
|
32750
5d9f72d0843e
Record that FCNTL.py has gone away; remove FCNTL hack in tempfile.py;
Tim Peters <tim.peters@gmail.com>
parents:
30890
diff
changeset
|
39 except ImportError: |
|
29967
a4abe17a5feb
Windows fix: When PYTHONCASEOK is set, or for any other reason imports
Tim Peters <tim.peters@gmail.com>
parents:
28361
diff
changeset
|
40 def _set_cloexec(fd): |
|
a4abe17a5feb
Windows fix: When PYTHONCASEOK is set, or for any other reason imports
Tim Peters <tim.peters@gmail.com>
parents:
28361
diff
changeset
|
41 pass |
|
a4abe17a5feb
Windows fix: When PYTHONCASEOK is set, or for any other reason imports
Tim Peters <tim.peters@gmail.com>
parents:
28361
diff
changeset
|
42 else: |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
43 def _set_cloexec(fd): |
|
32750
5d9f72d0843e
Record that FCNTL.py has gone away; remove FCNTL hack in tempfile.py;
Tim Peters <tim.peters@gmail.com>
parents:
30890
diff
changeset
|
44 try: |
|
5d9f72d0843e
Record that FCNTL.py has gone away; remove FCNTL hack in tempfile.py;
Tim Peters <tim.peters@gmail.com>
parents:
30890
diff
changeset
|
45 flags = _fcntl.fcntl(fd, _fcntl.F_GETFD, 0) |
|
73169
e4d44c2e8e81
Simplify and remove few dependencies on 'errno', thanks to PEP 3151.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
71609
diff
changeset
|
46 except OSError: |
|
32750
5d9f72d0843e
Record that FCNTL.py has gone away; remove FCNTL hack in tempfile.py;
Tim Peters <tim.peters@gmail.com>
parents:
30890
diff
changeset
|
47 pass |
|
30889
ceec69d240f0
fixed wrong error checking on fcntl call as per SF bug # 821896
Alex Martelli <aleaxit@gmail.com>
parents:
30681
diff
changeset
|
48 else: |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
49 # flags read successfully, modify |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
50 flags |= _fcntl.FD_CLOEXEC |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
51 _fcntl.fcntl(fd, _fcntl.F_SETFD, flags) |
|
29967
a4abe17a5feb
Windows fix: When PYTHONCASEOK is set, or for any other reason imports
Tim Peters <tim.peters@gmail.com>
parents:
28361
diff
changeset
|
52 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
53 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
54 try: |
|
47266
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
47080
diff
changeset
|
55 import _thread |
|
26892
95510f0a0624
Use the dummy_thread module in Queue.py and tempfile.py.
Guido van Rossum <guido@python.org>
parents:
26352
diff
changeset
|
56 except ImportError: |
|
47266
7fe3a8648ce2
Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents:
47080
diff
changeset
|
57 import _dummy_thread as _thread |
|
26892
95510f0a0624
Use the dummy_thread module in Queue.py and tempfile.py.
Guido van Rossum <guido@python.org>
parents:
26352
diff
changeset
|
58 _allocate_lock = _thread.allocate_lock |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
10901
diff
changeset
|
59 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
60 _text_openflags = _os.O_RDWR | _os.O_CREAT | _os.O_EXCL |
|
24715
4c746ba24de2
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24708
diff
changeset
|
61 if hasattr(_os, 'O_NOINHERIT'): |
|
4c746ba24de2
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24708
diff
changeset
|
62 _text_openflags |= _os.O_NOINHERIT |
|
4c746ba24de2
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24708
diff
changeset
|
63 if hasattr(_os, 'O_NOFOLLOW'): |
|
4c746ba24de2
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24708
diff
changeset
|
64 _text_openflags |= _os.O_NOFOLLOW |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
65 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
66 _bin_openflags = _text_openflags |
|
24715
4c746ba24de2
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24708
diff
changeset
|
67 if hasattr(_os, 'O_BINARY'): |
|
4c746ba24de2
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24708
diff
changeset
|
68 _bin_openflags |= _os.O_BINARY |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
69 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
70 if hasattr(_os, 'TMP_MAX'): |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
71 TMP_MAX = _os.TMP_MAX |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
72 else: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
73 TMP_MAX = 10000 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
74 |
|
70932
eafe8c7f7049
#10354: remove last public mentions of 'template' and comment as private.
R David Murray <rdmurray@bitdance.com>
parents:
66843
diff
changeset
|
75 # Although it does not have an underscore for historical reasons, this |
|
eafe8c7f7049
#10354: remove last public mentions of 'template' and comment as private.
R David Murray <rdmurray@bitdance.com>
parents:
66843
diff
changeset
|
76 # variable is an internal implementation detail (see issue 10354). |
|
24799
d9ff97ce75bb
template: removed special-casing for NT; there isn't an 8-character limit.
Tim Peters <tim.peters@gmail.com>
parents:
24798
diff
changeset
|
77 template = "tmp" |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
78 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
79 # Internal routines. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
80 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
81 _once_lock = _allocate_lock() |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
82 |
|
30890
1997a14f6b08
mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents:
30889
diff
changeset
|
83 if hasattr(_os, "lstat"): |
|
1997a14f6b08
mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents:
30889
diff
changeset
|
84 _stat = _os.lstat |
|
1997a14f6b08
mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents:
30889
diff
changeset
|
85 elif hasattr(_os, "stat"): |
|
1997a14f6b08
mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents:
30889
diff
changeset
|
86 _stat = _os.stat |
|
1997a14f6b08
mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents:
30889
diff
changeset
|
87 else: |
|
73169
e4d44c2e8e81
Simplify and remove few dependencies on 'errno', thanks to PEP 3151.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
71609
diff
changeset
|
88 # Fallback. All we need is something that raises OSError if the |
|
30890
1997a14f6b08
mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents:
30889
diff
changeset
|
89 # file doesn't exist. |
|
1997a14f6b08
mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents:
30889
diff
changeset
|
90 def _stat(fn): |
|
73170
5bd8a04206e8
Remove no-op code from previous commit.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
73169
diff
changeset
|
91 f = open(fn) |
|
30890
1997a14f6b08
mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents:
30889
diff
changeset
|
92 f.close() |
|
1997a14f6b08
mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents:
30889
diff
changeset
|
93 |
|
1997a14f6b08
mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents:
30889
diff
changeset
|
94 def _exists(fn): |
|
1997a14f6b08
mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents:
30889
diff
changeset
|
95 try: |
|
1997a14f6b08
mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents:
30889
diff
changeset
|
96 _stat(fn) |
|
73169
e4d44c2e8e81
Simplify and remove few dependencies on 'errno', thanks to PEP 3151.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
71609
diff
changeset
|
97 except OSError: |
|
30890
1997a14f6b08
mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents:
30889
diff
changeset
|
98 return False |
|
1997a14f6b08
mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents:
30889
diff
changeset
|
99 else: |
|
1997a14f6b08
mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents:
30889
diff
changeset
|
100 return True |
|
1997a14f6b08
mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents:
30889
diff
changeset
|
101 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
102 class _RandomNameSequence: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
103 """An instance of _RandomNameSequence generates an endless |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
104 sequence of unpredictable strings which can safely be incorporated |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
105 into file names. Each string is six characters long. Multiple |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
106 threads can safely use the same instance at the same time. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
107 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
108 _RandomNameSequence is an iterator.""" |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
109 |
| 66102 | 110 characters = "abcdefghijklmnopqrstuvwxyz0123456789_" |
|
773
c721afab0efa
Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents:
586
diff
changeset
|
111 |
|
73743
3c9ddd93c983
Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
71608
diff
changeset
|
112 @property |
|
3c9ddd93c983
Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
71608
diff
changeset
|
113 def rng(self): |
|
3c9ddd93c983
Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
71608
diff
changeset
|
114 cur_pid = _os.getpid() |
|
3c9ddd93c983
Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
71608
diff
changeset
|
115 if cur_pid != getattr(self, '_rng_pid', None): |
|
3c9ddd93c983
Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
71608
diff
changeset
|
116 self._rng = _Random() |
|
3c9ddd93c983
Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
71608
diff
changeset
|
117 self._rng_pid = cur_pid |
|
3c9ddd93c983
Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents:
71608
diff
changeset
|
118 return self._rng |
|
26315
3fd8c2aff305
_RandomNameSequence(): style guide changes, small speedup, don't
Tim Peters <tim.peters@gmail.com>
parents:
26314
diff
changeset
|
119 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
120 def __iter__(self): |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
121 return self |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
122 |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40587
diff
changeset
|
123 def __next__(self): |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
124 c = self.characters |
|
26315
3fd8c2aff305
_RandomNameSequence(): style guide changes, small speedup, don't
Tim Peters <tim.peters@gmail.com>
parents:
26314
diff
changeset
|
125 choose = self.rng.choice |
| 66102 | 126 letters = [choose(c) for dummy in "123456"] |
| 127 return ''.join(letters) | |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
128 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
129 def _candidate_tempdir_list(): |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
130 """Generate a list of candidate temporary directories which |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
131 _get_default_tempdir will try.""" |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
132 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
133 dirlist = [] |
|
773
c721afab0efa
Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents:
586
diff
changeset
|
134 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
135 # First, try the environment. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
136 for envname in 'TMPDIR', 'TEMP', 'TMP': |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
137 dirname = _os.getenv(envname) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
138 if dirname: dirlist.append(dirname) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
139 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
140 # Failing that, try OS-specific locations. |
|
47080
58a0d5524f9a
Merged revisions 63119-63128,63130-63131,63133,63135-63144,63146-63148,63151-63152,63155-63165,63167-63176,63181-63186,63188-63189 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
141 if _os.name == 'nt': |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
142 dirlist.extend([ r'c:\temp', r'c:\tmp', r'\temp', r'\tmp' ]) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
143 else: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
144 dirlist.extend([ '/tmp', '/var/tmp', '/usr/tmp' ]) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
145 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
146 # As a last resort, the current directory. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
147 try: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
148 dirlist.append(_os.getcwd()) |
|
73169
e4d44c2e8e81
Simplify and remove few dependencies on 'errno', thanks to PEP 3151.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
71609
diff
changeset
|
149 except (AttributeError, OSError): |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
150 dirlist.append(_os.curdir) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
151 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
152 return dirlist |
|
24715
4c746ba24de2
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24708
diff
changeset
|
153 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
154 def _get_default_tempdir(): |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
155 """Calculate the default directory to use for temporary files. |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
156 This routine should be called exactly once. |
|
16889
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
16678
diff
changeset
|
157 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
158 We determine whether or not a candidate temp dir is usable by |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
159 trying to create and write to a file in that directory. If this |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
160 is successful, the test file is deleted. To prevent denial of |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
161 service, the name of the test file must be randomized.""" |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
162 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
163 namer = _RandomNameSequence() |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
164 dirlist = _candidate_tempdir_list() |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
165 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
166 for dir in dirlist: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
167 if dir != _os.curdir: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
168 dir = _os.path.normcase(_os.path.abspath(dir)) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
169 # Try only a few names per directory. |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
170 for seq in range(100): |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40587
diff
changeset
|
171 name = next(namer) |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
172 filename = _os.path.join(dir, name) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
173 try: |
|
57514
9e51a14998a2
Merged revisions 76593 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
55598
diff
changeset
|
174 fd = _os.open(filename, _bin_openflags, 0o600) |
|
82182
377123f10820
Issue #16800: tempfile.gettempdir() no longer left temporary files when
Serhiy Storchaka <storchaka@gmail.com>
parents:
82086
diff
changeset
|
175 try: |
|
377123f10820
Issue #16800: tempfile.gettempdir() no longer left temporary files when
Serhiy Storchaka <storchaka@gmail.com>
parents:
82086
diff
changeset
|
176 try: |
|
82186
4622206db91b
Fix for issue #16800: Use buffered write to handle EINTR.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82182
diff
changeset
|
177 with _io.open(fd, 'wb', closefd=False) as fp: |
|
4622206db91b
Fix for issue #16800: Use buffered write to handle EINTR.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82182
diff
changeset
|
178 fp.write(b'blat') |
|
82182
377123f10820
Issue #16800: tempfile.gettempdir() no longer left temporary files when
Serhiy Storchaka <storchaka@gmail.com>
parents:
82086
diff
changeset
|
179 finally: |
|
377123f10820
Issue #16800: tempfile.gettempdir() no longer left temporary files when
Serhiy Storchaka <storchaka@gmail.com>
parents:
82086
diff
changeset
|
180 _os.close(fd) |
|
377123f10820
Issue #16800: tempfile.gettempdir() no longer left temporary files when
Serhiy Storchaka <storchaka@gmail.com>
parents:
82086
diff
changeset
|
181 finally: |
|
377123f10820
Issue #16800: tempfile.gettempdir() no longer left temporary files when
Serhiy Storchaka <storchaka@gmail.com>
parents:
82086
diff
changeset
|
182 _os.unlink(filename) |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
183 return dir |
|
73169
e4d44c2e8e81
Simplify and remove few dependencies on 'errno', thanks to PEP 3151.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
71609
diff
changeset
|
184 except FileExistsError: |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
185 pass |
|
73169
e4d44c2e8e81
Simplify and remove few dependencies on 'errno', thanks to PEP 3151.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
71609
diff
changeset
|
186 except OSError: |
|
e4d44c2e8e81
Simplify and remove few dependencies on 'errno', thanks to PEP 3151.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
71609
diff
changeset
|
187 break # no point trying more names in this directory |
|
82104
11eaa61124c2
Issue #17169: Restore errno in tempfile exceptions.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82087
diff
changeset
|
188 raise FileNotFoundError(_errno.ENOENT, |
|
11eaa61124c2
Issue #17169: Restore errno in tempfile exceptions.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82087
diff
changeset
|
189 "No usable temporary directory found in %s" % |
|
11eaa61124c2
Issue #17169: Restore errno in tempfile exceptions.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82087
diff
changeset
|
190 dirlist) |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
191 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
192 _name_sequence = None |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
193 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
194 def _get_candidate_names(): |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
195 """Common setup sequence for all user-callable interfaces.""" |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
196 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
197 global _name_sequence |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
198 if _name_sequence is None: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
199 _once_lock.acquire() |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
200 try: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
201 if _name_sequence is None: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
202 _name_sequence = _RandomNameSequence() |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
203 finally: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
204 _once_lock.release() |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
205 return _name_sequence |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
206 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
207 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
208 def _mkstemp_inner(dir, pre, suf, flags): |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
209 """Code common to mkstemp, TemporaryFile, and NamedTemporaryFile.""" |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
210 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
211 names = _get_candidate_names() |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
212 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
213 for seq in range(TMP_MAX): |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40587
diff
changeset
|
214 name = next(names) |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
215 file = _os.path.join(dir, pre + name + suf) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
216 try: |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41760
diff
changeset
|
217 fd = _os.open(file, flags, 0o600) |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
218 _set_cloexec(fd) |
|
30681
9691cc644160
Patch #810914: Return absolute path for mkstemp. Fixes #810408.
Martin v. Löwis <martin@v.loewis.de>
parents:
29967
diff
changeset
|
219 return (fd, _os.path.abspath(file)) |
|
73169
e4d44c2e8e81
Simplify and remove few dependencies on 'errno', thanks to PEP 3151.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
71609
diff
changeset
|
220 except FileExistsError: |
|
e4d44c2e8e81
Simplify and remove few dependencies on 'errno', thanks to PEP 3151.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
71609
diff
changeset
|
221 continue # try again |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
222 |
|
82104
11eaa61124c2
Issue #17169: Restore errno in tempfile exceptions.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82087
diff
changeset
|
223 raise FileExistsError(_errno.EEXIST, |
|
11eaa61124c2
Issue #17169: Restore errno in tempfile exceptions.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82087
diff
changeset
|
224 "No usable temporary file name found") |
|
24715
4c746ba24de2
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24708
diff
changeset
|
225 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
226 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
227 # User visible interfaces. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
228 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
229 def gettempprefix(): |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
230 """Accessor for tempdir.template.""" |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
231 return template |
| 430 | 232 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
233 tempdir = None |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
234 |
|
773
c721afab0efa
Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents:
586
diff
changeset
|
235 def gettempdir(): |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45561
diff
changeset
|
236 """Accessor for tempfile.tempdir.""" |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
237 global tempdir |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
238 if tempdir is None: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
239 _once_lock.acquire() |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
240 try: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
241 if tempdir is None: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
242 tempdir = _get_default_tempdir() |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
243 finally: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
244 _once_lock.release() |
|
3647
16ca8f0f3f11
Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents:
773
diff
changeset
|
245 return tempdir |
|
586
dbf408918201
Use $TMPDIR if it is set in the environment.
Guido van Rossum <guido@python.org>
parents:
534
diff
changeset
|
246 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
247 def mkstemp(suffix="", prefix=template, dir=None, text=False): |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45561
diff
changeset
|
248 """User-callable function to create and return a unique temporary |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
249 file. The return value is a pair (fd, name) where fd is the |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
250 file descriptor returned by os.open, and name is the filename. |
|
773
c721afab0efa
Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents:
586
diff
changeset
|
251 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
252 If 'suffix' is specified, the file name will end with that suffix, |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
253 otherwise there will be no suffix. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
254 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
255 If 'prefix' is specified, the file name will begin with that prefix, |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
256 otherwise a default prefix is used. |
|
15840
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
15827
diff
changeset
|
257 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
258 If 'dir' is specified, the file will be created in that directory, |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
259 otherwise a default directory is used. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
260 |
|
24816
c45642e1dca9
tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents:
24813
diff
changeset
|
261 If 'text' is specified and true, the file is opened in text |
|
c45642e1dca9
tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents:
24813
diff
changeset
|
262 mode. Else (the default) the file is opened in binary mode. On |
|
c45642e1dca9
tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents:
24813
diff
changeset
|
263 some operating systems, this makes no difference. |
|
9364
9f135b3d9a78
Fix so that after a fork() -- on Unix only -- the template gets
Guido van Rossum <guido@python.org>
parents:
8176
diff
changeset
|
264 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
265 The file is readable and writable only by the creating user ID. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
266 If the operating system uses permission bits to indicate whether a |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
267 file is executable, the file is executable by no one. The file |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
268 descriptor is not inherited by children of this process. |
|
15840
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
15827
diff
changeset
|
269 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
270 Caller is responsible for deleting the file when done with it. |
|
15840
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
15827
diff
changeset
|
271 """ |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
15827
diff
changeset
|
272 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
273 if dir is None: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
274 dir = gettempdir() |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
275 |
|
24816
c45642e1dca9
tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents:
24813
diff
changeset
|
276 if text: |
|
c45642e1dca9
tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents:
24813
diff
changeset
|
277 flags = _text_openflags |
|
c45642e1dca9
tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents:
24813
diff
changeset
|
278 else: |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
279 flags = _bin_openflags |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
280 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
281 return _mkstemp_inner(dir, prefix, suffix, flags) |
|
586
dbf408918201
Use $TMPDIR if it is set in the environment.
Guido van Rossum <guido@python.org>
parents:
534
diff
changeset
|
282 |
| 430 | 283 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
284 def mkdtemp(suffix="", prefix=template, dir=None): |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45561
diff
changeset
|
285 """User-callable function to create and return a unique temporary |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
286 directory. The return value is the pathname of the directory. |
|
5987
0a2c331200f5
Added TemporaryFile and wrapper class by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
5305
diff
changeset
|
287 |
|
24816
c45642e1dca9
tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents:
24813
diff
changeset
|
288 Arguments are as for mkstemp, except that the 'text' argument is |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
289 not accepted. |
|
5987
0a2c331200f5
Added TemporaryFile and wrapper class by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
5305
diff
changeset
|
290 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
291 The directory is readable, writable, and searchable only by the |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
292 creating user. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
293 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
294 Caller is responsible for deleting the directory when done with it. |
|
5987
0a2c331200f5
Added TemporaryFile and wrapper class by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
5305
diff
changeset
|
295 """ |
|
21474
a3b0673ba2ca
TemporaryFileWrapper: cache the value of os.unlink for use by __del__,
Tim Peters <tim.peters@gmail.com>
parents:
20752
diff
changeset
|
296 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
297 if dir is None: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
298 dir = gettempdir() |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
299 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
300 names = _get_candidate_names() |
|
24715
4c746ba24de2
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24708
diff
changeset
|
301 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
302 for seq in range(TMP_MAX): |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40587
diff
changeset
|
303 name = next(names) |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
304 file = _os.path.join(dir, prefix + name + suffix) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
305 try: |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41760
diff
changeset
|
306 _os.mkdir(file, 0o700) |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
307 return file |
|
73169
e4d44c2e8e81
Simplify and remove few dependencies on 'errno', thanks to PEP 3151.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
71609
diff
changeset
|
308 except FileExistsError: |
|
e4d44c2e8e81
Simplify and remove few dependencies on 'errno', thanks to PEP 3151.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
71609
diff
changeset
|
309 continue # try again |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
310 |
|
82104
11eaa61124c2
Issue #17169: Restore errno in tempfile exceptions.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82087
diff
changeset
|
311 raise FileExistsError(_errno.EEXIST, |
|
11eaa61124c2
Issue #17169: Restore errno in tempfile exceptions.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82087
diff
changeset
|
312 "No usable temporary directory name found") |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
313 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
314 def mktemp(suffix="", prefix=template, dir=None): |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45561
diff
changeset
|
315 """User-callable function to return a unique temporary file name. The |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
316 file is not created. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
317 |
|
24816
c45642e1dca9
tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents:
24813
diff
changeset
|
318 Arguments are as for mkstemp, except that the 'text' argument is |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
319 not accepted. |
|
21474
a3b0673ba2ca
TemporaryFileWrapper: cache the value of os.unlink for use by __del__,
Tim Peters <tim.peters@gmail.com>
parents:
20752
diff
changeset
|
320 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
321 This function is unsafe and should not be used. The file name |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
322 refers to a file that did not exist at some point, but by the time |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
323 you get around to creating it, someone else may have beaten you to |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
324 the punch. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
325 """ |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
326 |
|
26352
dc399e9cab89
Comment out the warnings about mktemp(). These are too annoying, and
Guido van Rossum <guido@python.org>
parents:
26315
diff
changeset
|
327 ## from warnings import warn as _warn |
|
dc399e9cab89
Comment out the warnings about mktemp(). These are too annoying, and
Guido van Rossum <guido@python.org>
parents:
26315
diff
changeset
|
328 ## _warn("mktemp is a potential security risk to your program", |
|
dc399e9cab89
Comment out the warnings about mktemp(). These are too annoying, and
Guido van Rossum <guido@python.org>
parents:
26315
diff
changeset
|
329 ## RuntimeWarning, stacklevel=2) |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
330 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
331 if dir is None: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
332 dir = gettempdir() |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
333 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
334 names = _get_candidate_names() |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
335 for seq in range(TMP_MAX): |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40587
diff
changeset
|
336 name = next(names) |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
337 file = _os.path.join(dir, prefix + name + suffix) |
|
30890
1997a14f6b08
mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents:
30889
diff
changeset
|
338 if not _exists(file): |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
339 return file |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
340 |
|
82104
11eaa61124c2
Issue #17169: Restore errno in tempfile exceptions.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82087
diff
changeset
|
341 raise FileExistsError(_errno.EEXIST, |
|
11eaa61124c2
Issue #17169: Restore errno in tempfile exceptions.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82087
diff
changeset
|
342 "No usable temporary filename found") |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
343 |
|
45256
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
344 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
345 class _TemporaryFileWrapper: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
346 """Temporary file wrapper |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
347 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
348 This class provides a wrapper around files opened for |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
349 temporary use. In particular, it seeks to automatically |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
350 remove the file when it is no longer needed. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
351 """ |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
352 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
353 def __init__(self, file, name, delete=True): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6924
diff
changeset
|
354 self.file = file |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
355 self.name = name |
|
26314
8e6013bbe7a7
_TemporaryFileWrapper: changed self.close_called to a proper bool.
Tim Peters <tim.peters@gmail.com>
parents:
24874
diff
changeset
|
356 self.close_called = False |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
357 self.delete = delete |
|
5987
0a2c331200f5
Added TemporaryFile and wrapper class by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
5305
diff
changeset
|
358 |
|
0a2c331200f5
Added TemporaryFile and wrapper class by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
5305
diff
changeset
|
359 def __getattr__(self, name): |
|
45256
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
360 # Attribute lookups are delegated to the underlying file |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
361 # and cached for non-numeric results |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
362 # (i.e. methods are cached, closed and friends are not) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6924
diff
changeset
|
363 file = self.__dict__['file'] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6924
diff
changeset
|
364 a = getattr(file, name) |
|
45256
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
365 if not isinstance(a, int): |
|
10901
17ca2f64b6d6
In class TemporaryFileWrapper, don't cache attributes of tpye int --
Guido van Rossum <guido@python.org>
parents:
9447
diff
changeset
|
366 setattr(self, name, a) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6924
diff
changeset
|
367 return a |
|
5987
0a2c331200f5
Added TemporaryFile and wrapper class by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
5305
diff
changeset
|
368 |
|
45256
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
369 # The underlying __enter__ method returns the wrong object |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
370 # (self.file) so override it to return the wrapper |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
371 def __enter__(self): |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
372 self.file.__enter__() |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
373 return self |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
374 |
| 45561 | 375 # iter() doesn't use __getattr__ to find the __iter__ method |
|
45558
c38dcb7d94a6
iter() doesn't use __getattr__ to find an __iter__ method. I'm not sure if the behavior is deliberately but this workaround fixes the issue for the next alpha release tomorrow.
Christian Heimes <christian@cheimes.de>
parents:
45256
diff
changeset
|
376 def __iter__(self): |
| 45561 | 377 return iter(self.file) |
|
45558
c38dcb7d94a6
iter() doesn't use __getattr__ to find an __iter__ method. I'm not sure if the behavior is deliberately but this workaround fixes the issue for the next alpha release tomorrow.
Christian Heimes <christian@cheimes.de>
parents:
45256
diff
changeset
|
378 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
379 # NT provides delete-on-close as a primitive, so we don't need |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
380 # the wrapper to do anything special. We still use it so that |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
381 # file.name is useful (i.e. not "(fdopen)") with NamedTemporaryFile. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
382 if _os.name != 'nt': |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
383 # Cache the unlinker so we don't get spurious errors at |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
384 # shutdown when the module-level "os" is None'd out. Note |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
385 # that this must be referenced as self.unlink, because the |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
386 # name TemporaryFileWrapper may also get None'd out before |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
387 # __del__ is called. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
388 unlink = _os.unlink |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
389 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
390 def close(self): |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
391 if not self.close_called: |
|
26314
8e6013bbe7a7
_TemporaryFileWrapper: changed self.close_called to a proper bool.
Tim Peters <tim.peters@gmail.com>
parents:
24874
diff
changeset
|
392 self.close_called = True |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
393 self.file.close() |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
394 if self.delete: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
395 self.unlink(self.name) |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
396 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
397 def __del__(self): |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
398 self.close() |
|
5987
0a2c331200f5
Added TemporaryFile and wrapper class by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
5305
diff
changeset
|
399 |
|
45256
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
400 # Need to trap __exit__ as well to ensure the file gets |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
401 # deleted when used in a with statement |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
402 def __exit__(self, exc, value, tb): |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
403 result = self.file.__exit__(exc, value, tb) |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
404 self.close() |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
405 return result |
|
55592
10940bcedc99
Merged revisions 73695,73711 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47630
diff
changeset
|
406 else: |
|
10940bcedc99
Merged revisions 73695,73711 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47630
diff
changeset
|
407 def __exit__(self, exc, value, tb): |
|
55598
743fa282d748
Merged revisions 73749 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55592
diff
changeset
|
408 self.file.__exit__(exc, value, tb) |
|
45256
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
409 |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
410 |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
411 def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
412 newline=None, suffix="", prefix=template, |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
413 dir=None, delete=True): |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
414 """Create and return a temporary file. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
415 Arguments: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
416 'prefix', 'suffix', 'dir' -- as for mkstemp. |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
417 'mode' -- the mode argument to io.open (default "w+b"). |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
418 'buffering' -- the buffer size argument to io.open (default -1). |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
419 'encoding' -- the encoding argument to io.open (default None) |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
420 'newline' -- the newline argument to io.open (default None) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
421 'delete' -- whether the file is deleted on close (default True). |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
422 The file is created as mkstemp() would do it. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
423 |
|
34399
d7968621d28d
SF bug #1099516: tempfile files not types.FileType
Raymond Hettinger <python@rcn.com>
parents:
32750
diff
changeset
|
424 Returns an object with a file-like interface; the name of the file |
|
d7968621d28d
SF bug #1099516: tempfile files not types.FileType
Raymond Hettinger <python@rcn.com>
parents:
32750
diff
changeset
|
425 is accessible as file.name. The file will be automatically deleted |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
426 when it is closed unless the 'delete' argument is set to False. |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
427 """ |
|
15827
81db92e80ea8
A variant of SF patch 103028 (Make tempfile.mktemp threadsafe).
Tim Peters <tim.peters@gmail.com>
parents:
14242
diff
changeset
|
428 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
429 if dir is None: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
430 dir = gettempdir() |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
431 |
|
57514
9e51a14998a2
Merged revisions 76593 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
55598
diff
changeset
|
432 flags = _bin_openflags |
|
15827
81db92e80ea8
A variant of SF patch 103028 (Make tempfile.mktemp threadsafe).
Tim Peters <tim.peters@gmail.com>
parents:
14242
diff
changeset
|
433 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
434 # Setting O_TEMPORARY in the flags causes the OS to delete |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
435 # the file when it is closed. This is only supported by Windows. |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
436 if _os.name == 'nt' and delete: |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
437 flags |= _os.O_TEMPORARY |
|
15827
81db92e80ea8
A variant of SF patch 103028 (Make tempfile.mktemp threadsafe).
Tim Peters <tim.peters@gmail.com>
parents:
14242
diff
changeset
|
438 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
439 (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags) |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
440 file = _io.open(fd, mode, buffering=buffering, |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
441 newline=newline, encoding=encoding) |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
442 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
443 return _TemporaryFileWrapper(file, name, delete) |
|
15827
81db92e80ea8
A variant of SF patch 103028 (Make tempfile.mktemp threadsafe).
Tim Peters <tim.peters@gmail.com>
parents:
14242
diff
changeset
|
444 |
|
24813
7b7d8820ae38
Patch #595014: Cygwin tempfile patch
Jason Tishler <jason@tishler.net>
parents:
24802
diff
changeset
|
445 if _os.name != 'posix' or _os.sys.platform == 'cygwin': |
|
7b7d8820ae38
Patch #595014: Cygwin tempfile patch
Jason Tishler <jason@tishler.net>
parents:
24802
diff
changeset
|
446 # On non-POSIX and Cygwin systems, assume that we cannot unlink a file |
|
7b7d8820ae38
Patch #595014: Cygwin tempfile patch
Jason Tishler <jason@tishler.net>
parents:
24802
diff
changeset
|
447 # while it is open. |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
448 TemporaryFile = NamedTemporaryFile |
|
15827
81db92e80ea8
A variant of SF patch 103028 (Make tempfile.mktemp threadsafe).
Tim Peters <tim.peters@gmail.com>
parents:
14242
diff
changeset
|
449 |
|
81db92e80ea8
A variant of SF patch 103028 (Make tempfile.mktemp threadsafe).
Tim Peters <tim.peters@gmail.com>
parents:
14242
diff
changeset
|
450 else: |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
451 def TemporaryFile(mode='w+b', buffering=-1, encoding=None, |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
452 newline=None, suffix="", prefix=template, |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
453 dir=None): |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
454 """Create and return a temporary file. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
455 Arguments: |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
34399
diff
changeset
|
456 'prefix', 'suffix', 'dir' -- as for mkstemp. |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
457 'mode' -- the mode argument to io.open (default "w+b"). |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
458 'buffering' -- the buffer size argument to io.open (default -1). |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
459 'encoding' -- the encoding argument to io.open (default None) |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
460 'newline' -- the newline argument to io.open (default None) |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
461 The file is created as mkstemp() would do it. |
|
15827
81db92e80ea8
A variant of SF patch 103028 (Make tempfile.mktemp threadsafe).
Tim Peters <tim.peters@gmail.com>
parents:
14242
diff
changeset
|
462 |
|
34399
d7968621d28d
SF bug #1099516: tempfile files not types.FileType
Raymond Hettinger <python@rcn.com>
parents:
32750
diff
changeset
|
463 Returns an object with a file-like interface. The file has no |
|
d7968621d28d
SF bug #1099516: tempfile files not types.FileType
Raymond Hettinger <python@rcn.com>
parents:
32750
diff
changeset
|
464 name, and will cease to exist when it is closed. |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
465 """ |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
466 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
467 if dir is None: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
468 dir = gettempdir() |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
469 |
|
57514
9e51a14998a2
Merged revisions 76593 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
55598
diff
changeset
|
470 flags = _bin_openflags |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
471 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
472 (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
473 try: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
474 _os.unlink(name) |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
475 return _io.open(fd, mode, buffering=buffering, |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
476 newline=newline, encoding=encoding) |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
477 except: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
478 _os.close(fd) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
479 raise |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
480 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
481 class SpooledTemporaryFile: |
|
82080
fb4ed16f35bd
Issue #17147. Mention BytesIO in SpooledTemporaryFile documentation.
Serhiy Storchaka <storchaka@gmail.com>
parents:
80928
diff
changeset
|
482 """Temporary file wrapper, specialized to switch from BytesIO |
|
fb4ed16f35bd
Issue #17147. Mention BytesIO in SpooledTemporaryFile documentation.
Serhiy Storchaka <storchaka@gmail.com>
parents:
80928
diff
changeset
|
483 or StringIO to a real file when it exceeds a certain size or |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
484 when a fileno is needed. |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
485 """ |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
486 _rolled = False |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
487 |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
488 def __init__(self, max_size=0, mode='w+b', buffering=-1, |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
489 encoding=None, newline=None, |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
490 suffix="", prefix=template, dir=None): |
|
42162
a62eccb53f30
Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
491 if 'b' in mode: |
|
a62eccb53f30
Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
492 self._file = _io.BytesIO() |
|
a62eccb53f30
Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
493 else: |
|
43705
51a7374105ac
Issue 1340 by Amaury Forgeot d'Arc (with help from Christian Heimes,
Guido van Rossum <guido@python.org>
parents:
43634
diff
changeset
|
494 # Setting newline="\n" avoids newline translation; |
|
51a7374105ac
Issue 1340 by Amaury Forgeot d'Arc (with help from Christian Heimes,
Guido van Rossum <guido@python.org>
parents:
43634
diff
changeset
|
495 # this is important because otherwise on Windows we'd |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43706
diff
changeset
|
496 # hget double newline translation upon rollover(). |
|
47630
a021ff11718b
Fixed test_tempfile.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47266
diff
changeset
|
497 self._file = _io.StringIO(newline="\n") |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
498 self._max_size = max_size |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
499 self._rolled = False |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
500 self._TemporaryFileArgs = {'mode': mode, 'buffering': buffering, |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
501 'suffix': suffix, 'prefix': prefix, |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
502 'encoding': encoding, 'newline': newline, |
|
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
503 'dir': dir} |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
504 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
505 def _check(self, file): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
506 if self._rolled: return |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
507 max_size = self._max_size |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
508 if max_size and file.tell() > max_size: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
509 self.rollover() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
510 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
511 def rollover(self): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
512 if self._rolled: return |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
513 file = self._file |
|
42931
aee21e0c9a70
Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents:
42632
diff
changeset
|
514 newfile = self._file = TemporaryFile(**self._TemporaryFileArgs) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
515 del self._TemporaryFileArgs |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
516 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
517 newfile.write(file.getvalue()) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
518 newfile.seek(file.tell(), 0) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
519 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
520 self._rolled = True |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
521 |
|
45256
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
522 # The method caching trick from NamedTemporaryFile |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
523 # won't work here, because _file may change from a |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
524 # _StringIO instance to a real file. So we list |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
525 # all the methods directly. |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
526 |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
527 # Context management protocol |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
528 def __enter__(self): |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
529 if self._file.closed: |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
530 raise ValueError("Cannot enter context with closed file") |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
531 return self |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
532 |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
533 def __exit__(self, exc, value, tb): |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
534 self._file.close() |
|
bc62854ee9c2
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
535 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
536 # file protocol |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
537 def __iter__(self): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
538 return self._file.__iter__() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
539 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
540 def close(self): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
541 self._file.close() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
542 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
543 @property |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
544 def closed(self): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
545 return self._file.closed |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
546 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
547 @property |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
548 def encoding(self): |
|
82086
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
549 try: |
|
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
550 return self._file.encoding |
|
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
551 except AttributeError: |
|
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
552 if 'b' in self._TemporaryFileArgs['mode']: |
|
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
553 raise |
|
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
554 return self._TemporaryFileArgs['encoding'] |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
555 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
556 def fileno(self): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
557 self.rollover() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
558 return self._file.fileno() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
559 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
560 def flush(self): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
561 self._file.flush() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
562 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
563 def isatty(self): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
564 return self._file.isatty() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
565 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
566 @property |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
567 def mode(self): |
|
82086
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
568 try: |
|
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
569 return self._file.mode |
|
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
570 except AttributeError: |
|
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
571 return self._TemporaryFileArgs['mode'] |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
572 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
573 @property |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
574 def name(self): |
|
82086
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
575 try: |
|
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
576 return self._file.name |
|
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
577 except AttributeError: |
|
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
578 return None |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
579 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
580 @property |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
581 def newlines(self): |
|
82086
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
582 try: |
|
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
583 return self._file.newlines |
|
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
584 except AttributeError: |
|
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
585 if 'b' in self._TemporaryFileArgs['mode']: |
|
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
586 raise |
|
dfc6902b63d7
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82080
diff
changeset
|
587 return self._TemporaryFileArgs['newline'] |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
588 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
589 def read(self, *args): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
590 return self._file.read(*args) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
591 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
592 def readline(self, *args): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
593 return self._file.readline(*args) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
594 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
595 def readlines(self, *args): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
596 return self._file.readlines(*args) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
597 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
598 def seek(self, *args): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
599 self._file.seek(*args) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
600 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
601 @property |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
602 def softspace(self): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
603 return self._file.softspace |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
604 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
605 def tell(self): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
606 return self._file.tell() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
607 |
|
73737
5a6911930bad
Issue #9957: SpooledTemporaryFile.truncate() now accepts an optional size parameter, as other file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73170
diff
changeset
|
608 def truncate(self, size=None): |
|
5a6911930bad
Issue #9957: SpooledTemporaryFile.truncate() now accepts an optional size parameter, as other file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73170
diff
changeset
|
609 if size is None: |
|
5a6911930bad
Issue #9957: SpooledTemporaryFile.truncate() now accepts an optional size parameter, as other file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73170
diff
changeset
|
610 self._file.truncate() |
|
5a6911930bad
Issue #9957: SpooledTemporaryFile.truncate() now accepts an optional size parameter, as other file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73170
diff
changeset
|
611 else: |
|
5a6911930bad
Issue #9957: SpooledTemporaryFile.truncate() now accepts an optional size parameter, as other file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73170
diff
changeset
|
612 if size > self._max_size: |
|
5a6911930bad
Issue #9957: SpooledTemporaryFile.truncate() now accepts an optional size parameter, as other file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73170
diff
changeset
|
613 self.rollover() |
|
5a6911930bad
Issue #9957: SpooledTemporaryFile.truncate() now accepts an optional size parameter, as other file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73170
diff
changeset
|
614 self._file.truncate(size) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
615 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
616 def write(self, s): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
617 file = self._file |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
618 rv = file.write(s) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
619 self._check(file) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
620 return rv |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
621 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
622 def writelines(self, iterable): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
623 file = self._file |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
624 rv = file.writelines(iterable) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
625 self._check(file) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
626 return rv |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
627 |
|
65618
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
628 |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
629 class TemporaryDirectory(object): |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
630 """Create and return a temporary directory. This has the same |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
631 behavior as mkdtemp but can be used as a context manager. For |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
632 example: |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
633 |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
634 with TemporaryDirectory() as tmpdir: |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
635 ... |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
636 |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
637 Upon exiting the context, the directory and everthing contained |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
638 in it are removed. |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
639 """ |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
640 |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
641 def __init__(self, suffix="", prefix=template, dir=None): |
|
66843
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
642 self._closed = False |
|
80928
fdf907708f49
Issue #16714: use 'raise' exceptions, don't 'throw'.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
73743
diff
changeset
|
643 self.name = None # Handle mkdtemp raising an exception |
|
65618
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
644 self.name = mkdtemp(suffix, prefix, dir) |
|
66843
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
645 |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
646 def __repr__(self): |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
647 return "<{} {!r}>".format(self.__class__.__name__, self.name) |
|
65618
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
648 |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
649 def __enter__(self): |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
650 return self.name |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
651 |
|
66843
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
652 def cleanup(self, _warn=False): |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
653 if self.name and not self._closed: |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
654 try: |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
655 self._rmtree(self.name) |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
656 except (TypeError, AttributeError) as ex: |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
657 # Issue #10188: Emit a warning on stderr |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
658 # if the directory could not be cleaned |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
659 # up due to missing globals |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
660 if "None" not in str(ex): |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
661 raise |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
662 print("ERROR: {!r} while cleaning up {!r}".format(ex, self,), |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
663 file=_sys.stderr) |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
664 return |
|
65618
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
665 self._closed = True |
|
66843
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
666 if _warn: |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
667 self._warn("Implicitly cleaning up {!r}".format(self), |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
668 ResourceWarning) |
|
65618
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
669 |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
670 def __exit__(self, exc, value, tb): |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
671 self.cleanup() |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
672 |
|
66843
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
673 def __del__(self): |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
674 # Issue a ResourceWarning if implicit cleanup needed |
|
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
675 self.cleanup(_warn=True) |
|
65618
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
676 |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
677 # XXX (ncoghlan): The following code attempts to make |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
678 # this class tolerant of the module nulling out process |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
679 # that happens during CPython interpreter shutdown |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
680 # Alas, it doesn't actually manage it. See issue #10188 |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
681 _listdir = staticmethod(_os.listdir) |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
682 _path_join = staticmethod(_os.path.join) |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
683 _isdir = staticmethod(_os.path.isdir) |
|
71608
5f7e71cfbcd6
Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents:
66843
diff
changeset
|
684 _islink = staticmethod(_os.path.islink) |
|
65618
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
685 _remove = staticmethod(_os.remove) |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
686 _rmdir = staticmethod(_os.rmdir) |
|
73169
e4d44c2e8e81
Simplify and remove few dependencies on 'errno', thanks to PEP 3151.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
71609
diff
changeset
|
687 _os_error = OSError |
|
66843
612dd5f2aab3
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents:
66833
diff
changeset
|
688 _warn = _warnings.warn |
|
65618
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
689 |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
690 def _rmtree(self, path): |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
691 # Essentially a stripped down version of shutil.rmtree. We can't |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
692 # use globals because they may be None'ed out at shutdown. |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
693 for name in self._listdir(path): |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
694 fullname = self._path_join(path, name) |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
695 try: |
|
71608
5f7e71cfbcd6
Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents:
66843
diff
changeset
|
696 isdir = self._isdir(fullname) and not self._islink(fullname) |
|
65618
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
697 except self._os_error: |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
698 isdir = False |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
699 if isdir: |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
700 self._rmtree(fullname) |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
701 else: |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
702 try: |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
703 self._remove(fullname) |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
704 except self._os_error: |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
705 pass |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
706 try: |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
707 self._rmdir(path) |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
708 except self._os_error: |
|
b172d7537b99
Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents:
57514
diff
changeset
|
709 pass |
