annotate Lib/test/test_tempfile.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents c96457df40e0
children 787b969d37f0
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: 20752
diff changeset
1 # tempfile.py unit tests.
20752
f2af42cf827a SF bug #476138: tempfile behavior across platforms
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2 import tempfile
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
3 import os
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
4 import sys
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
5 import re
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
6 import errno
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
7 import warnings
20752
f2af42cf827a SF bug #476138: tempfile behavior across platforms
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
8
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
9 import unittest
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
10 from test import test_support
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
11
25830
deabd053b6de Don't call warnings.resetwarnings(); that does bad things that cause
Fred Drake <fdrake@acm.org>
parents: 24960
diff changeset
12 warnings.filterwarnings("ignore",
deabd053b6de Don't call warnings.resetwarnings(); that does bad things that cause
Fred Drake <fdrake@acm.org>
parents: 24960
diff changeset
13 category=RuntimeWarning,
deabd053b6de Don't call warnings.resetwarnings(); that does bad things that cause
Fred Drake <fdrake@acm.org>
parents: 24960
diff changeset
14 message="mktemp", module=__name__)
deabd053b6de Don't call warnings.resetwarnings(); that does bad things that cause
Fred Drake <fdrake@acm.org>
parents: 24960
diff changeset
15
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
16 if hasattr(os, 'stat'):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
17 import stat
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
18 has_stat = 1
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
19 else:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
20 has_stat = 0
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
21
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
22 has_textmode = (tempfile._text_openflags != tempfile._bin_openflags)
24873
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
23 has_spawnl = hasattr(os, 'spawnl')
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
24
24871
258e0b1347c2 Drop the number of test files to 100 for all the tests
Neal Norwitz <nnorwitz@gmail.com>
parents: 24811
diff changeset
25 # TEST_FILES may need to be tweaked for systems depending on the maximum
258e0b1347c2 Drop the number of test files to 100 for all the tests
Neal Norwitz <nnorwitz@gmail.com>
parents: 24811
diff changeset
26 # number of files that can be opened at one time (see ulimit -n)
27161
b49a40db7b47 Added the Mac to platforms that don't have user/group/other modes.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26355
diff changeset
27 if sys.platform == 'mac':
b49a40db7b47 Added the Mac to platforms that don't have user/group/other modes.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26355
diff changeset
28 TEST_FILES = 32
39752
4c85ce70426e Patch #1540470, for OpenBSD 4.0. Backport candidate for 2.[34].
Neal Norwitz <nnorwitz@gmail.com>
parents: 38627
diff changeset
29 elif sys.platform in ('openbsd3', 'openbsd4'):
33494
d984cb7e2352 Patch #1026986: Add OpenBSD 3.5 and 3.6 to list of broken systems.
Martin v. Löwis <martin@v.loewis.de>
parents: 31846
diff changeset
30 TEST_FILES = 48
27161
b49a40db7b47 Added the Mac to platforms that don't have user/group/other modes.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26355
diff changeset
31 else:
b49a40db7b47 Added the Mac to platforms that don't have user/group/other modes.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26355
diff changeset
32 TEST_FILES = 100
24871
258e0b1347c2 Drop the number of test files to 100 for all the tests
Neal Norwitz <nnorwitz@gmail.com>
parents: 24811
diff changeset
33
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
34 # This is organized as one test for each chunk of code in tempfile.py,
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
35 # in order of their appearance in the file. Testing which requires
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
36 # threads is not done here.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
37
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
38 # Common functionality.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
39 class TC(unittest.TestCase):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
40
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
41 str_check = re.compile(r"[a-zA-Z0-9_-]{6}$")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
42
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
43 def failOnException(self, what, ei=None):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
44 if ei is None:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
45 ei = sys.exc_info()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
46 self.fail("%s raised %s: %s" % (what, ei[0], ei[1]))
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
47
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
48 def nameCheck(self, name, dir, pre, suf):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
49 (ndir, nbase) = os.path.split(name)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
50 npre = nbase[:len(pre)]
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
51 nsuf = nbase[len(nbase)-len(suf):]
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
52
30681
9691cc644160 Patch #810914: Return absolute path for mkstemp. Fixes #810408.
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
53 # check for equality of the absolute paths!
9691cc644160 Patch #810914: Return absolute path for mkstemp. Fixes #810408.
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
54 self.assertEqual(os.path.abspath(ndir), os.path.abspath(dir),
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
55 "file '%s' not in directory '%s'" % (name, dir))
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
56 self.assertEqual(npre, pre,
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
57 "file '%s' does not begin with '%s'" % (nbase, pre))
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
58 self.assertEqual(nsuf, suf,
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
59 "file '%s' does not end with '%s'" % (nbase, suf))
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
60
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
61 nbase = nbase[len(pre):len(nbase)-len(suf)]
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
62 self.assert_(self.str_check.match(nbase),
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
63 "random string '%s' does not match /^[a-zA-Z0-9_-]{6}$/"
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
64 % nbase)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
65
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
66 test_classes = []
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
67
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
68 class test_exports(TC):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
69 def test_exports(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
70 # There are no surprising symbols in the tempfile module
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
71 dict = tempfile.__dict__
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
72
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
73 expected = {
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
74 "NamedTemporaryFile" : 1,
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
75 "TemporaryFile" : 1,
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
76 "mkstemp" : 1,
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
77 "mkdtemp" : 1,
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
78 "mktemp" : 1,
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
79 "TMP_MAX" : 1,
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
80 "gettempprefix" : 1,
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
81 "gettempdir" : 1,
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
82 "tempdir" : 1,
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
83 "template" : 1,
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
84 "SpooledTemporaryFile" : 1
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
85 }
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
86
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
87 unexp = []
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
88 for key in dict:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
89 if key[0] != '_' and key not in expected:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
90 unexp.append(key)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
91 self.failUnless(len(unexp) == 0,
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
92 "unexpected keys: %s" % unexp)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
93
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
94 test_classes.append(test_exports)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
95
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
96
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
97 class test__RandomNameSequence(TC):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
98 """Test the internal iterator object _RandomNameSequence."""
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
99
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
100 def setUp(self):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
101 self.r = tempfile._RandomNameSequence()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
102
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
103 def test_get_six_char_str(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
104 # _RandomNameSequence returns a six-character string
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
105 s = self.r.next()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
106 self.nameCheck(s, '', '', '')
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
107
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
108 def test_many(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
109 # _RandomNameSequence returns no duplicate strings (stochastic)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
110
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
111 dict = {}
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
112 r = self.r
24871
258e0b1347c2 Drop the number of test files to 100 for all the tests
Neal Norwitz <nnorwitz@gmail.com>
parents: 24811
diff changeset
113 for i in xrange(TEST_FILES):
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
114 s = r.next()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
115 self.nameCheck(s, '', '', '')
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
116 self.failIf(s in dict)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
117 dict[s] = 1
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
118
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
119 def test_supports_iter(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
120 # _RandomNameSequence supports the iterator protocol
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
121
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
122 i = 0
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
123 r = self.r
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
124 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
125 for s in r:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
126 i += 1
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
127 if i == 20:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
128 break
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
129 except:
58791
c96457df40e0 Manual backport of r78046.
Georg Brandl <georg@python.org>
parents: 53485
diff changeset
130 self.failOnException("iteration")
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
131
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
132 test_classes.append(test__RandomNameSequence)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
133
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
134
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
135 class test__candidate_tempdir_list(TC):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
136 """Test the internal function _candidate_tempdir_list."""
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
137
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
138 def test_nonempty_list(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
139 # _candidate_tempdir_list returns a nonempty list of strings
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
140
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
141 cand = tempfile._candidate_tempdir_list()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
142
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
143 self.failIf(len(cand) == 0)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
144 for c in cand:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
145 self.assert_(isinstance(c, basestring),
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
146 "%s is not a string" % c)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
147
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
148 def test_wanted_dirs(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
149 # _candidate_tempdir_list contains the expected directories
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
150
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
151 # Make sure the interesting environment variables are all set.
53485
0688e8b24632 Merged revisions 71984 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 46613
diff changeset
152 with test_support.EnvironmentVarGuard() as env:
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
153 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: 20752
diff changeset
154 dirname = os.getenv(envname)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
155 if not dirname:
53485
0688e8b24632 Merged revisions 71984 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 46613
diff changeset
156 env.set(envname, os.path.abspath(envname))
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
157
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
158 cand = tempfile._candidate_tempdir_list()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
159
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
160 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: 20752
diff changeset
161 dirname = os.getenv(envname)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
162 if not dirname: raise ValueError
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
163 self.assert_(dirname in cand)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
164
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
165 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
166 dirname = os.getcwd()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
167 except (AttributeError, os.error):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
168 dirname = os.curdir
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
169
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
170 self.assert_(dirname in cand)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
171
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
172 # Not practical to try to verify the presence of OS-specific
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
173 # paths in this list.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
174
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
175 test_classes.append(test__candidate_tempdir_list)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
176
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
177
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
178 # We test _get_default_tempdir by testing gettempdir.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
179
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
180
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
181 class test__get_candidate_names(TC):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
182 """Test the internal function _get_candidate_names."""
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
183
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
184 def test_retval(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
185 # _get_candidate_names returns a _RandomNameSequence object
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
186 obj = tempfile._get_candidate_names()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
187 self.assert_(isinstance(obj, tempfile._RandomNameSequence))
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
188
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
189 def test_same_thing(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
190 # _get_candidate_names always returns the same object
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
191 a = tempfile._get_candidate_names()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
192 b = tempfile._get_candidate_names()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
193
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
194 self.assert_(a is b)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
195
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
196 test_classes.append(test__get_candidate_names)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
197
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
198
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
199 class test__mkstemp_inner(TC):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
200 """Test the internal function _mkstemp_inner."""
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
201
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
202 class mkstemped:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
203 _bflags = tempfile._bin_openflags
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
204 _tflags = tempfile._text_openflags
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
205 _close = os.close
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
206 _unlink = os.unlink
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
207
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
208 def __init__(self, dir, pre, suf, bin):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
209 if bin: flags = self._bflags
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
210 else: flags = self._tflags
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
211
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
212 (self.fd, self.name) = tempfile._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: 20752
diff changeset
213
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
214 def write(self, str):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
215 os.write(self.fd, str)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
216
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
217 def __del__(self):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
218 self._close(self.fd)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
219 self._unlink(self.name)
24715
4c746ba24de2 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24708
diff changeset
220
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
221 def do_create(self, dir=None, pre="", suf="", bin=1):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
222 if dir is None:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
223 dir = tempfile.gettempdir()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
224 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
225 file = self.mkstemped(dir, pre, suf, bin)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
226 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
227 self.failOnException("_mkstemp_inner")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
228
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
229 self.nameCheck(file.name, dir, pre, suf)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
230 return file
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
231
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
232 def test_basic(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
233 # _mkstemp_inner can create files
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
234 self.do_create().write("blat")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
235 self.do_create(pre="a").write("blat")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
236 self.do_create(suf="b").write("blat")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
237 self.do_create(pre="a", suf="b").write("blat")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
238 self.do_create(pre="aa", suf=".txt").write("blat")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
239
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
240 def test_basic_many(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
241 # _mkstemp_inner can create many files (stochastic)
24871
258e0b1347c2 Drop the number of test files to 100 for all the tests
Neal Norwitz <nnorwitz@gmail.com>
parents: 24811
diff changeset
242 extant = range(TEST_FILES)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
243 for i in extant:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
244 extant[i] = self.do_create(pre="aa")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
245
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
246 def test_choose_directory(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
247 # _mkstemp_inner can create files in a user-selected directory
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
248 dir = tempfile.mkdtemp()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
249 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
250 self.do_create(dir=dir).write("blat")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
251 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
252 os.rmdir(dir)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
253
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
254 def test_file_mode(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
255 # _mkstemp_inner creates files with the proper mode
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
256 if not has_stat:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
257 return # ugh, can't use TestSkipped.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
258
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
259 file = self.do_create()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
260 mode = stat.S_IMODE(os.stat(file.name).st_mode)
24716
343e64c92eb2 There's no distinction among 'user', 'group' and 'world' permissions
Tim Peters <tim.peters@gmail.com>
parents: 24715
diff changeset
261 expected = 0600
27161
b49a40db7b47 Added the Mac to platforms that don't have user/group/other modes.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26355
diff changeset
262 if sys.platform in ('win32', 'os2emx', 'mac'):
24716
343e64c92eb2 There's no distinction among 'user', 'group' and 'world' permissions
Tim Peters <tim.peters@gmail.com>
parents: 24715
diff changeset
263 # There's no distinction among 'user', 'group' and 'world';
343e64c92eb2 There's no distinction among 'user', 'group' and 'world' permissions
Tim Peters <tim.peters@gmail.com>
parents: 24715
diff changeset
264 # replicate the 'user' bits.
343e64c92eb2 There's no distinction among 'user', 'group' and 'world' permissions
Tim Peters <tim.peters@gmail.com>
parents: 24715
diff changeset
265 user = expected >> 6
343e64c92eb2 There's no distinction among 'user', 'group' and 'world' permissions
Tim Peters <tim.peters@gmail.com>
parents: 24715
diff changeset
266 expected = user * (1 + 8 + 64)
343e64c92eb2 There's no distinction among 'user', 'group' and 'world' permissions
Tim Peters <tim.peters@gmail.com>
parents: 24715
diff changeset
267 self.assertEqual(mode, expected)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
268
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
269 def test_noinherit(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
270 # _mkstemp_inner file handles are not inherited by child processes
24873
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
271 if not has_spawnl:
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
272 return # ugh, can't use TestSkipped.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
273
24873
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
274 if test_support.verbose:
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
275 v="v"
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
276 else:
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
277 v="q"
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
278
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
279 file = self.do_create()
24873
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
280 fd = "%d" % file.fd
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
281
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
282 try:
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
283 me = __file__
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
284 except NameError:
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
285 me = sys.argv[0]
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
286
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
287 # We have to exec something, so that FD_CLOEXEC will take
24873
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
288 # effect. The core of this test is therefore in
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
289 # tf_inherit_check.py, which see.
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
290 tester = os.path.join(os.path.dirname(os.path.abspath(me)),
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
291 "tf_inherit_check.py")
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
292
30761
0fe30710da80 Patch #813200: Quote executable path on Windows. Fixes #811082.
Martin v. Löwis <martin@v.loewis.de>
parents: 30681
diff changeset
293 # On Windows a spawn* /path/ with embedded spaces shouldn't be quoted,
0fe30710da80 Patch #813200: Quote executable path on Windows. Fixes #811082.
Martin v. Löwis <martin@v.loewis.de>
parents: 30681
diff changeset
294 # but an arg with embedded spaces should be decorated with double
0fe30710da80 Patch #813200: Quote executable path on Windows. Fixes #811082.
Martin v. Löwis <martin@v.loewis.de>
parents: 30681
diff changeset
295 # quotes on each end
44215
4de594740a88 This is probably what was meant here.
Armin Rigo <arigo@tunes.org>
parents: 41234
diff changeset
296 if sys.platform in ('win32',):
30761
0fe30710da80 Patch #813200: Quote executable path on Windows. Fixes #811082.
Martin v. Löwis <martin@v.loewis.de>
parents: 30681
diff changeset
297 decorated = '"%s"' % sys.executable
0fe30710da80 Patch #813200: Quote executable path on Windows. Fixes #811082.
Martin v. Löwis <martin@v.loewis.de>
parents: 30681
diff changeset
298 tester = '"%s"' % tester
0fe30710da80 Patch #813200: Quote executable path on Windows. Fixes #811082.
Martin v. Löwis <martin@v.loewis.de>
parents: 30681
diff changeset
299 else:
0fe30710da80 Patch #813200: Quote executable path on Windows. Fixes #811082.
Martin v. Löwis <martin@v.loewis.de>
parents: 30681
diff changeset
300 decorated = sys.executable
0fe30710da80 Patch #813200: Quote executable path on Windows. Fixes #811082.
Martin v. Löwis <martin@v.loewis.de>
parents: 30681
diff changeset
301
0fe30710da80 Patch #813200: Quote executable path on Windows. Fixes #811082.
Martin v. Löwis <martin@v.loewis.de>
parents: 30681
diff changeset
302 retval = os.spawnl(os.P_WAIT, sys.executable, decorated, tester, v, fd)
24873
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
303 self.failIf(retval < 0,
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
304 "child process caught fatal signal %d" % -retval)
34588
70cb0c3fe4a7 Exceedingly minor tweak.
Michael W. Hudson <mwh@python.net>
parents: 33494
diff changeset
305 self.failIf(retval > 0, "child process reports failure %d"%retval)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
306
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
307 def test_textmode(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
308 # _mkstemp_inner can create files in text mode
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
309 if not has_textmode:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
310 return # ugh, can't use TestSkipped.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
311
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
312 self.do_create(bin=0).write("blat\n")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
313 # XXX should test that the file really is a text file
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
314
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
315 test_classes.append(test__mkstemp_inner)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
316
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
317
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
318 class test_gettempprefix(TC):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
319 """Test gettempprefix()."""
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
320
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
321 def test_sane_template(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
322 # gettempprefix returns a nonempty prefix string
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
323 p = tempfile.gettempprefix()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
324
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
325 self.assert_(isinstance(p, basestring))
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
326 self.assert_(len(p) > 0)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
327
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
328 def test_usable_template(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
329 # gettempprefix returns a usable prefix string
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
330
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
331 # Create a temp directory, avoiding use of the prefix.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
332 # Then attempt to create a file whose name is
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
333 # prefix + 'xxxxxx.xxx' in that directory.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
334 p = tempfile.gettempprefix() + "xxxxxx.xxx"
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
335 d = tempfile.mkdtemp(prefix="")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
336 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
337 p = os.path.join(d, p)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
338 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
339 fd = os.open(p, os.O_RDWR | os.O_CREAT)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
340 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
341 self.failOnException("os.open")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
342 os.close(fd)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
343 os.unlink(p)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
344 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
345 os.rmdir(d)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
346
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
347 test_classes.append(test_gettempprefix)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
348
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
349
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
350 class test_gettempdir(TC):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
351 """Test gettempdir()."""
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
352
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
353 def test_directory_exists(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
354 # gettempdir returns a directory which exists
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
355
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
356 dir = tempfile.gettempdir()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
357 self.assert_(os.path.isabs(dir) or dir == os.curdir,
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
358 "%s is not an absolute path" % dir)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
359 self.assert_(os.path.isdir(dir),
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
360 "%s is not a directory" % dir)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
361
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
362 def test_directory_writable(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
363 # gettempdir returns a directory writable by the user
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
364
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
365 # sneaky: just instantiate a NamedTemporaryFile, which
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
366 # defaults to writing into the directory returned by
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
367 # gettempdir.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
368 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
369 file = tempfile.NamedTemporaryFile()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
370 file.write("blat")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
371 file.close()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
372 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
373 self.failOnException("create file in %s" % tempfile.gettempdir())
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
374
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
375 def test_same_thing(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
376 # gettempdir always returns the same object
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
377 a = tempfile.gettempdir()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
378 b = tempfile.gettempdir()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
379
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
380 self.assert_(a is b)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
381
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
382 test_classes.append(test_gettempdir)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
383
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
384
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
385 class test_mkstemp(TC):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
386 """Test mkstemp()."""
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
387
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 34588
diff changeset
388 def do_create(self, dir=None, pre="", suf=""):
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
389 if dir is None:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
390 dir = tempfile.gettempdir()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
391 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
392 (fd, name) = tempfile.mkstemp(dir=dir, prefix=pre, suffix=suf)
30681
9691cc644160 Patch #810914: Return absolute path for mkstemp. Fixes #810408.
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
393 (ndir, nbase) = os.path.split(name)
9691cc644160 Patch #810914: Return absolute path for mkstemp. Fixes #810408.
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
394 adir = os.path.abspath(dir)
9691cc644160 Patch #810914: Return absolute path for mkstemp. Fixes #810408.
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
395 self.assertEqual(adir, ndir,
9691cc644160 Patch #810914: Return absolute path for mkstemp. Fixes #810408.
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
396 "Directory '%s' incorrectly returned as '%s'" % (adir, ndir))
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
397 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
398 self.failOnException("mkstemp")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
399
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
400 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
401 self.nameCheck(name, dir, pre, suf)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
402 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
403 os.close(fd)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
404 os.unlink(name)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
405
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
406 def test_basic(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
407 # mkstemp can create files
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
408 self.do_create()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
409 self.do_create(pre="a")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
410 self.do_create(suf="b")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
411 self.do_create(pre="a", suf="b")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
412 self.do_create(pre="aa", suf=".txt")
30681
9691cc644160 Patch #810914: Return absolute path for mkstemp. Fixes #810408.
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
413 self.do_create(dir=".")
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
414
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
415 def test_choose_directory(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
416 # mkstemp can create directories in a user-selected directory
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
417 dir = tempfile.mkdtemp()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
418 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
419 self.do_create(dir=dir)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
420 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
421 os.rmdir(dir)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
422
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
423 test_classes.append(test_mkstemp)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
424
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
425
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
426 class test_mkdtemp(TC):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
427 """Test mkdtemp()."""
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
428
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
429 def do_create(self, dir=None, pre="", suf=""):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
430 if dir is None:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
431 dir = tempfile.gettempdir()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
432 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
433 name = tempfile.mkdtemp(dir=dir, prefix=pre, suffix=suf)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
434 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
435 self.failOnException("mkdtemp")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
436
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
437 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
438 self.nameCheck(name, dir, pre, suf)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
439 return name
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
440 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
441 os.rmdir(name)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
442 raise
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
443
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
444 def test_basic(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
445 # mkdtemp can create directories
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
446 os.rmdir(self.do_create())
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
447 os.rmdir(self.do_create(pre="a"))
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
448 os.rmdir(self.do_create(suf="b"))
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
449 os.rmdir(self.do_create(pre="a", suf="b"))
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
450 os.rmdir(self.do_create(pre="aa", suf=".txt"))
24715
4c746ba24de2 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24708
diff changeset
451
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
452 def test_basic_many(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
453 # mkdtemp can create many directories (stochastic)
24871
258e0b1347c2 Drop the number of test files to 100 for all the tests
Neal Norwitz <nnorwitz@gmail.com>
parents: 24811
diff changeset
454 extant = range(TEST_FILES)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
455 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
456 for i in extant:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
457 extant[i] = self.do_create(pre="aa")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
458 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
459 for i in extant:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
460 if(isinstance(i, basestring)):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
461 os.rmdir(i)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
462
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
463 def test_choose_directory(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
464 # mkdtemp can create directories in a user-selected directory
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
465 dir = tempfile.mkdtemp()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
466 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
467 os.rmdir(self.do_create(dir=dir))
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
468 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
469 os.rmdir(dir)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
470
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
471 def test_mode(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
472 # mkdtemp creates directories with the proper mode
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
473 if not has_stat:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
474 return # ugh, can't use TestSkipped.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
475
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
476 dir = self.do_create()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
477 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
478 mode = stat.S_IMODE(os.stat(dir).st_mode)
31846
92552467145e When /tmp has certain sticky bits set, newly created subdirectories
Guido van Rossum <guido@python.org>
parents: 30761
diff changeset
479 mode &= 0777 # Mask off sticky bits inherited from /tmp
24716
343e64c92eb2 There's no distinction among 'user', 'group' and 'world' permissions
Tim Peters <tim.peters@gmail.com>
parents: 24715
diff changeset
480 expected = 0700
27161
b49a40db7b47 Added the Mac to platforms that don't have user/group/other modes.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26355
diff changeset
481 if sys.platform in ('win32', 'os2emx', 'mac'):
24716
343e64c92eb2 There's no distinction among 'user', 'group' and 'world' permissions
Tim Peters <tim.peters@gmail.com>
parents: 24715
diff changeset
482 # There's no distinction among 'user', 'group' and 'world';
343e64c92eb2 There's no distinction among 'user', 'group' and 'world' permissions
Tim Peters <tim.peters@gmail.com>
parents: 24715
diff changeset
483 # replicate the 'user' bits.
343e64c92eb2 There's no distinction among 'user', 'group' and 'world' permissions
Tim Peters <tim.peters@gmail.com>
parents: 24715
diff changeset
484 user = expected >> 6
343e64c92eb2 There's no distinction among 'user', 'group' and 'world' permissions
Tim Peters <tim.peters@gmail.com>
parents: 24715
diff changeset
485 expected = user * (1 + 8 + 64)
343e64c92eb2 There's no distinction among 'user', 'group' and 'world' permissions
Tim Peters <tim.peters@gmail.com>
parents: 24715
diff changeset
486 self.assertEqual(mode, expected)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
487 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
488 os.rmdir(dir)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
489
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
490 test_classes.append(test_mkdtemp)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
491
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
492
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
493 class test_mktemp(TC):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
494 """Test mktemp()."""
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
495
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
496 # For safety, all use of mktemp must occur in a private directory.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
497 # We must also suppress the RuntimeWarning it generates.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
498 def setUp(self):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
499 self.dir = tempfile.mkdtemp()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
500
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
501 def tearDown(self):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
502 if self.dir:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
503 os.rmdir(self.dir)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
504 self.dir = None
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
505
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
506 class mktemped:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
507 _unlink = os.unlink
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
508 _bflags = tempfile._bin_openflags
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
509
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
510 def __init__(self, dir, pre, suf):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
511 self.name = tempfile.mktemp(dir=dir, prefix=pre, suffix=suf)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
512 # Create the file. This will raise an exception if it's
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
513 # mysteriously appeared in the meanwhile.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
514 os.close(os.open(self.name, self._bflags, 0600))
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
515
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
516 def __del__(self):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
517 self._unlink(self.name)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
518
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
519 def do_create(self, pre="", suf=""):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
520 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
521 file = self.mktemped(self.dir, pre, suf)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
522 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
523 self.failOnException("mktemp")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
524
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
525 self.nameCheck(file.name, self.dir, pre, suf)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
526 return file
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
527
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
528 def test_basic(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
529 # mktemp can choose usable file names
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
530 self.do_create()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
531 self.do_create(pre="a")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
532 self.do_create(suf="b")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
533 self.do_create(pre="a", suf="b")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
534 self.do_create(pre="aa", suf=".txt")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
535
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
536 def test_many(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
537 # mktemp can choose many usable file names (stochastic)
24871
258e0b1347c2 Drop the number of test files to 100 for all the tests
Neal Norwitz <nnorwitz@gmail.com>
parents: 24811
diff changeset
538 extant = range(TEST_FILES)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
539 for i in extant:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
540 extant[i] = self.do_create(pre="aa")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
541
26355
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
542 ## def test_warning(self):
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
543 ## # mktemp issues a warning when used
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
544 ## warnings.filterwarnings("error",
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
545 ## category=RuntimeWarning,
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
546 ## message="mktemp")
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
547 ## self.assertRaises(RuntimeWarning,
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
548 ## tempfile.mktemp, dir=self.dir)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
549
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
550 test_classes.append(test_mktemp)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
551
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
552
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
553 # We test _TemporaryFileWrapper by testing NamedTemporaryFile.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
554
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
555
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
556 class test_NamedTemporaryFile(TC):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
557 """Test NamedTemporaryFile()."""
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
558
41161
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
559 def do_create(self, dir=None, pre="", suf="", delete=True):
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
560 if dir is None:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
561 dir = tempfile.gettempdir()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
562 try:
41161
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
563 file = tempfile.NamedTemporaryFile(dir=dir, prefix=pre, suffix=suf,
41164
93e67d362a81 Fix a tab.
Georg Brandl <georg@python.org>
parents: 41161
diff changeset
564 delete=delete)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
565 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
566 self.failOnException("NamedTemporaryFile")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
567
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
568 self.nameCheck(file.name, dir, pre, suf)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
569 return file
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
570
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
571
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
572 def test_basic(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
573 # NamedTemporaryFile can create files
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
574 self.do_create()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
575 self.do_create(pre="a")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
576 self.do_create(suf="b")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
577 self.do_create(pre="a", suf="b")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
578 self.do_create(pre="aa", suf=".txt")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
579
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
580 def test_creates_named(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
581 # NamedTemporaryFile creates files with names
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
582 f = tempfile.NamedTemporaryFile()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
583 self.failUnless(os.path.exists(f.name),
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
584 "NamedTemporaryFile %s does not exist" % f.name)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
585
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
586 def test_del_on_close(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
587 # A NamedTemporaryFile is deleted when closed
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
588 dir = tempfile.mkdtemp()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
589 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
590 f = tempfile.NamedTemporaryFile(dir=dir)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
591 f.write('blat')
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
592 f.close()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
593 self.failIf(os.path.exists(f.name),
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
594 "NamedTemporaryFile %s exists after close" % f.name)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
595 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
596 os.rmdir(dir)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
597
41161
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
598 def test_dis_del_on_close(self):
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
599 # Tests that delete-on-close can be disabled
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
600 dir = tempfile.mkdtemp()
41164
93e67d362a81 Fix a tab.
Georg Brandl <georg@python.org>
parents: 41161
diff changeset
601 tmp = None
41161
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
602 try:
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
603 f = tempfile.NamedTemporaryFile(dir=dir, delete=False)
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
604 tmp = f.name
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
605 f.write('blat')
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
606 f.close()
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
607 self.failUnless(os.path.exists(f.name),
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
608 "NamedTemporaryFile %s missing after close" % f.name)
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
609 finally:
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
610 if tmp is not None:
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
611 os.unlink(tmp)
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
612 os.rmdir(dir)
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
613
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
614 def test_multiple_close(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
615 # A NamedTemporaryFile can be closed many times without error
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
616 f = tempfile.NamedTemporaryFile()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
617 f.write('abc\n')
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
618 f.close()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
619 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
620 f.close()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
621 f.close()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
622 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
623 self.failOnException("close")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
624
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: 44215
diff changeset
625 def test_context_manager(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: 44215
diff changeset
626 # A NamedTemporaryFile can be used as a context manager
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: 44215
diff changeset
627 with tempfile.NamedTemporaryFile() as f:
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: 44215
diff changeset
628 self.failUnless(os.path.exists(f.name))
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: 44215
diff changeset
629 self.failIf(os.path.exists(f.name))
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: 44215
diff changeset
630 def use_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: 44215
diff changeset
631 with f:
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: 44215
diff changeset
632 pass
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: 44215
diff changeset
633 self.failUnlessRaises(ValueError, use_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: 44215
diff changeset
634
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
635 # How to test the mode and bufsize parameters?
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
636
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
637 test_classes.append(test_NamedTemporaryFile)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
638
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
639 class test_SpooledTemporaryFile(TC):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
640 """Test SpooledTemporaryFile()."""
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
641
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
642 def do_create(self, max_size=0, dir=None, pre="", suf=""):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
643 if dir is None:
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
644 dir = tempfile.gettempdir()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
645 try:
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
646 file = tempfile.SpooledTemporaryFile(max_size=max_size, dir=dir, prefix=pre, suffix=suf)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
647 except:
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
648 self.failOnException("SpooledTemporaryFile")
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
649
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
650 return file
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
651
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
652
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
653 def test_basic(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
654 # SpooledTemporaryFile can create files
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
655 f = self.do_create()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
656 self.failIf(f._rolled)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
657 f = self.do_create(max_size=100, pre="a", suf=".txt")
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
658 self.failIf(f._rolled)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
659
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
660 def test_del_on_close(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
661 # A SpooledTemporaryFile is deleted when closed
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
662 dir = tempfile.mkdtemp()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
663 try:
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
664 f = tempfile.SpooledTemporaryFile(max_size=10, dir=dir)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
665 self.failIf(f._rolled)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
666 f.write('blat ' * 5)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
667 self.failUnless(f._rolled)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
668 filename = f.name
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
669 f.close()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
670 self.failIf(os.path.exists(filename),
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
671 "SpooledTemporaryFile %s exists after close" % filename)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
672 finally:
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
673 os.rmdir(dir)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
674
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
675 def test_rewrite_small(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
676 # A SpooledTemporaryFile can be written to multiple within the max_size
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
677 f = self.do_create(max_size=30)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
678 self.failIf(f._rolled)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
679 for i in range(5):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
680 f.seek(0, 0)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
681 f.write('x' * 20)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
682 self.failIf(f._rolled)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
683
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
684 def test_write_sequential(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
685 # A SpooledTemporaryFile should hold exactly max_size bytes, and roll
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
686 # over afterward
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
687 f = self.do_create(max_size=30)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
688 self.failIf(f._rolled)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
689 f.write('x' * 20)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
690 self.failIf(f._rolled)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
691 f.write('x' * 10)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
692 self.failIf(f._rolled)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
693 f.write('x')
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
694 self.failUnless(f._rolled)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
695
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
696 def test_sparse(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
697 # A SpooledTemporaryFile that is written late in the file will extend
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
698 # when that occurs
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
699 f = self.do_create(max_size=30)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
700 self.failIf(f._rolled)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
701 f.seek(100, 0)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
702 self.failIf(f._rolled)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
703 f.write('x')
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
704 self.failUnless(f._rolled)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
705
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
706 def test_fileno(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
707 # A SpooledTemporaryFile should roll over to a real file on fileno()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
708 f = self.do_create(max_size=30)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
709 self.failIf(f._rolled)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
710 self.failUnless(f.fileno() > 0)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
711 self.failUnless(f._rolled)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
712
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: 44215
diff changeset
713 def test_multiple_close_before_rollover(self):
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
714 # A SpooledTemporaryFile can be closed many times without error
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
715 f = tempfile.SpooledTemporaryFile()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
716 f.write('abc\n')
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: 44215
diff changeset
717 self.failIf(f._rolled)
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: 44215
diff changeset
718 f.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: 44215
diff changeset
719 try:
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: 44215
diff changeset
720 f.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: 44215
diff changeset
721 f.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: 44215
diff changeset
722 except:
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: 44215
diff changeset
723 self.failOnException("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: 44215
diff changeset
724
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: 44215
diff changeset
725 def test_multiple_close_after_rollover(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: 44215
diff changeset
726 # A SpooledTemporaryFile can be closed many times without error
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: 44215
diff changeset
727 f = tempfile.SpooledTemporaryFile(max_size=1)
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: 44215
diff changeset
728 f.write('abc\n')
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: 44215
diff changeset
729 self.failUnless(f._rolled)
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
730 f.close()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
731 try:
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
732 f.close()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
733 f.close()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
734 except:
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
735 self.failOnException("close")
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
736
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
737 def test_bound_methods(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
738 # It should be OK to steal a bound method from a SpooledTemporaryFile
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
739 # and use it independently; when the file rolls over, those bound
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
740 # methods should continue to function
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
741 f = self.do_create(max_size=30)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
742 read = f.read
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
743 write = f.write
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
744 seek = f.seek
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
745
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
746 write("a" * 35)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
747 write("b" * 35)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
748 seek(0, 0)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
749 self.failUnless(read(70) == 'a'*35 + 'b'*35)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
750
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: 44215
diff changeset
751 def test_context_manager_before_rollover(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: 44215
diff changeset
752 # A SpooledTemporaryFile can be used as a context manager
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: 44215
diff changeset
753 with tempfile.SpooledTemporaryFile(max_size=1) as f:
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: 44215
diff changeset
754 self.failIf(f._rolled)
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: 44215
diff changeset
755 self.failIf(f.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: 44215
diff changeset
756 self.failUnless(f.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: 44215
diff changeset
757 def use_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: 44215
diff changeset
758 with f:
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: 44215
diff changeset
759 pass
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: 44215
diff changeset
760 self.failUnlessRaises(ValueError, use_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: 44215
diff changeset
761
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: 44215
diff changeset
762 def test_context_manager_during_rollover(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: 44215
diff changeset
763 # A SpooledTemporaryFile can be used as a context manager
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: 44215
diff changeset
764 with tempfile.SpooledTemporaryFile(max_size=1) as f:
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: 44215
diff changeset
765 self.failIf(f._rolled)
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: 44215
diff changeset
766 f.write('abc\n')
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: 44215
diff changeset
767 f.flush()
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: 44215
diff changeset
768 self.failUnless(f._rolled)
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: 44215
diff changeset
769 self.failIf(f.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: 44215
diff changeset
770 self.failUnless(f.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: 44215
diff changeset
771 def use_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: 44215
diff changeset
772 with f:
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: 44215
diff changeset
773 pass
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: 44215
diff changeset
774 self.failUnlessRaises(ValueError, use_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: 44215
diff changeset
775
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: 44215
diff changeset
776 def test_context_manager_after_rollover(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: 44215
diff changeset
777 # A SpooledTemporaryFile can be used as a context manager
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: 44215
diff changeset
778 f = tempfile.SpooledTemporaryFile(max_size=1)
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: 44215
diff changeset
779 f.write('abc\n')
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: 44215
diff changeset
780 f.flush()
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: 44215
diff changeset
781 self.failUnless(f._rolled)
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: 44215
diff changeset
782 with f:
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: 44215
diff changeset
783 self.failIf(f.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: 44215
diff changeset
784 self.failUnless(f.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: 44215
diff changeset
785 def use_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: 44215
diff changeset
786 with f:
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: 44215
diff changeset
787 pass
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: 44215
diff changeset
788 self.failUnlessRaises(ValueError, use_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: 44215
diff changeset
789
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: 44215
diff changeset
790
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
791 test_classes.append(test_SpooledTemporaryFile)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
792
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
793
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
794 class test_TemporaryFile(TC):
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
795 """Test TemporaryFile()."""
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
796
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
797 def test_basic(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
798 # TemporaryFile can create files
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
799 # No point in testing the name params - the file has no name.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
800 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
801 tempfile.TemporaryFile()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
802 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
803 self.failOnException("TemporaryFile")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
804
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
805 def test_has_no_name(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
806 # TemporaryFile creates files with no names (on this system)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
807 dir = tempfile.mkdtemp()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
808 f = tempfile.TemporaryFile(dir=dir)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
809 f.write('blat')
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
810
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
811 # Sneaky: because this file has no name, it should not prevent
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
812 # us from removing the directory it was created in.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
813 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
814 os.rmdir(dir)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
815 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
816 ei = sys.exc_info()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
817 # cleanup
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
818 f.close()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
819 os.rmdir(dir)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
820 self.failOnException("rmdir", ei)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
821
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
822 def test_multiple_close(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
823 # A TemporaryFile can be closed many times without error
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
824 f = tempfile.TemporaryFile()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
825 f.write('abc\n')
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
826 f.close()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
827 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
828 f.close()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
829 f.close()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
830 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
831 self.failOnException("close")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
832
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
833 # How to test the mode and bufsize parameters?
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
834
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
835
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
836 if tempfile.NamedTemporaryFile is not tempfile.TemporaryFile:
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
837 test_classes.append(test_TemporaryFile)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
838
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
839 def test_main():
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 27161
diff changeset
840 test_support.run_unittest(*test_classes)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
841
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
842 if __name__ == "__main__":
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
843 test_main()