annotate Lib/tempfile.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 74fc476fe728
children b36cb4602e21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
22 "SpooledTemporaryFile",
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
23 "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
24 "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
25 "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
26 "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
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
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
30 # Imports.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
31
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 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
33 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
34 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
35
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
36 try:
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41234
diff changeset
37 from cStringIO import StringIO as _StringIO
46585
59db575ae374 A few small changes:
Skip Montanaro <skip@pobox.com>
parents: 45254
diff changeset
38 except ImportError:
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41234
diff changeset
39 from StringIO import StringIO as _StringIO
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
40
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
41 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
42 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
43 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
44 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
45 pass
a4abe17a5feb Windows fix: When PYTHONCASEOK is set, or for any other reason imports
Tim Peters <tim.peters@gmail.com>
parents: 28361
diff changeset
46 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
47 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
48 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
49 flags = _fcntl.fcntl(fd, _fcntl.F_GETFD, 0)
5d9f72d0843e Record that FCNTL.py has gone away; remove FCNTL hack in tempfile.py;
Tim Peters <tim.peters@gmail.com>
parents: 30890
diff changeset
50 except IOError:
5d9f72d0843e Record that FCNTL.py has gone away; remove FCNTL hack in tempfile.py;
Tim Peters <tim.peters@gmail.com>
parents: 30890
diff changeset
51 pass
30889
ceec69d240f0 fixed wrong error checking on fcntl call as per SF bug # 821896
Alex Martelli <aleaxit@gmail.com>
parents: 30681
diff changeset
52 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
53 # 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
54 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
55 _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
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
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
58 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
59 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
60 except ImportError:
95510f0a0624 Use the dummy_thread module in Queue.py and tempfile.py.
Guido van Rossum <guido@python.org>
parents: 26352
diff changeset
61 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
62 _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
63
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
64 _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
65 if hasattr(_os, 'O_NOINHERIT'):
4c746ba24de2 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24708
diff changeset
66 _text_openflags |= _os.O_NOINHERIT
4c746ba24de2 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24708
diff changeset
67 if hasattr(_os, 'O_NOFOLLOW'):
4c746ba24de2 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24708
diff changeset
68 _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
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 _bin_openflags = _text_openflags
24715
4c746ba24de2 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24708
diff changeset
71 if hasattr(_os, 'O_BINARY'):
4c746ba24de2 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24708
diff changeset
72 _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
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 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
75 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
76 else:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
77 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
78
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
79 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
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 # 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
82
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
83 _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
84
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
85 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
86 _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
87 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
88 _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
89 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
90 # Fallback. All we need is something that raises os.error if the
1997a14f6b08 mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents: 30889
diff changeset
91 # 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
92 def _stat(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
93 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
94 f = open(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 except IOError:
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 raise _os.error
1997a14f6b08 mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents: 30889
diff changeset
97 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
98
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 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
100 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
101 _stat(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
102 except _os.error:
1997a14f6b08 mktemp() shouldn't rely on os.path.exists(), which can return False if
Guido van Rossum <guido@python.org>
parents: 30889
diff changeset
103 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
104 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
105 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
106
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
107 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
108 """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
109 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
110 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
111 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
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 _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
114
26315
3fd8c2aff305 _RandomNameSequence(): style guide changes, small speedup, don't
Tim Peters <tim.peters@gmail.com>
parents: 26314
diff changeset
115 characters = ("abcdefghijklmnopqrstuvwxyz" +
3fd8c2aff305 _RandomNameSequence(): style guide changes, small speedup, don't
Tim Peters <tim.peters@gmail.com>
parents: 26314
diff changeset
116 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
41080
ff0c46fb195a Patch #1668482: don't use '-' in mkstemp
Collin Winter <collinw@gmail.com>
parents: 38849
diff changeset
117 "0123456789_")
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 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
120 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
121 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
122 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
123
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 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
125 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
126
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
127 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
128 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
129 c = self.characters
26315
3fd8c2aff305 _RandomNameSequence(): style guide changes, small speedup, don't
Tim Peters <tim.peters@gmail.com>
parents: 26314
diff changeset
130 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
131
26315
3fd8c2aff305 _RandomNameSequence(): style guide changes, small speedup, don't
Tim Peters <tim.peters@gmail.com>
parents: 26314
diff changeset
132 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
133 try:
26315
3fd8c2aff305 _RandomNameSequence(): style guide changes, small speedup, don't
Tim Peters <tim.peters@gmail.com>
parents: 26314
diff changeset
134 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
135 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
136 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
137
26315
3fd8c2aff305 _RandomNameSequence(): style guide changes, small speedup, don't
Tim Peters <tim.peters@gmail.com>
parents: 26314
diff changeset
138 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
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 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
141 """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
142 _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
143
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 = []
773
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
145
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
146 # 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
147 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
148 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
149 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
150
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
151 # Failing that, try OS-specific locations.
46942
5a7ccd65e59b Remove references to platform 'mac'
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 46585
diff changeset
152 if _os.name == 'riscos':
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 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
154 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
155 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
156 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
157 else:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
158 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
159
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
160 # 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
161 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
162 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
163 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
164 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
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 return dirlist
24715
4c746ba24de2 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24708
diff changeset
167
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
168 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
169 """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
170 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
171
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 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
173 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
174 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
175 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
176
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
177 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
178 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
179 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
180
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
181 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
182 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
183 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
184 # 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
185 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
186 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
187 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
188 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
189 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
190 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
191 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
192 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
193 _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
194 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
195 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
196 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
197 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
198 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
199 pass
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
200 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
201 ("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
202
24874
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
203 _name_sequence = None
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
204
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 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
206 """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
207
24874
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
208 global _name_sequence
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
209 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
210 _once_lock.acquire()
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
211 try:
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
212 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
213 _name_sequence = _RandomNameSequence()
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
214 finally:
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
215 _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
216 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
217
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
218
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
219 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
220 """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
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 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
223
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
224 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
225 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
226 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
227 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
228 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
229 _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
230 return (fd, _os.path.abspath(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
231 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
232 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
233 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
234 raise
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
235
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
236 raise IOError, (_errno.EEXIST, "No usable temporary file name found")
24715
4c746ba24de2 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24708
diff changeset
237
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
238
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
239 # 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
240
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
241 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
242 """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
243 return template
430
0f9ebc63dcb5 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
244
24874
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
245 tempdir = None
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
246
773
c721afab0efa Mostly rewritten to be more flexible and more portable
Guido van Rossum <guido@python.org>
parents: 586
diff changeset
247 def gettempdir():
46585
59db575ae374 A few small changes:
Skip Montanaro <skip@pobox.com>
parents: 45254
diff changeset
248 """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
249 global tempdir
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
250 if tempdir is None:
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
251 _once_lock.acquire()
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
252 try:
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
253 if tempdir is None:
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
254 tempdir = _get_default_tempdir()
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
255 finally:
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
256 _once_lock.release()
3647
16ca8f0f3f11 Be more careful about default temp dir
Guido van Rossum <guido@python.org>
parents: 773
diff changeset
257 return tempdir
586
dbf408918201 Use $TMPDIR if it is set in the environment.
Guido van Rossum <guido@python.org>
parents: 534
diff changeset
258
24874
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
259 def mkstemp(suffix="", prefix=template, dir=None, text=False):
46585
59db575ae374 A few small changes:
Skip Montanaro <skip@pobox.com>
parents: 45254
diff changeset
260 """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
261 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
262 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
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 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
265 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
266
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
267 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
268 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
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 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
271 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
272
24816
c45642e1dca9 tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents: 24813
diff changeset
273 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
274 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
275 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
276
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
277 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
278 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
279 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
280 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
281
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
282 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
283 """
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
284
24874
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
285 if dir is None:
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
286 dir = gettempdir()
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
287
24816
c45642e1dca9 tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents: 24813
diff changeset
288 if text:
c45642e1dca9 tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents: 24813
diff changeset
289 flags = _text_openflags
c45642e1dca9 tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents: 24813
diff changeset
290 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
291 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
292
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
293 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
294
430
0f9ebc63dcb5 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
295
24874
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
296 def mkdtemp(suffix="", prefix=template, dir=None):
46585
59db575ae374 A few small changes:
Skip Montanaro <skip@pobox.com>
parents: 45254
diff changeset
297 """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
298 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
299
24816
c45642e1dca9 tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents: 24813
diff changeset
300 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
301 not accepted.
5987
0a2c331200f5 Added TemporaryFile and wrapper class by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 5305
diff changeset
302
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
303 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
304 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
305
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
306 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
307 """
21474
a3b0673ba2ca TemporaryFileWrapper: cache the value of os.unlink for use by __del__,
Tim Peters <tim.peters@gmail.com>
parents: 20752
diff changeset
308
24874
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
309 if dir is None:
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
310 dir = gettempdir()
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
311
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
312 names = _get_candidate_names()
24715
4c746ba24de2 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24708
diff changeset
313
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
314 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
315 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
316 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
317 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
318 _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
319 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
320 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
321 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
322 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
323 raise
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
324
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
325 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
326
24874
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
327 def mktemp(suffix="", prefix=template, dir=None):
46585
59db575ae374 A few small changes:
Skip Montanaro <skip@pobox.com>
parents: 45254
diff changeset
328 """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
329 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
330
24816
c45642e1dca9 tempfile's mkstemp(): Changed last argument from
Tim Peters <tim.peters@gmail.com>
parents: 24813
diff changeset
331 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
332 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
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 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
335 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
336 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
337 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
338 """
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
339
26352
dc399e9cab89 Comment out the warnings about mktemp(). These are too annoying, and
Guido van Rossum <guido@python.org>
parents: 26315
diff changeset
340 ## 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
341 ## _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
342 ## 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
343
24874
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
344 if dir is None:
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
345 dir = gettempdir()
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
346
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
347 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
348 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
349 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
350 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
351 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
352 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
353
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
354 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
355
45254
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
356
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
357 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
358 """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
359
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
360 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
361 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
362 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
363 """
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
364
41161
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 41080
diff changeset
365 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
366 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
367 self.name = name
26314
8e6013bbe7a7 _TemporaryFileWrapper: changed self.close_called to a proper bool.
Tim Peters <tim.peters@gmail.com>
parents: 24874
diff changeset
368 self.close_called = False
41161
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 41080
diff changeset
369 self.delete = delete
5987
0a2c331200f5 Added TemporaryFile and wrapper class by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 5305
diff changeset
370
0a2c331200f5 Added TemporaryFile and wrapper class by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 5305
diff changeset
371 def __getattr__(self, name):
45254
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
372 # Attribute lookups are delegated to the underlying file
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
373 # and cached for non-numeric results
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
374 # (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
375 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
376 a = getattr(file, name)
45254
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
377 if not issubclass(type(a), type(0)):
10901
17ca2f64b6d6 In class TemporaryFileWrapper, don't cache attributes of tpye int --
Guido van Rossum <guido@python.org>
parents: 9447
diff changeset
378 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
379 return a
5987
0a2c331200f5 Added TemporaryFile and wrapper class by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 5305
diff changeset
380
45254
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
381 # The underlying __enter__ method returns the wrong object
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
382 # (self.file) so override it to return the wrapper
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
383 def __enter__(self):
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
384 self.file.__enter__()
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
385 return self
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
386
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
387 # 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
388 # 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
389 # 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
390 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
391 # 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
392 # 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
393 # 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
394 # 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
395 # __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
396 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
397
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
398 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
399 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
400 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
401 self.file.close()
41161
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 41080
diff changeset
402 if self.delete:
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 41080
diff changeset
403 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
404
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
405 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
406 self.close()
5987
0a2c331200f5 Added TemporaryFile and wrapper class by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 5305
diff changeset
407
45254
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
408 # Need to trap __exit__ as well to ensure the file gets
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
409 # deleted when used in a with statement
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
410 def __exit__(self, exc, value, tb):
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
411 result = self.file.__exit__(exc, value, tb)
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
412 self.close()
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
413 return result
54998
2faf2fa2a423 provide a dummy __exit__ on windows
Benjamin Peterson <benjamin@python.org>
parents: 46942
diff changeset
414 else:
2faf2fa2a423 provide a dummy __exit__ on windows
Benjamin Peterson <benjamin@python.org>
parents: 46942
diff changeset
415 def __exit__(self, exc, value, tb):
55029
74fc476fe728 proxy the __exit__ call
Benjamin Peterson <benjamin@python.org>
parents: 54998
diff changeset
416 self.file.__exit__(exc, value, tb)
45254
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
417
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
418
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
419 def NamedTemporaryFile(mode='w+b', bufsize=-1, suffix="",
41161
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 41080
diff changeset
420 prefix=template, 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
421 """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
422 Arguments:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
423 '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
424 '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
425 'bufsize' -- the buffer size argument to os.fdopen (default -1).
41161
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 41080
diff changeset
426 '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
427 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
428
34399
d7968621d28d SF bug #1099516: tempfile files not types.FileType
Raymond Hettinger <python@rcn.com>
parents: 32750
diff changeset
429 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
430 is accessible as file.name. The file will be automatically deleted
41161
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 41080
diff changeset
431 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
432 """
15827
81db92e80ea8 A variant of SF patch 103028 (Make tempfile.mktemp threadsafe).
Tim Peters <tim.peters@gmail.com>
parents: 14242
diff changeset
433
24874
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
434 if dir is None:
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
435 dir = gettempdir()
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
436
24800
931fb3700d0b NamedTemporaryFile(), TemporaryFile(): removed needless local vrbl 'bin'.
Tim Peters <tim.peters@gmail.com>
parents: 24799
diff changeset
437 if 'b' in mode:
931fb3700d0b NamedTemporaryFile(), TemporaryFile(): removed needless local vrbl 'bin'.
Tim Peters <tim.peters@gmail.com>
parents: 24799
diff changeset
438 flags = _bin_openflags
931fb3700d0b NamedTemporaryFile(), TemporaryFile(): removed needless local vrbl 'bin'.
Tim Peters <tim.peters@gmail.com>
parents: 24799
diff changeset
439 else:
931fb3700d0b NamedTemporaryFile(), TemporaryFile(): removed needless local vrbl 'bin'.
Tim Peters <tim.peters@gmail.com>
parents: 24799
diff changeset
440 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
441
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
442 # 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
443 # the file when it is closed. This is only supported by Windows.
41161
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 41080
diff changeset
444 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
445 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
446
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
447 (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
448 file = _os.fdopen(fd, mode, bufsize)
41161
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 41080
diff changeset
449 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
450
24813
7b7d8820ae38 Patch #595014: Cygwin tempfile patch
Jason Tishler <jason@tishler.net>
parents: 24802
diff changeset
451 if _os.name != 'posix' or _os.sys.platform == 'cygwin':
7b7d8820ae38 Patch #595014: Cygwin tempfile patch
Jason Tishler <jason@tishler.net>
parents: 24802
diff changeset
452 # 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
453 # 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
454 TemporaryFile = NamedTemporaryFile
15827
81db92e80ea8 A variant of SF patch 103028 (Make tempfile.mktemp threadsafe).
Tim Peters <tim.peters@gmail.com>
parents: 14242
diff changeset
455
81db92e80ea8 A variant of SF patch 103028 (Make tempfile.mktemp threadsafe).
Tim Peters <tim.peters@gmail.com>
parents: 14242
diff changeset
456 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
457 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
458 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
459 """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
460 Arguments:
38849
e76b6f3a4419 Param name is dir, not directory. Update docstring. Backport candidate
Neal Norwitz <nnorwitz@gmail.com>
parents: 38153
diff changeset
461 'prefix', 'suffix', 'dir' -- as for mkstemp.
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
462 '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
463 '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
464 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
465
34399
d7968621d28d SF bug #1099516: tempfile files not types.FileType
Raymond Hettinger <python@rcn.com>
parents: 32750
diff changeset
466 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
467 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
468 """
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
469
24874
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
470 if dir is None:
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
471 dir = gettempdir()
b517d9292ce7 Get rid of _once(); inlining it takes less code. :-)
Guido van Rossum <guido@python.org>
parents: 24816
diff changeset
472
24800
931fb3700d0b NamedTemporaryFile(), TemporaryFile(): removed needless local vrbl 'bin'.
Tim Peters <tim.peters@gmail.com>
parents: 24799
diff changeset
473 if 'b' in mode:
931fb3700d0b NamedTemporaryFile(), TemporaryFile(): removed needless local vrbl 'bin'.
Tim Peters <tim.peters@gmail.com>
parents: 24799
diff changeset
474 flags = _bin_openflags
931fb3700d0b NamedTemporaryFile(), TemporaryFile(): removed needless local vrbl 'bin'.
Tim Peters <tim.peters@gmail.com>
parents: 24799
diff changeset
475 else:
931fb3700d0b NamedTemporaryFile(), TemporaryFile(): removed needless local vrbl 'bin'.
Tim Peters <tim.peters@gmail.com>
parents: 24799
diff changeset
476 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
477
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
478 (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
479 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
480 _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
481 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
482 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
483 _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
484 raise
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
485
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
486 class SpooledTemporaryFile:
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
487 """Temporary file wrapper, specialized to switch from
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
488 StringIO to a real file when it exceeds a certain size or
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
489 when a fileno is needed.
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
490 """
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
491 _rolled = False
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
492
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
493 def __init__(self, max_size=0, mode='w+b', bufsize=-1,
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
494 suffix="", prefix=template, dir=None):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
495 self._file = _StringIO()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
496 self._max_size = max_size
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
497 self._rolled = False
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
498 self._TemporaryFileArgs = (mode, bufsize, suffix, prefix, dir)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
499
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
500 def _check(self, file):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
501 if self._rolled: return
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
502 max_size = self._max_size
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
503 if max_size and file.tell() > max_size:
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
504 self.rollover()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
505
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
506 def rollover(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
507 if self._rolled: return
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
508 file = self._file
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
509 newfile = self._file = TemporaryFile(*self._TemporaryFileArgs)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
510 del self._TemporaryFileArgs
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
511
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
512 newfile.write(file.getvalue())
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
513 newfile.seek(file.tell(), 0)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
514
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
515 self._rolled = True
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41234
diff changeset
516
45254
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
517 # The method caching trick from NamedTemporaryFile
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
518 # won't work here, because _file may change from a
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
519 # _StringIO instance to a real file. So we list
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
520 # all the methods directly.
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
521
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
522 # Context management protocol
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
523 def __enter__(self):
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
524 if self._file.closed:
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
525 raise ValueError("Cannot enter context with closed file")
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
526 return self
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
527
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
528 def __exit__(self, exc, value, tb):
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
529 self._file.close()
83821e0f395b Issue 2021: Allow NamedTemporaryFile and SpooledTemporaryFile to be used as context managers. (The NamedTemporaryFile fix should be considered for backporting to 2.5)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41532
diff changeset
530
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
531 # file protocol
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
532 def __iter__(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
533 return self._file.__iter__()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
534
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
535 def close(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
536 self._file.close()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
537
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
538 @property
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
539 def closed(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
540 return self._file.closed
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
541
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
542 @property
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
543 def encoding(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
544 return self._file.encoding
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
545
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
546 def fileno(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
547 self.rollover()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
548 return self._file.fileno()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
549
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
550 def flush(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
551 self._file.flush()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
552
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
553 def isatty(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
554 return self._file.isatty()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
555
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
556 @property
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
557 def mode(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
558 return self._file.mode
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
559
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
560 @property
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
561 def name(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
562 return self._file.name
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
563
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
564 @property
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
565 def newlines(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
566 return self._file.newlines
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
567
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
568 def next(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
569 return self._file.next
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
570
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
571 def read(self, *args):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
572 return self._file.read(*args)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
573
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
574 def readline(self, *args):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
575 return self._file.readline(*args)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
576
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
577 def readlines(self, *args):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
578 return self._file.readlines(*args)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
579
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
580 def seek(self, *args):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
581 self._file.seek(*args)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
582
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
583 @property
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
584 def softspace(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
585 return self._file.softspace
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
586
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
587 def tell(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
588 return self._file.tell()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
589
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
590 def truncate(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
591 self._file.truncate()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
592
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
593 def write(self, s):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
594 file = self._file
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
595 rv = file.write(s)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
596 self._check(file)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
597 return rv
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
598
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
599 def writelines(self, iterable):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
600 file = self._file
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
601 rv = file.writelines(iterable)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
602 self._check(file)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
603 return rv
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
604
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
605 def xreadlines(self, *args):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41161
diff changeset
606 return self._file.xreadlines(*args)