Mercurial > cpython-lru_cache-weakref
annotate Lib/tempfile.py @ 27998:e27f58a50737 legacy-trunk
SF bug #690377: Dud footnote reference in distutils docs
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Fri, 21 Feb 2003 02:47:09 +0000 |
| parents | 95510f0a0624 |
| children | 6fddcdd11f49 |
| 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 template - the default prefix for all temporary names. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
14 You may change this to control the default prefix. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
15 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
|
16 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
|
17 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
|
18 """ |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
19 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
20 __all__ = [ |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
21 "NamedTemporaryFile", "TemporaryFile", # high 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 "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
|
23 "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
|
24 "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
|
25 "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
|
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 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
29 # Imports. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
30 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
31 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
|
32 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
|
33 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
|
34 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
35 if _os.name == 'mac': |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
36 import macfs as _macfs |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
37 import MACFS as _MACFS |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
38 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
39 try: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
40 import fcntl as _fcntl |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
41 def _set_cloexec(fd): |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
42 flags = _fcntl.fcntl(fd, _fcntl.F_GETFD, 0) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
43 if flags >= 0: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
44 # 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
|
45 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
|
46 _fcntl.fcntl(fd, _fcntl.F_SETFD, flags) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
47 except (ImportError, AttributeError): |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
48 def _set_cloexec(fd): |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
49 pass |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
50 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
51 try: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
52 import 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
|
53 except ImportError: |
|
95510f0a0624
Use the dummy_thread module in Queue.py and tempfile.py.
Guido van Rossum <guido@python.org>
parents:
26352
diff
changeset
|
54 import dummy_thread as _thread |
|
95510f0a0624
Use the dummy_thread module in Queue.py and tempfile.py.
Guido van Rossum <guido@python.org>
parents:
26352
diff
changeset
|
55 _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
|
56 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
57 _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
|
58 if hasattr(_os, 'O_NOINHERIT'): |
|
4c746ba24de2
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24708
diff
changeset
|
59 _text_openflags |= _os.O_NOINHERIT |
|
4c746ba24de2
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24708
diff
changeset
|
60 if hasattr(_os, 'O_NOFOLLOW'): |
|
4c746ba24de2
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24708
diff
changeset
|
61 _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
|
62 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
63 _bin_openflags = _text_openflags |
|
24715
4c746ba24de2
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24708
diff
changeset
|
64 if hasattr(_os, 'O_BINARY'): |
|
4c746ba24de2
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24708
diff
changeset
|
65 _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
|
66 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
67 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
|
68 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
|
69 else: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
70 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
|
71 |
|
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
|
72 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
|
73 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
74 tempdir = None |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
75 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
76 # 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
|
77 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
78 _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
|
79 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
80 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
|
81 """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
|
82 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
|
83 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
|
84 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
|
85 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
86 _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
|
87 |
|
26315
3fd8c2aff305
_RandomNameSequence(): style guide changes, small speedup, don't
Tim Peters <tim.peters@gmail.com>
parents:
26314
diff
changeset
|
88 characters = ("abcdefghijklmnopqrstuvwxyz" + |
|
3fd8c2aff305
_RandomNameSequence(): style guide changes, small speedup, don't
Tim Peters <tim.peters@gmail.com>
parents:
26314
diff
changeset
|
89 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + |
|
3fd8c2aff305
_RandomNameSequence(): style guide changes, small speedup, don't
Tim Peters <tim.peters@gmail.com>
parents:
26314
diff
changeset
|
90 "0123456789-_") |
|
773
c721afab0efa
Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents:
586
diff
changeset
|
91 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
92 def __init__(self): |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
93 self.mutex = _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
|
94 self.rng = _Random() |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
95 self.normcase = _os.path.normcase |
|
26315
3fd8c2aff305
_RandomNameSequence(): style guide changes, small speedup, don't
Tim Peters <tim.peters@gmail.com>
parents:
26314
diff
changeset
|
96 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
97 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
|
98 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
|
99 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
100 def next(self): |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
101 m = self.mutex |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
102 c = self.characters |
|
26315
3fd8c2aff305
_RandomNameSequence(): style guide changes, small speedup, don't
Tim Peters <tim.peters@gmail.com>
parents:
26314
diff
changeset
|
103 choose = self.rng.choice |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
104 |
|
26315
3fd8c2aff305
_RandomNameSequence(): style guide changes, small speedup, don't
Tim Peters <tim.peters@gmail.com>
parents:
26314
diff
changeset
|
105 m.acquire() |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
106 try: |
|
26315
3fd8c2aff305
_RandomNameSequence(): style guide changes, small speedup, don't
Tim Peters <tim.peters@gmail.com>
parents:
26314
diff
changeset
|
107 letters = [choose(c) for dummy in "123456"] |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
108 finally: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
109 m.release() |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
110 |
|
26315
3fd8c2aff305
_RandomNameSequence(): style guide changes, small speedup, don't
Tim Peters <tim.peters@gmail.com>
parents:
26314
diff
changeset
|
111 return self.normcase(''.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
|
112 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
113 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
|
114 """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
|
115 _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
|
116 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
117 dirlist = [] |
|
773
c721afab0efa
Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents:
586
diff
changeset
|
118 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
119 # 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
|
120 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
|
121 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
|
122 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
|
123 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
124 # Failing that, try OS-specific locations. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
125 if _os.name == 'mac': |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
126 try: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
127 refnum, dirid = _macfs.FindFolder(_MACFS.kOnSystemDisk, |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
128 _MACFS.kTemporaryFolderType, 1) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
129 dirname = _macfs.FSSpec((refnum, dirid, '')).as_pathname() |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
130 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
|
131 except _macfs.error: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
132 pass |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
133 elif _os.name == 'riscos': |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
134 dirname = _os.getenv('Wimp$ScrapDir') |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
135 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
|
136 elif _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
|
137 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
|
138 else: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
139 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
|
140 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
141 # 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
|
142 try: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
143 dirlist.append(_os.getcwd()) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
144 except (AttributeError, _os.error): |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
145 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
|
146 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
147 return dirlist |
|
24715
4c746ba24de2
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24708
diff
changeset
|
148 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
149 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
|
150 """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
|
151 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
|
152 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
153 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
|
154 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
|
155 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
|
156 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
|
157 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
158 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
|
159 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
|
160 flags = _text_openflags |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
161 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
162 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
|
163 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
|
164 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
|
165 # Try only a few names per directory. |
|
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 seq in xrange(100): |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
167 name = namer.next() |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
168 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
|
169 try: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
170 fd = _os.open(filename, flags, 0600) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
171 fp = _os.fdopen(fd, 'w') |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
172 fp.write('blat') |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
173 fp.close() |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
174 _os.unlink(filename) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
175 del fp, fd |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
176 return dir |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
177 except (OSError, IOError), e: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
178 if e[0] != _errno.EEXIST: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
179 break # no point trying more names in this directory |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
180 pass |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
181 raise IOError, (_errno.ENOENT, |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
182 ("No usable temporary directory found in %s" % dirlist)) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
183 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
184 _name_sequence = None |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
185 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
186 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
|
187 """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
|
188 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
189 global _name_sequence |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
190 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
|
191 _once_lock.acquire() |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
192 try: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
193 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
|
194 _name_sequence = _RandomNameSequence() |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
195 finally: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
196 _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
|
197 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
|
198 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
199 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
200 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
|
201 """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
|
202 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
203 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
|
204 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
205 for seq in xrange(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
|
206 name = names.next() |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
207 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
|
208 try: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
209 fd = _os.open(file, flags, 0600) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
210 _set_cloexec(fd) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
211 return (fd, file) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
212 except OSError, e: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
213 if e.errno == _errno.EEXIST: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
214 continue # try again |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
215 raise |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
216 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
217 raise IOError, (_errno.EEXIST, "No usable temporary file name found") |
|
24715
4c746ba24de2
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24708
diff
changeset
|
218 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
219 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
220 # 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
|
221 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
222 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
|
223 """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
|
224 return template |
| 430 | 225 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
226 tempdir = None |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
227 |
|
773
c721afab0efa
Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents:
586
diff
changeset
|
228 def gettempdir(): |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
229 """Accessor for tempdir.tempdir.""" |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
230 global tempdir |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
231 if tempdir is None: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
232 _once_lock.acquire() |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
233 try: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
234 if tempdir is None: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
235 tempdir = _get_default_tempdir() |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
236 finally: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
237 _once_lock.release() |
|
3647
16ca8f0f3f11
Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents:
773
diff
changeset
|
238 return tempdir |
|
586
dbf408918201
Use $TMPDIR if it is set in the environment.
Guido van Rossum <guido@python.org>
parents:
534
diff
changeset
|
239 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
240 def mkstemp(suffix="", prefix=template, dir=None, text=False): |
|
24816
c45642e1dca9
tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents:
24813
diff
changeset
|
241 """mkstemp([suffix, [prefix, [dir, [text]]]]) |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
242 User-callable function to create and return a unique temporary |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
243 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
|
244 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
|
245 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
246 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
|
247 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
|
248 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
249 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
|
250 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
|
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 '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
|
253 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
|
254 |
|
24816
c45642e1dca9
tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents:
24813
diff
changeset
|
255 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
|
256 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
|
257 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
|
258 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
259 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
|
260 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
|
261 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
|
262 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
|
263 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
264 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
|
265 """ |
|
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
|
266 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
267 if dir is None: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
268 dir = gettempdir() |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
269 |
|
24816
c45642e1dca9
tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents:
24813
diff
changeset
|
270 if text: |
|
c45642e1dca9
tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents:
24813
diff
changeset
|
271 flags = _text_openflags |
|
c45642e1dca9
tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents:
24813
diff
changeset
|
272 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
|
273 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
|
274 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
275 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
|
276 |
| 430 | 277 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
278 def mkdtemp(suffix="", prefix=template, 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
|
279 """mkdtemp([suffix, [prefix, [dir]]]) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
280 User-callable function to create and return a unique temporary |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
281 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
|
282 |
|
24816
c45642e1dca9
tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents:
24813
diff
changeset
|
283 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
|
284 not accepted. |
|
5987
0a2c331200f5
Added TemporaryFile and wrapper class by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
5305
diff
changeset
|
285 |
|
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 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
|
287 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
|
288 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
289 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
|
290 """ |
|
21474
a3b0673ba2ca
TemporaryFileWrapper: cache the value of os.unlink for use by __del__,
Tim Peters <tim.peters@gmail.com>
parents:
20752
diff
changeset
|
291 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
292 if dir is None: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
293 dir = gettempdir() |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
294 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
295 names = _get_candidate_names() |
|
24715
4c746ba24de2
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24708
diff
changeset
|
296 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
297 for seq in xrange(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
|
298 name = names.next() |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
299 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
|
300 try: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
301 _os.mkdir(file, 0700) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
302 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
|
303 except OSError, e: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
304 if e.errno == _errno.EEXIST: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
305 continue # try again |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
306 raise |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
307 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
308 raise IOError, (_errno.EEXIST, "No usable temporary directory name found") |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
309 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
310 def mktemp(suffix="", prefix=template, 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
|
311 """mktemp([suffix, [prefix, [dir]]]) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
312 User-callable function to return a unique temporary file name. The |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
313 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
|
314 |
|
24816
c45642e1dca9
tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents:
24813
diff
changeset
|
315 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
|
316 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
|
317 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
318 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
|
319 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
|
320 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
|
321 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
|
322 """ |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
323 |
|
26352
dc399e9cab89
Comment out the warnings about mktemp(). These are too annoying, and
Guido van Rossum <guido@python.org>
parents:
26315
diff
changeset
|
324 ## 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
|
325 ## _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
|
326 ## 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
|
327 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
328 if dir is None: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
329 dir = gettempdir() |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
330 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
331 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
|
332 for seq in xrange(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
|
333 name = names.next() |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
334 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
|
335 if not _os.path.exists(file): |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
336 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
|
337 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
338 raise IOError, (_errno.EEXIST, "No usable temporary filename found") |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
339 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
340 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
|
341 """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
|
342 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
343 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
|
344 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
|
345 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
|
346 """ |
|
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 def __init__(self, file, name): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6924
diff
changeset
|
349 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
|
350 self.name = name |
|
26314
8e6013bbe7a7
_TemporaryFileWrapper: changed self.close_called to a proper bool.
Tim Peters <tim.peters@gmail.com>
parents:
24874
diff
changeset
|
351 self.close_called = False |
|
5987
0a2c331200f5
Added TemporaryFile and wrapper class by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
5305
diff
changeset
|
352 |
|
0a2c331200f5
Added TemporaryFile and wrapper class by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
5305
diff
changeset
|
353 def __getattr__(self, name): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6924
diff
changeset
|
354 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
|
355 a = getattr(file, name) |
|
10901
17ca2f64b6d6
In class TemporaryFileWrapper, don't cache attributes of tpye int --
Guido van Rossum <guido@python.org>
parents:
9447
diff
changeset
|
356 if type(a) != type(0): |
|
17ca2f64b6d6
In class TemporaryFileWrapper, don't cache attributes of tpye int --
Guido van Rossum <guido@python.org>
parents:
9447
diff
changeset
|
357 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
|
358 return a |
|
5987
0a2c331200f5
Added TemporaryFile and wrapper class by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
5305
diff
changeset
|
359 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
360 # 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
|
361 # 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
|
362 # 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
|
363 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
|
364 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
365 # 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
|
366 # 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
|
367 # 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
|
368 # 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
|
369 # __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
|
370 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
|
371 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
372 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
|
373 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
|
374 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
|
375 self.file.close() |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
376 self.unlink(self.name) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
377 |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
378 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
|
379 self.close() |
|
5987
0a2c331200f5
Added TemporaryFile and wrapper class by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
5305
diff
changeset
|
380 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
381 def NamedTemporaryFile(mode='w+b', bufsize=-1, suffix="", |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
382 prefix=template, 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
|
383 """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
|
384 Arguments: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
385 'prefix', 'suffix', 'dir' -- as for mkstemp. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
386 'mode' -- the mode argument to os.fdopen (default "w+b"). |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
387 'bufsize' -- the buffer size argument to os.fdopen (default -1). |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
388 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
|
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 Returns a file object; the name of the file is accessible as |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
391 file.name. The file will be automatically deleted when it is |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
392 closed. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
393 """ |
|
15827
81db92e80ea8
A variant of SF patch 103028 (Make tempfile.mktemp threadsafe).
Tim Peters <tim.peters@gmail.com>
parents:
14242
diff
changeset
|
394 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
395 if dir is None: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
396 dir = gettempdir() |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
397 |
|
24800
931fb3700d0b
NamedTemporaryFile(), TemporaryFile(): removed needless local vrbl 'bin'.
Tim Peters <tim.peters@gmail.com>
parents:
24799
diff
changeset
|
398 if 'b' in mode: |
|
931fb3700d0b
NamedTemporaryFile(), TemporaryFile(): removed needless local vrbl 'bin'.
Tim Peters <tim.peters@gmail.com>
parents:
24799
diff
changeset
|
399 flags = _bin_openflags |
|
931fb3700d0b
NamedTemporaryFile(), TemporaryFile(): removed needless local vrbl 'bin'.
Tim Peters <tim.peters@gmail.com>
parents:
24799
diff
changeset
|
400 else: |
|
931fb3700d0b
NamedTemporaryFile(), TemporaryFile(): removed needless local vrbl 'bin'.
Tim Peters <tim.peters@gmail.com>
parents:
24799
diff
changeset
|
401 flags = _text_openflags |
|
15827
81db92e80ea8
A variant of SF patch 103028 (Make tempfile.mktemp threadsafe).
Tim Peters <tim.peters@gmail.com>
parents:
14242
diff
changeset
|
402 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
403 # 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
|
404 # the file when it is closed. This is only supported by Windows. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
405 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
|
406 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
|
407 |
|
24708
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
408 (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
|
409 file = _os.fdopen(fd, mode, bufsize) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
410 return _TemporaryFileWrapper(file, name) |
|
15827
81db92e80ea8
A variant of SF patch 103028 (Make tempfile.mktemp threadsafe).
Tim Peters <tim.peters@gmail.com>
parents:
14242
diff
changeset
|
411 |
|
24813
7b7d8820ae38
Patch #595014: Cygwin tempfile patch
Jason Tishler <jason@tishler.net>
parents:
24802
diff
changeset
|
412 if _os.name != 'posix' or _os.sys.platform == 'cygwin': |
|
7b7d8820ae38
Patch #595014: Cygwin tempfile patch
Jason Tishler <jason@tishler.net>
parents:
24802
diff
changeset
|
413 # 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
|
414 # 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
|
415 TemporaryFile = NamedTemporaryFile |
|
15827
81db92e80ea8
A variant of SF patch 103028 (Make tempfile.mktemp threadsafe).
Tim Peters <tim.peters@gmail.com>
parents:
14242
diff
changeset
|
416 |
|
81db92e80ea8
A variant of SF patch 103028 (Make tempfile.mktemp threadsafe).
Tim Peters <tim.peters@gmail.com>
parents:
14242
diff
changeset
|
417 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
|
418 def TemporaryFile(mode='w+b', bufsize=-1, suffix="", |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
419 prefix=template, 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
|
420 """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
|
421 Arguments: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
422 'prefix', 'suffix', 'directory' -- as for mkstemp. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
423 'mode' -- the mode argument to os.fdopen (default "w+b"). |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
424 'bufsize' -- the buffer size argument to os.fdopen (default -1). |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
425 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
|
426 |
|
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 Returns a file object. The file has no name, and will cease to |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
428 exist when it is closed. |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
429 """ |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
430 |
|
24874
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
431 if dir is None: |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
432 dir = gettempdir() |
|
b517d9292ce7
Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents:
24816
diff
changeset
|
433 |
|
24800
931fb3700d0b
NamedTemporaryFile(), TemporaryFile(): removed needless local vrbl 'bin'.
Tim Peters <tim.peters@gmail.com>
parents:
24799
diff
changeset
|
434 if 'b' in mode: |
|
931fb3700d0b
NamedTemporaryFile(), TemporaryFile(): removed needless local vrbl 'bin'.
Tim Peters <tim.peters@gmail.com>
parents:
24799
diff
changeset
|
435 flags = _bin_openflags |
|
931fb3700d0b
NamedTemporaryFile(), TemporaryFile(): removed needless local vrbl 'bin'.
Tim Peters <tim.peters@gmail.com>
parents:
24799
diff
changeset
|
436 else: |
|
931fb3700d0b
NamedTemporaryFile(), TemporaryFile(): removed needless local vrbl 'bin'.
Tim Peters <tim.peters@gmail.com>
parents:
24799
diff
changeset
|
437 flags = _text_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
|
438 |
|
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) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
440 try: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
441 _os.unlink(name) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
442 return _os.fdopen(fd, mode, bufsize) |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
443 except: |
|
aa8bd0dc4f8a
Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
444 _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
|
445 raise |
