annotate Lib/test/test_tempfile.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents e42be90eb9c5
children 5c2ff6e64c47
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
73745
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
4 import signal
24708
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 sys
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 re
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)
61258
f6785bce54b5 In a number of places code still revers
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58817
diff changeset
27 if 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
28 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
29 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
30 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
31
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
32 # 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
33 # 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
34 # 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
35
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
36 # 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
37 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
38
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
39 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
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 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
42 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
43 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
44 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
45
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
46 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
47 (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
48 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
49 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
50
30681
9691cc644160 Patch #810914: Return absolute path for mkstemp. Fixes #810408.
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
51 # 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
52 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
53 "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
54 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
55 "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
56 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
57 "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
58
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
59 nbase = nbase[len(pre):len(nbase)-len(suf)]
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
60 self.assertTrue(self.str_check.match(nbase),
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
61 "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
62 % 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
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
64 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
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 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
67 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
68 # 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
69 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
70
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
71 expected = {
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
72 "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
73 "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
74 "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
75 "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
76 "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
77 "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
78 "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
79 "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
80 "tempdir" : 1,
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
81 "template" : 1,
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
82 "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
83 }
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
84
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
85 unexp = []
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
86 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
87 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
88 unexp.append(key)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
89 self.assertTrue(len(unexp) == 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
90 "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
91
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
92 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
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
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
95 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
96 """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
97
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
98 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
99 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
100
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
101 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
102 # _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
103 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
104 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
105
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
106 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
107 # _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
108
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
109 dict = {}
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
110 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
111 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
112 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
113 self.nameCheck(s, '', '', '')
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
114 self.assertNotIn(s, dict)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
115 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
116
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
117 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
118 # _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
119
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
120 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
121 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
122 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
123 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
124 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
125 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
126 break
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
127 except:
58791
c96457df40e0 Manual backport of r78046.
Georg Brandl <georg@python.org>
parents: 53485
diff changeset
128 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
129
73745
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
130 @unittest.skipUnless(hasattr(os, 'fork'),
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
131 "os.fork is required for this test")
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
132 def test_process_awareness(self):
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
133 # ensure that the random source differs between
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
134 # child and parent.
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
135 read_fd, write_fd = os.pipe()
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
136 pid = None
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
137 try:
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
138 pid = os.fork()
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
139 if not pid:
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
140 os.close(read_fd)
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
141 os.write(write_fd, next(self.r).encode("ascii"))
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
142 os.close(write_fd)
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
143 # bypass the normal exit handlers- leave those to
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
144 # the parent.
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
145 os._exit(0)
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
146 parent_value = next(self.r)
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
147 child_value = os.read(read_fd, len(parent_value)).decode("ascii")
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
148 finally:
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
149 if pid:
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
150 # best effort to ensure the process can't bleed out
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
151 # via any bugs above
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
152 try:
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
153 os.kill(pid, signal.SIGKILL)
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
154 except EnvironmentError:
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
155 pass
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
156 os.close(read_fd)
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
157 os.close(write_fd)
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
158 self.assertNotEqual(child_value, parent_value)
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
159
e42be90eb9c5 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68446
diff changeset
160
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
161 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
162
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
163
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
164 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
165 """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
166
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
167 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
168 # _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
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 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
171
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
172 self.assertFalse(len(cand) == 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
173 for c in cand:
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
174 self.assertIsInstance(c, basestring)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
175
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
176 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
177 # _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
178
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
179 # 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
180 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
181 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
182 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
183 if not dirname:
53644
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53484
diff changeset
184 env[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
185
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
186 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
187
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
188 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
189 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
190 if not dirname: raise ValueError
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
191 self.assertIn(dirname, cand)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
192
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
193 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
194 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
195 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
196 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
197
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
198 self.assertIn(dirname, cand)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
199
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
200 # 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
201 # 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
202
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
203 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
204
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
205
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
206 # 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
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
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
209 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
210 """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
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 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
213 # _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
214 obj = tempfile._get_candidate_names()
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
215 self.assertIsInstance(obj, tempfile._RandomNameSequence)
24708
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 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
218 # _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
219 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
220 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
221
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
222 self.assertTrue(a is b)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
223
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
224 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
225
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
226
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
227 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
228 """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
229
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
230 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
231 _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
232 _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
233 _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
234 _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
235
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
236 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
237 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
238 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
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 (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
241
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
242 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
243 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
244
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
245 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
246 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
247 self._unlink(self.name)
24715
4c746ba24de2 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24708
diff changeset
248
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
249 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
250 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
251 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
252 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
253 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
254 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
255 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
256
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
257 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
258 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
259
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
260 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
261 # _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
262 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
263 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
264 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
265 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
266 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
267
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
268 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
269 # _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
270 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
271 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
272 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
273
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
274 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
275 # _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
276 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
277 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
278 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
279 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
280 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
281
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
282 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
283 # _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
284 if not has_stat:
52353
e6009b9cd908 remove test_support.TestSkipped and just use unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 46613
diff changeset
285 return # ugh, can't use SkipTest.
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 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
288 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
289 expected = 0600
61258
f6785bce54b5 In a number of places code still revers
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58817
diff changeset
290 if sys.platform in ('win32', 'os2emx'):
24716
343e64c92eb2 There's no distinction among 'user', 'group' and 'world' permissions
Tim Peters <tim.peters@gmail.com>
parents: 24715
diff changeset
291 # 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
292 # 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
293 user = expected >> 6
343e64c92eb2 There's no distinction among 'user', 'group' and 'world' permissions
Tim Peters <tim.peters@gmail.com>
parents: 24715
diff changeset
294 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
295 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
296
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
297 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
298 # _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
299 if not has_spawnl:
52353
e6009b9cd908 remove test_support.TestSkipped and just use unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 46613
diff changeset
300 return # ugh, can't use SkipTest.
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
301
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
302 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
303 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
304 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
305 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
306
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
307 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
308 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
309
99f5d18be588 Patch by Zack W to make test_noinherit() more robust: spawn a Python
Guido van Rossum <guido@python.org>
parents: 24871
diff changeset
310 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
311 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
312 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
313 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
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 # 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
316 # 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
317 # 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
318 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
319 "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
320
30761
0fe30710da80 Patch #813200: Quote executable path on Windows. Fixes #811082.
Martin v. Löwis <martin@v.loewis.de>
parents: 30681
diff changeset
321 # 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
322 # 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
323 # quotes on each end
45280
4afbcde343ed Backport relevant part of issue 2021 fix (r60695): Support with statement properly in tempfile.NamedTemporaryFile
Nick Coghlan <ncoghlan@gmail.com>
parents: 39752
diff changeset
324 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
325 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
326 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
327 else:
0fe30710da80 Patch #813200: Quote executable path on Windows. Fixes #811082.
Martin v. Löwis <martin@v.loewis.de>
parents: 30681
diff changeset
328 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
329
0fe30710da80 Patch #813200: Quote executable path on Windows. Fixes #811082.
Martin v. Löwis <martin@v.loewis.de>
parents: 30681
diff changeset
330 retval = os.spawnl(os.P_WAIT, sys.executable, decorated, tester, v, fd)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
331 self.assertFalse(retval < 0,
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
332 "child process caught fatal signal %d" % -retval)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
333 self.assertFalse(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
334
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
335 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
336 # _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
337 if not has_textmode:
52353
e6009b9cd908 remove test_support.TestSkipped and just use unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 46613
diff changeset
338 return # ugh, can't use SkipTest.
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
339
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
340 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
341 # 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
342
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
343 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
344
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
345
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
346 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
347 """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 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
350 # 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
351 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
352
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
353 self.assertIsInstance(p, basestring)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
354 self.assertTrue(len(p) > 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
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 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
357 # 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
358
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
359 # 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
360 # 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
361 # 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
362 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
363 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
364 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
365 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
366 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
367 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
368 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
369 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
370 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
371 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
372 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
373 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
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 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
376
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
377
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
378 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
379 """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
380
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
381 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
382 # 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
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 dir = tempfile.gettempdir()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
385 self.assertTrue(os.path.isabs(dir) or dir == os.curdir,
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
386 "%s is not an absolute path" % dir)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
387 self.assertTrue(os.path.isdir(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
388 "%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
389
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
390 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
391 # 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
392
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
393 # 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
394 # 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
395 # gettempdir.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
396 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
397 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
398 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
399 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
400 except:
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.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
402
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
403 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
404 # 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
405 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
406 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
407
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
408 self.assertTrue(a is b)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
409
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
410 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
411
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
412
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
413 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
414 """Test mkstemp()."""
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
415
38627
98e81af40007 Normalized a few cases of whitespace in function declarations.
Martin Blais <blais@furius.ca>
parents: 34588
diff changeset
416 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
417 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
418 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
419 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
420 (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
421 (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
422 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
423 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
424 "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
425 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
426 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
427
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
428 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
429 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
430 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
431 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
432 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
433
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
434 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
435 # 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
436 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
437 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
438 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
439 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
440 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
441 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
442
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
443 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
444 # 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
445 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
446 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
447 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
448 finally:
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(dir)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
450
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
451 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
452
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
453
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
454 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
455 """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
456
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
457 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
458 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
459 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
460 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
461 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
462 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
463 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
464
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
465 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
466 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
467 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
468 except:
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(name)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
470 raise
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
471
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
472 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
473 # 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
474 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
475 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
476 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
477 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
478 os.rmdir(self.do_create(pre="aa", suf=".txt"))
24715
4c746ba24de2 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24708
diff changeset
479
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
480 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
481 # 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
482 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
483 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
484 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
485 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
486 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
487 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
488 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
489 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
490
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
491 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
492 # 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
493 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
494 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
495 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
496 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
497 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
498
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
499 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
500 # 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
501 if not has_stat:
52353
e6009b9cd908 remove test_support.TestSkipped and just use unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 46613
diff changeset
502 return # ugh, can't use SkipTest.
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
503
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
504 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
505 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
506 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
507 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
508 expected = 0700
61258
f6785bce54b5 In a number of places code still revers
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58817
diff changeset
509 if sys.platform in ('win32', 'os2emx'):
24716
343e64c92eb2 There's no distinction among 'user', 'group' and 'world' permissions
Tim Peters <tim.peters@gmail.com>
parents: 24715
diff changeset
510 # 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
511 # 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
512 user = expected >> 6
343e64c92eb2 There's no distinction among 'user', 'group' and 'world' permissions
Tim Peters <tim.peters@gmail.com>
parents: 24715
diff changeset
513 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
514 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
515 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
516 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
517
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
518 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
519
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
520
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
521 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
522 """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
523
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
524 # 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
525 # 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
526 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
527 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
528
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
529 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
530 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
531 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
532 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
533
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
534 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
535 _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
536 _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
537
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
538 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
539 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
540 # 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
541 # 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
542 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
543
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
544 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
545 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
546
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
547 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
548 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
549 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
550 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
551 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
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 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
554 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
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 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
557 # 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
558 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
559 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
560 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
561 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
562 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
563
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
564 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
565 # 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
566 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
567 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
568 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
569
26355
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
570 ## def test_warning(self):
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
571 ## # mktemp issues a warning when used
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
572 ## warnings.filterwarnings("error",
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
573 ## category=RuntimeWarning,
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
574 ## message="mktemp")
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
575 ## self.assertRaises(RuntimeWarning,
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
576 ## 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
577
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
578 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
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
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
581 # 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
582
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
583
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
584 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
585 """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
586
41161
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
587 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
588 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
589 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
590 try:
41161
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
591 file = tempfile.NamedTemporaryFile(dir=dir, prefix=pre, suffix=suf,
41164
93e67d362a81 Fix a tab.
Georg Brandl <georg@python.org>
parents: 41161
diff changeset
592 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
593 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
594 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
595
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
596 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
597 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
598
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
599
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
600 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
601 # 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
602 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
603 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
604 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
605 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
606 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
607
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
608 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
609 # 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
610 f = tempfile.NamedTemporaryFile()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
611 self.assertTrue(os.path.exists(f.name),
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
612 "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
613
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_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
615 # 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
616 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
617 try:
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 = 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
619 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
620 f.close()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
621 self.assertFalse(os.path.exists(f.name),
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
622 "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
623 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
624 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
625
41161
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
626 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
627 # 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
628 dir = tempfile.mkdtemp()
41164
93e67d362a81 Fix a tab.
Georg Brandl <georg@python.org>
parents: 41161
diff changeset
629 tmp = None
41161
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
630 try:
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
631 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
632 tmp = f.name
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
633 f.write('blat')
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
634 f.close()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
635 self.assertTrue(os.path.exists(f.name),
41161
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
636 "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
637 finally:
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
638 if tmp is not None:
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
639 os.unlink(tmp)
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
640 os.rmdir(dir)
7b36a64dcb08 Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter
Georg Brandl <georg@python.org>
parents: 39752
diff changeset
641
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
642 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
643 # 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
644 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
645 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
646 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
647 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
648 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
649 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
650 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
651 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
652
45280
4afbcde343ed Backport relevant part of issue 2021 fix (r60695): Support with statement properly in tempfile.NamedTemporaryFile
Nick Coghlan <ncoghlan@gmail.com>
parents: 39752
diff changeset
653 def test_context_manager(self):
4afbcde343ed Backport relevant part of issue 2021 fix (r60695): Support with statement properly in tempfile.NamedTemporaryFile
Nick Coghlan <ncoghlan@gmail.com>
parents: 39752
diff changeset
654 # A NamedTemporaryFile can be used as a context manager
4afbcde343ed Backport relevant part of issue 2021 fix (r60695): Support with statement properly in tempfile.NamedTemporaryFile
Nick Coghlan <ncoghlan@gmail.com>
parents: 39752
diff changeset
655 with tempfile.NamedTemporaryFile() as f:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
656 self.assertTrue(os.path.exists(f.name))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
657 self.assertFalse(os.path.exists(f.name))
45280
4afbcde343ed Backport relevant part of issue 2021 fix (r60695): Support with statement properly in tempfile.NamedTemporaryFile
Nick Coghlan <ncoghlan@gmail.com>
parents: 39752
diff changeset
658 def use_closed():
4afbcde343ed Backport relevant part of issue 2021 fix (r60695): Support with statement properly in tempfile.NamedTemporaryFile
Nick Coghlan <ncoghlan@gmail.com>
parents: 39752
diff changeset
659 with f:
4afbcde343ed Backport relevant part of issue 2021 fix (r60695): Support with statement properly in tempfile.NamedTemporaryFile
Nick Coghlan <ncoghlan@gmail.com>
parents: 39752
diff changeset
660 pass
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
661 self.assertRaises(ValueError, use_closed)
45280
4afbcde343ed Backport relevant part of issue 2021 fix (r60695): Support with statement properly in tempfile.NamedTemporaryFile
Nick Coghlan <ncoghlan@gmail.com>
parents: 39752
diff changeset
662
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
663 # 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
664
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
665 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
666
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
667 class test_SpooledTemporaryFile(TC):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
668 """Test SpooledTemporaryFile()."""
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
669
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
670 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
671 if dir is None:
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
672 dir = tempfile.gettempdir()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
673 try:
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
674 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
675 except:
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
676 self.failOnException("SpooledTemporaryFile")
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
677
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
678 return file
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
679
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
680
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
681 def test_basic(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
682 # SpooledTemporaryFile can create files
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
683 f = self.do_create()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
684 self.assertFalse(f._rolled)
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
685 f = self.do_create(max_size=100, pre="a", suf=".txt")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
686 self.assertFalse(f._rolled)
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
687
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
688 def test_del_on_close(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
689 # A SpooledTemporaryFile is deleted when closed
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
690 dir = tempfile.mkdtemp()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
691 try:
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
692 f = tempfile.SpooledTemporaryFile(max_size=10, dir=dir)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
693 self.assertFalse(f._rolled)
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
694 f.write('blat ' * 5)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
695 self.assertTrue(f._rolled)
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
696 filename = f.name
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
697 f.close()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
698 self.assertFalse(os.path.exists(filename),
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
699 "SpooledTemporaryFile %s exists after close" % filename)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
700 finally:
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
701 os.rmdir(dir)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
702
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
703 def test_rewrite_small(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
704 # 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
705 f = self.do_create(max_size=30)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
706 self.assertFalse(f._rolled)
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
707 for i in range(5):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
708 f.seek(0, 0)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
709 f.write('x' * 20)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
710 self.assertFalse(f._rolled)
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
711
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
712 def test_write_sequential(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
713 # 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
714 # over afterward
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
715 f = self.do_create(max_size=30)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
716 self.assertFalse(f._rolled)
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
717 f.write('x' * 20)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
718 self.assertFalse(f._rolled)
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
719 f.write('x' * 10)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
720 self.assertFalse(f._rolled)
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
721 f.write('x')
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
722 self.assertTrue(f._rolled)
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
723
68446
7aee0fe6ae5d Port #11488 patch from 3.1 (changeset f816841bab03)
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
724 def test_writelines(self):
7aee0fe6ae5d Port #11488 patch from 3.1 (changeset f816841bab03)
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
725 # Verify writelines with a SpooledTemporaryFile
7aee0fe6ae5d Port #11488 patch from 3.1 (changeset f816841bab03)
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
726 f = self.do_create()
7aee0fe6ae5d Port #11488 patch from 3.1 (changeset f816841bab03)
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
727 f.writelines((b'x', b'y', b'z'))
7aee0fe6ae5d Port #11488 patch from 3.1 (changeset f816841bab03)
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
728 f.seek(0)
7aee0fe6ae5d Port #11488 patch from 3.1 (changeset f816841bab03)
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
729 buf = f.read()
7aee0fe6ae5d Port #11488 patch from 3.1 (changeset f816841bab03)
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
730 self.assertEqual(buf, b'xyz')
7aee0fe6ae5d Port #11488 patch from 3.1 (changeset f816841bab03)
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
731
7aee0fe6ae5d Port #11488 patch from 3.1 (changeset f816841bab03)
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
732 def test_writelines_sequential(self):
7aee0fe6ae5d Port #11488 patch from 3.1 (changeset f816841bab03)
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
733 # A SpooledTemporaryFile should hold exactly max_size bytes, and roll
7aee0fe6ae5d Port #11488 patch from 3.1 (changeset f816841bab03)
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
734 # over afterward
7aee0fe6ae5d Port #11488 patch from 3.1 (changeset f816841bab03)
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
735 f = self.do_create(max_size=35)
7aee0fe6ae5d Port #11488 patch from 3.1 (changeset f816841bab03)
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
736 f.writelines((b'x' * 20, b'x' * 10, b'x' * 5))
7aee0fe6ae5d Port #11488 patch from 3.1 (changeset f816841bab03)
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
737 self.assertFalse(f._rolled)
7aee0fe6ae5d Port #11488 patch from 3.1 (changeset f816841bab03)
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
738 f.write(b'x')
7aee0fe6ae5d Port #11488 patch from 3.1 (changeset f816841bab03)
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
739 self.assertTrue(f._rolled)
7aee0fe6ae5d Port #11488 patch from 3.1 (changeset f816841bab03)
R David Murray <rdmurray@bitdance.com>
parents: 68256
diff changeset
740
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
741 def test_sparse(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
742 # 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
743 # when that occurs
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
744 f = self.do_create(max_size=30)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
745 self.assertFalse(f._rolled)
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
746 f.seek(100, 0)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
747 self.assertFalse(f._rolled)
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
748 f.write('x')
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
749 self.assertTrue(f._rolled)
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
750
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
751 def test_fileno(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
752 # 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
753 f = self.do_create(max_size=30)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
754 self.assertFalse(f._rolled)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
755 self.assertTrue(f.fileno() > 0)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
756 self.assertTrue(f._rolled)
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
757
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
758 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
759 # 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
760 f = tempfile.SpooledTemporaryFile()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
761 f.write('abc\n')
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
762 self.assertFalse(f._rolled)
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
763 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
764 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
765 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
766 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
767 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
768 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
769
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 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
771 # 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
772 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
773 f.write('abc\n')
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
774 self.assertTrue(f._rolled)
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
775 f.close()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
776 try:
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
777 f.close()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
778 f.close()
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
779 except:
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
780 self.failOnException("close")
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
781
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
782 def test_bound_methods(self):
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
783 # 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
784 # 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
785 # methods should continue to function
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
786 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
787 read = f.read
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
788 write = f.write
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
789 seek = f.seek
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
790
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
791 write("a" * 35)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
792 write("b" * 35)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
793 seek(0, 0)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
794 self.assertTrue(read(70) == 'a'*35 + 'b'*35)
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
795
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
796 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
797 # 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
798 with tempfile.SpooledTemporaryFile(max_size=1) as f:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
799 self.assertFalse(f._rolled)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
800 self.assertFalse(f.closed)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
801 self.assertTrue(f.closed)
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
802 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
803 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
804 pass
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
805 self.assertRaises(ValueError, use_closed)
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
806
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
807 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
808 # 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
809 with tempfile.SpooledTemporaryFile(max_size=1) as f:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
810 self.assertFalse(f._rolled)
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
811 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
812 f.flush()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
813 self.assertTrue(f._rolled)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
814 self.assertFalse(f.closed)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
815 self.assertTrue(f.closed)
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
816 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
817 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
818 pass
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
819 self.assertRaises(ValueError, use_closed)
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
820
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
821 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
822 # 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
823 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
824 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
825 f.flush()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
826 self.assertTrue(f._rolled)
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
827 with f:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
828 self.assertFalse(f.closed)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
829 self.assertTrue(f.closed)
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
830 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
831 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
832 pass
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
833 self.assertRaises(ValueError, use_closed)
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
834
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
835
41234
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
836 test_classes.append(test_SpooledTemporaryFile)
8d06c1121b33 Patch #1630118: add a SpooledTemporaryFile class to tempfile.
Collin Winter <collinw@gmail.com>
parents: 41164
diff changeset
837
24708
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 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
840 """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
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 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
843 # 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
844 # 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
845 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
846 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
847 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
848 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
849
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
850 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
851 # 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
852 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
853 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
854 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
855
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
856 # 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
857 # 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
858 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
859 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
860 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
861 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
862 # cleanup
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
863 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
864 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
865 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
866
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
867 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
868 # 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
869 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
870 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
871 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
872 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
873 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
874 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
875 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
876 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
877
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
878 # 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
879
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
880
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
881 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
882 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
883
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
884 def test_main():
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 27161
diff changeset
885 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
886
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
887 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
888 test_main()