annotate Lib/test/test_tempfile.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 3c9ddd93c983
children dfc6902b63d7 588087429809
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
73743
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
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
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45256
diff changeset
10 from test import support
24708
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
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
13 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
14 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
15 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
16 else:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
17 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
18
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
19 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
20 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
21
24871
258e0b1347c2 Drop the number of test files to 100 for all the tests
Neal Norwitz <nnorwitz@gmail.com>
parents: 24811
diff changeset
22 # 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
23 # number of files that can be opened at one time (see ulimit -n)
61259
1f7506f23771 Remove traces of MacOS9 support.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59773
diff changeset
24 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
25 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
26 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
27 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
28
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
29 # 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
30 # 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
31 # 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
32
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
33 # 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
34 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
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 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
37
59773
d8700b4f1b9f Clean up warnings filter use in test_tempfile.
Brett Cannon <bcannon@gmail.com>
parents: 59615
diff changeset
38 def setUp(self):
d8700b4f1b9f Clean up warnings filter use in test_tempfile.
Brett Cannon <bcannon@gmail.com>
parents: 59615
diff changeset
39 self._warnings_manager = support.check_warnings()
d8700b4f1b9f Clean up warnings filter use in test_tempfile.
Brett Cannon <bcannon@gmail.com>
parents: 59615
diff changeset
40 self._warnings_manager.__enter__()
d8700b4f1b9f Clean up warnings filter use in test_tempfile.
Brett Cannon <bcannon@gmail.com>
parents: 59615
diff changeset
41 warnings.filterwarnings("ignore", category=RuntimeWarning,
d8700b4f1b9f Clean up warnings filter use in test_tempfile.
Brett Cannon <bcannon@gmail.com>
parents: 59615
diff changeset
42 message="mktemp", module=__name__)
d8700b4f1b9f Clean up warnings filter use in test_tempfile.
Brett Cannon <bcannon@gmail.com>
parents: 59615
diff changeset
43
d8700b4f1b9f Clean up warnings filter use in test_tempfile.
Brett Cannon <bcannon@gmail.com>
parents: 59615
diff changeset
44 def tearDown(self):
d8700b4f1b9f Clean up warnings filter use in test_tempfile.
Brett Cannon <bcannon@gmail.com>
parents: 59615
diff changeset
45 self._warnings_manager.__exit__(None, None, None)
d8700b4f1b9f Clean up warnings filter use in test_tempfile.
Brett Cannon <bcannon@gmail.com>
parents: 59615
diff changeset
46
d8700b4f1b9f Clean up warnings filter use in test_tempfile.
Brett Cannon <bcannon@gmail.com>
parents: 59615
diff changeset
47
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
48 def 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
49 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
50 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
51 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
52
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
53 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
54 (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
55 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
56 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
57
30681
9691cc644160 Patch #810914: Return absolute path for mkstemp. Fixes #810408.
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
58 # 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
59 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
60 "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
61 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
62 "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
63 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
64 "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
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 nbase = nbase[len(pre):len(nbase)-len(suf)]
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
67 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
68 "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
69 % nbase)
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 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
72
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
73 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
74 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
75 # 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
76 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
77
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
78 expected = {
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
79 "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
80 "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
81 "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
82 "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
83 "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
84 "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
85 "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
86 "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
87 "tempdir" : 1,
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
88 "template" : 1,
65618
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
89 "SpooledTemporaryFile" : 1,
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
90 "TemporaryDirectory" : 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
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
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
93 unexp = []
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
94 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
95 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
96 unexp.append(key)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
97 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
98 "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
99
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
100 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
101
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
102
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
103 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
104 """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
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 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
107 self.r = tempfile._RandomNameSequence()
59773
d8700b4f1b9f Clean up warnings filter use in test_tempfile.
Brett Cannon <bcannon@gmail.com>
parents: 59615
diff changeset
108 super().setUp()
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
109
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
110 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
111 # _RandomNameSequence returns a six-character string
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 40450
diff changeset
112 s = next(self.r)
24708
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, '', '', '')
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
114
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
115 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
116 # _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
117
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
118 dict = {}
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
119 r = self.r
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
120 for i in range(TEST_FILES):
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 40450
diff changeset
121 s = next(r)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
122 self.nameCheck(s, '', '', '')
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
123 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
124 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
125
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45256
diff changeset
126 def 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
127 # _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
128
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
129 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
130 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
131 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
132 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
133 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
134 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
135 break
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
136 except:
59613
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58473
diff changeset
137 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
138
73743
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
139 @unittest.skipUnless(hasattr(os, 'fork'),
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
140 "os.fork is required for this test")
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
141 def test_process_awareness(self):
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
142 # ensure that the random source differs between
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
143 # child and parent.
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
144 read_fd, write_fd = os.pipe()
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
145 pid = None
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
146 try:
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
147 pid = os.fork()
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
148 if not pid:
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
149 os.close(read_fd)
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
150 os.write(write_fd, next(self.r).encode("ascii"))
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
151 os.close(write_fd)
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
152 # bypass the normal exit handlers- leave those to
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
153 # the parent.
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
154 os._exit(0)
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
155 parent_value = next(self.r)
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
156 child_value = os.read(read_fd, len(parent_value)).decode("ascii")
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
157 finally:
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
158 if pid:
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
159 # best effort to ensure the process can't bleed out
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
160 # via any bugs above
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
161 try:
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
162 os.kill(pid, signal.SIGKILL)
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
163 except EnvironmentError:
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
164 pass
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
165 os.close(read_fd)
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
166 os.close(write_fd)
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
167 self.assertNotEqual(child_value, parent_value)
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
168
3c9ddd93c983 Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71608
diff changeset
169
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
170 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
171
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
172
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
173 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
174 """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
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_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
177 # _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
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 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
180
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
181 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
182 for c in cand:
58473
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58456
diff changeset
183 self.assertIsInstance(c, str)
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
184
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
185 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
186 # _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
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 # Make sure the interesting environment variables are all set.
53496
c42b7dd15505 test_support was renamed to support on py3k.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 53492
diff changeset
189 with 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
190 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
191 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
192 if not dirname:
53646
5c55b9c80846 Merged revisions 72167 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 53496
diff changeset
193 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
194
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
195 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
196
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
197 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
198 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
199 if not dirname: raise ValueError
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 57514
diff changeset
200 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
201
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
202 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
203 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
204 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
205 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
206
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 57514
diff changeset
207 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
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 # 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
210 # 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
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 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
213
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
214
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
215 # 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
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
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
218 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
219 """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
220
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
221 def 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
222 # _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
223 obj = tempfile._get_candidate_names()
58473
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58456
diff changeset
224 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
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 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
227 # _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
228 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
229 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
230
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
231 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
232
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
233 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
234
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 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
237 """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
238
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
239 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
240 _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
241 _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
242 _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
243 _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
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 __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
246 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
247 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
248
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
249 (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
250
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
251 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
252 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
253
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
254 def __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
255 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
256 self._unlink(self.name)
24715
4c746ba24de2 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24708
diff changeset
257
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
258 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
259 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
260 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
261 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
262 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
263 except:
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.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
265
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.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
267 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
268
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
269 def test_basic(self):
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
270 # _mkstemp_inner can create files
42925
00cbb33cb864 More str/bytes strictness.
Guido van Rossum <guido@python.org>
parents: 42902
diff changeset
271 self.do_create().write(b"blat")
00cbb33cb864 More str/bytes strictness.
Guido van Rossum <guido@python.org>
parents: 42902
diff changeset
272 self.do_create(pre="a").write(b"blat")
00cbb33cb864 More str/bytes strictness.
Guido van Rossum <guido@python.org>
parents: 42902
diff changeset
273 self.do_create(suf="b").write(b"blat")
00cbb33cb864 More str/bytes strictness.
Guido van Rossum <guido@python.org>
parents: 42902
diff changeset
274 self.do_create(pre="a", suf="b").write(b"blat")
00cbb33cb864 More str/bytes strictness.
Guido van Rossum <guido@python.org>
parents: 42902
diff changeset
275 self.do_create(pre="aa", suf=".txt").write(b"blat")
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
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
277 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
278 # _mkstemp_inner can create many files (stochastic)
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
279 extant = list(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
280 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
281 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
282
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
283 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
284 # _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
285 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
286 try:
49200
bdb660dca74d Issue #3782: os.write() must not accept unicode strings
Antoine Pitrou <solipsis@pitrou.net>
parents: 47201
diff changeset
287 self.do_create(dir=dir).write(b"blat")
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
288 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
289 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
290
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
291 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
292 # _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
293 if not has_stat:
52389
d4e7470bd660 Merged revisions 70554,70588-70589,70598,70605,70611-70621,70623-70624,70626-70627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49201
diff changeset
294 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
295
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
296 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
297 mode = stat.S_IMODE(os.stat(file.name).st_mode)
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41843
diff changeset
298 expected = 0o600
61259
1f7506f23771 Remove traces of MacOS9 support.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59773
diff changeset
299 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
300 # 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
301 # 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
302 user = expected >> 6
343e64c92eb2 There's no distinction among 'user', 'group' and 'world' permissions
Tim Peters <tim.peters@gmail.com>
parents: 24715
diff changeset
303 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
304 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
305
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
306 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
307 # _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
308 if not has_spawnl:
52389
d4e7470bd660 Merged revisions 70554,70588-70589,70598,70605,70611-70621,70623-70624,70626-70627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49201
diff changeset
309 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
310
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45256
diff changeset
311 if support.verbose:
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
312 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
313 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
314 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
315
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
316 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
317 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
318
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 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
320 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
321 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
322 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
323
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
324 # 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
325 # 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
326 # 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
327 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
328 "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
329
30761
0fe30710da80 Patch #813200: Quote executable path on Windows. Fixes #811082.
Martin v. Löwis <martin@v.loewis.de>
parents: 30681
diff changeset
330 # 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
331 # 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
332 # quotes on each end
44230
6b517ac10e8e Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
333 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
334 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
335 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
336 else:
0fe30710da80 Patch #813200: Quote executable path on Windows. Fixes #811082.
Martin v. Löwis <martin@v.loewis.de>
parents: 30681
diff changeset
337 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
338
0fe30710da80 Patch #813200: Quote executable path on Windows. Fixes #811082.
Martin v. Löwis <martin@v.loewis.de>
parents: 30681
diff changeset
339 retval = os.spawnl(os.P_WAIT, sys.executable, decorated, tester, v, fd)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
340 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
341 "child process caught fatal signal %d" % -retval)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
342 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
343
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
344 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
345 # _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
346 if not has_textmode:
52389
d4e7470bd660 Merged revisions 70554,70588-70589,70598,70605,70611-70621,70623-70624,70626-70627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49201
diff changeset
347 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
348
57514
9e51a14998a2 Merged revisions 76593 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 55593
diff changeset
349 # A text file is truncated at the first Ctrl+Z byte
9e51a14998a2 Merged revisions 76593 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 55593
diff changeset
350 f = self.do_create(bin=0)
9e51a14998a2 Merged revisions 76593 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 55593
diff changeset
351 f.write(b"blat\x1a")
9e51a14998a2 Merged revisions 76593 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 55593
diff changeset
352 f.write(b"extra\n")
9e51a14998a2 Merged revisions 76593 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 55593
diff changeset
353 os.lseek(f.fd, 0, os.SEEK_SET)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57514
diff changeset
354 self.assertEqual(os.read(f.fd, 20), b"blat")
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 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
357
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 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
360 """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
361
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
362 def test_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
363 # 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
364 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
365
58473
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58456
diff changeset
366 self.assertIsInstance(p, str)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
367 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
368
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
369 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
370 # 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
371
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
372 # 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
373 # 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
374 # 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
375 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
376 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
377 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
378 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
379 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
380 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
381 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
382 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
383 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
384 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
385 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
386 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
387
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
388 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
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
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
391 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
392 """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
393
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
394 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
395 # 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
396
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
397 dir = tempfile.gettempdir()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
398 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
399 "%s is not an absolute path" % dir)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
400 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
401 "%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
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_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
404 # 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
405
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
406 # 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
407 # 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
408 # gettempdir.
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
409 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
410 file = tempfile.NamedTemporaryFile()
42925
00cbb33cb864 More str/bytes strictness.
Guido van Rossum <guido@python.org>
parents: 42902
diff changeset
411 file.write(b"blat")
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
412 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
413 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
414 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
415
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
416 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
417 # 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
418 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
419 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
420
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
421 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
422
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
423 test_classes.append(test_gettempdir)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
424
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
425
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
426 class test_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
427 """Test mkstemp()."""
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
428
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 34588
diff changeset
429 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
430 if dir is None:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
431 dir = tempfile.gettempdir()
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
432 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
433 (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
434 (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
435 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
436 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
437 "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
438 except:
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.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
440
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
441 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
442 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
443 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
444 os.close(fd)
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
445 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
446
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
447 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
448 # 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
449 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
450 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
451 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
452 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
453 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
454 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
455
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
456 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
457 # 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
458 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
459 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
460 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
461 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
462 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
463
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
464 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
465
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
466
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
467 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
468 """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
469
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
470 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
471 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
472 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
473 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
474 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
475 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
476 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
477
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
478 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
479 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
480 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
481 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
482 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
483 raise
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
484
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
485 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
486 # 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
487 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
488 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
489 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
490 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
491 os.rmdir(self.do_create(pre="aa", suf=".txt"))
24715
4c746ba24de2 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24708
diff changeset
492
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 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
494 # mkdtemp can create many directories (stochastic)
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
495 extant = list(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
496 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
497 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
498 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
499 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
500 for i in extant:
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 42931
diff changeset
501 if(isinstance(i, str)):
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
502 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
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 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
505 # 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
506 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
507 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
508 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
509 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
510 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
511
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
512 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
513 # 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
514 if not has_stat:
52389
d4e7470bd660 Merged revisions 70554,70588-70589,70598,70605,70611-70621,70623-70624,70626-70627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49201
diff changeset
515 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
516
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
517 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
518 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
519 mode = stat.S_IMODE(os.stat(dir).st_mode)
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41843
diff changeset
520 mode &= 0o777 # Mask off sticky bits inherited from /tmp
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41843
diff changeset
521 expected = 0o700
61259
1f7506f23771 Remove traces of MacOS9 support.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 59773
diff changeset
522 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
523 # 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
524 # 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
525 user = expected >> 6
343e64c92eb2 There's no distinction among 'user', 'group' and 'world' permissions
Tim Peters <tim.peters@gmail.com>
parents: 24715
diff changeset
526 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
527 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
528 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
529 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
530
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
531 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
532
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 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
535 """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
536
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
537 # 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
538 # 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
539 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
540 self.dir = tempfile.mkdtemp()
59773
d8700b4f1b9f Clean up warnings filter use in test_tempfile.
Brett Cannon <bcannon@gmail.com>
parents: 59615
diff changeset
541 super().setUp()
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
542
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
543 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
544 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
545 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
546 self.dir = None
59773
d8700b4f1b9f Clean up warnings filter use in test_tempfile.
Brett Cannon <bcannon@gmail.com>
parents: 59615
diff changeset
547 super().tearDown()
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
548
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
549 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
550 _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
551 _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
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 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
554 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
555 # 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
556 # mysteriously appeared in the meanwhile.
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41843
diff changeset
557 os.close(os.open(self.name, self._bflags, 0o600))
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
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
559 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
560 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
561
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
562 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
563 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
564 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
565 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
566 self.failOnException("mktemp")
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
567
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
568 self.nameCheck(file.name, 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
569 return file
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
570
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
571 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
572 # 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
573 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
574 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
575 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
576 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
577 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
578
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
579 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
580 # mktemp can choose many usable file names (stochastic)
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
581 extant = list(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
582 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
583 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
584
26355
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
585 ## def test_warning(self):
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
586 ## # mktemp issues a warning when used
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
587 ## warnings.filterwarnings("error",
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
588 ## category=RuntimeWarning,
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
589 ## message="mktemp")
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
590 ## self.assertRaises(RuntimeWarning,
8fe296ad5936 Two bugs:
Fred Drake <fdrake@acm.org>
parents: 25830
diff changeset
591 ## 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
592
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
593 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
594
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 # 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
597
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 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
600 """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
601
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
602 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
603 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
604 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
605 try:
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
606 file = tempfile.NamedTemporaryFile(dir=dir, prefix=pre, suffix=suf,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
607 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
608 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
609 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
610
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
611 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
612 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
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
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
615 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
616 # 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
617 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
618 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
619 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
620 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
621 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
622
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
623 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
624 # 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
625 f = tempfile.NamedTemporaryFile()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
626 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
627 "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
628
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
629 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
630 # 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
631 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
632 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
633 f = tempfile.NamedTemporaryFile(dir=dir)
42925
00cbb33cb864 More str/bytes strictness.
Guido van Rossum <guido@python.org>
parents: 42902
diff changeset
634 f.write(b'blat')
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
635 f.close()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
636 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
637 "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
638 finally:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
639 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
640
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
641 def test_dis_del_on_close(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
642 # Tests that delete-on-close can be disabled
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
643 dir = tempfile.mkdtemp()
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
644 tmp = None
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
645 try:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
646 f = tempfile.NamedTemporaryFile(dir=dir, delete=False)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
647 tmp = f.name
42925
00cbb33cb864 More str/bytes strictness.
Guido van Rossum <guido@python.org>
parents: 42902
diff changeset
648 f.write(b'blat')
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
649 f.close()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
650 self.assertTrue(os.path.exists(f.name),
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
651 "NamedTemporaryFile %s missing after close" % f.name)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
652 finally:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
653 if tmp is not None:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
654 os.unlink(tmp)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
655 os.rmdir(dir)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
656
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
657 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
658 # 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
659 f = tempfile.NamedTemporaryFile()
42925
00cbb33cb864 More str/bytes strictness.
Guido van Rossum <guido@python.org>
parents: 42902
diff changeset
660 f.write(b'abc\n')
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
661 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
662 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
663 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
664 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
665 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
666 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
667
45256
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
668 def test_context_manager(self):
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
669 # A NamedTemporaryFile can be used as a context manager
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
670 with tempfile.NamedTemporaryFile() as f:
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
671 self.assertTrue(os.path.exists(f.name))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
672 self.assertFalse(os.path.exists(f.name))
45256
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
673 def use_closed():
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
674 with f:
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
675 pass
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
676 self.assertRaises(ValueError, use_closed)
45256
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
677
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
678 # 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
679
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
680 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
681
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
682 class test_SpooledTemporaryFile(TC):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
683 """Test SpooledTemporaryFile()."""
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
684
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
685 def do_create(self, max_size=0, dir=None, pre="", suf=""):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
686 if dir is None:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
687 dir = tempfile.gettempdir()
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
688 try:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
689 file = tempfile.SpooledTemporaryFile(max_size=max_size, dir=dir, prefix=pre, suffix=suf)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
690 except:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
691 self.failOnException("SpooledTemporaryFile")
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
692
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
693 return file
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
694
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
695
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
696 def test_basic(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
697 # SpooledTemporaryFile can create files
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
698 f = self.do_create()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
699 self.assertFalse(f._rolled)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
700 f = self.do_create(max_size=100, pre="a", suf=".txt")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
701 self.assertFalse(f._rolled)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
702
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
703 def test_del_on_close(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
704 # A SpooledTemporaryFile is deleted when closed
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
705 dir = tempfile.mkdtemp()
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
706 try:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
707 f = tempfile.SpooledTemporaryFile(max_size=10, dir=dir)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
708 self.assertFalse(f._rolled)
42902
62874b34d95d Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42162
diff changeset
709 f.write(b'blat ' * 5)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
710 self.assertTrue(f._rolled)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
711 filename = f.name
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
712 f.close()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
713 self.assertFalse(isinstance(filename, str) and os.path.exists(filename),
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
714 "SpooledTemporaryFile %s exists after close" % filename)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
715 finally:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
716 os.rmdir(dir)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
717
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
718 def test_rewrite_small(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
719 # A SpooledTemporaryFile can be written to multiple within the max_size
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
720 f = self.do_create(max_size=30)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
721 self.assertFalse(f._rolled)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
722 for i in range(5):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
723 f.seek(0, 0)
42902
62874b34d95d Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42162
diff changeset
724 f.write(b'x' * 20)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
725 self.assertFalse(f._rolled)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
726
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
727 def test_write_sequential(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
728 # A SpooledTemporaryFile should hold exactly max_size bytes, and roll
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
729 # over afterward
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
730 f = self.do_create(max_size=30)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
731 self.assertFalse(f._rolled)
42902
62874b34d95d Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42162
diff changeset
732 f.write(b'x' * 20)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
733 self.assertFalse(f._rolled)
42902
62874b34d95d Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42162
diff changeset
734 f.write(b'x' * 10)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
735 self.assertFalse(f._rolled)
42902
62874b34d95d Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42162
diff changeset
736 f.write(b'x')
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
737 self.assertTrue(f._rolled)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
738
68443
f816841bab03 #11488: Add tests for writelines method of SpooledTemporaryFile.
R David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
739 def test_writelines(self):
f816841bab03 #11488: Add tests for writelines method of SpooledTemporaryFile.
R David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
740 # Verify writelines with a SpooledTemporaryFile
f816841bab03 #11488: Add tests for writelines method of SpooledTemporaryFile.
R David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
741 f = self.do_create()
f816841bab03 #11488: Add tests for writelines method of SpooledTemporaryFile.
R David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
742 f.writelines((b'x', b'y', b'z'))
f816841bab03 #11488: Add tests for writelines method of SpooledTemporaryFile.
R David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
743 f.seek(0)
f816841bab03 #11488: Add tests for writelines method of SpooledTemporaryFile.
R David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
744 buf = f.read()
f816841bab03 #11488: Add tests for writelines method of SpooledTemporaryFile.
R David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
745 self.assertEqual(buf, b'xyz')
f816841bab03 #11488: Add tests for writelines method of SpooledTemporaryFile.
R David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
746
f816841bab03 #11488: Add tests for writelines method of SpooledTemporaryFile.
R David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
747 def test_writelines_sequential(self):
f816841bab03 #11488: Add tests for writelines method of SpooledTemporaryFile.
R David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
748 # A SpooledTemporaryFile should hold exactly max_size bytes, and roll
f816841bab03 #11488: Add tests for writelines method of SpooledTemporaryFile.
R David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
749 # over afterward
f816841bab03 #11488: Add tests for writelines method of SpooledTemporaryFile.
R David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
750 f = self.do_create(max_size=35)
f816841bab03 #11488: Add tests for writelines method of SpooledTemporaryFile.
R David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
751 f.writelines((b'x' * 20, b'x' * 10, b'x' * 5))
f816841bab03 #11488: Add tests for writelines method of SpooledTemporaryFile.
R David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
752 self.assertFalse(f._rolled)
f816841bab03 #11488: Add tests for writelines method of SpooledTemporaryFile.
R David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
753 f.write(b'x')
f816841bab03 #11488: Add tests for writelines method of SpooledTemporaryFile.
R David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
754 self.assertTrue(f._rolled)
f816841bab03 #11488: Add tests for writelines method of SpooledTemporaryFile.
R David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
755
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
756 def test_sparse(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
757 # A SpooledTemporaryFile that is written late in the file will extend
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
758 # when that occurs
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
759 f = self.do_create(max_size=30)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
760 self.assertFalse(f._rolled)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
761 f.seek(100, 0)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
762 self.assertFalse(f._rolled)
42902
62874b34d95d Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42162
diff changeset
763 f.write(b'x')
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
764 self.assertTrue(f._rolled)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
765
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
766 def test_fileno(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
767 # A SpooledTemporaryFile should roll over to a real file on fileno()
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
768 f = self.do_create(max_size=30)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
769 self.assertFalse(f._rolled)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
770 self.assertTrue(f.fileno() > 0)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
771 self.assertTrue(f._rolled)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
772
45256
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
773 def test_multiple_close_before_rollover(self):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
774 # A SpooledTemporaryFile can be closed many times without error
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
775 f = tempfile.SpooledTemporaryFile()
42902
62874b34d95d Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42162
diff changeset
776 f.write(b'abc\n')
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
777 self.assertFalse(f._rolled)
45256
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
778 f.close()
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
779 try:
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
780 f.close()
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
781 f.close()
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
782 except:
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
783 self.failOnException("close")
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
784
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
785 def test_multiple_close_after_rollover(self):
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
786 # A SpooledTemporaryFile can be closed many times without error
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
787 f = tempfile.SpooledTemporaryFile(max_size=1)
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
788 f.write(b'abc\n')
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
789 self.assertTrue(f._rolled)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
790 f.close()
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
791 try:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
792 f.close()
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
793 f.close()
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
794 except:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
795 self.failOnException("close")
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
796
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
797 def test_bound_methods(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
798 # It should be OK to steal a bound method from a SpooledTemporaryFile
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
799 # and use it independently; when the file rolls over, those bound
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
800 # methods should continue to function
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
801 f = self.do_create(max_size=30)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
802 read = f.read
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
803 write = f.write
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
804 seek = f.seek
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
805
42902
62874b34d95d Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42162
diff changeset
806 write(b"a" * 35)
62874b34d95d Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42162
diff changeset
807 write(b"b" * 35)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
808 seek(0, 0)
42162
a62eccb53f30 Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
809 self.assertEqual(read(70), b'a'*35 + b'b'*35)
a62eccb53f30 Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
810
a62eccb53f30 Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
811 def test_text_mode(self):
a62eccb53f30 Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
812 # Creating a SpooledTemporaryFile with a text mode should produce
a62eccb53f30 Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
813 # a file object reading and writing (Unicode) text strings.
a62eccb53f30 Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
814 f = tempfile.SpooledTemporaryFile(mode='w+', max_size=10)
a62eccb53f30 Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
815 f.write("abc\n")
a62eccb53f30 Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
816 f.seek(0)
a62eccb53f30 Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
817 self.assertEqual(f.read(), "abc\n")
a62eccb53f30 Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
818 f.write("def\n")
a62eccb53f30 Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
819 f.seek(0)
a62eccb53f30 Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
820 self.assertEqual(f.read(), "abc\ndef\n")
a62eccb53f30 Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
821 f.write("xyzzy\n")
a62eccb53f30 Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
822 f.seek(0)
a62eccb53f30 Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
823 self.assertEqual(f.read(), "abc\ndef\nxyzzy\n")
57514
9e51a14998a2 Merged revisions 76593 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 55593
diff changeset
824 # Check that Ctrl+Z doesn't truncate the file
9e51a14998a2 Merged revisions 76593 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 55593
diff changeset
825 f.write("foo\x1abar\n")
9e51a14998a2 Merged revisions 76593 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 55593
diff changeset
826 f.seek(0)
9e51a14998a2 Merged revisions 76593 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 55593
diff changeset
827 self.assertEqual(f.read(), "abc\ndef\nxyzzy\nfoo\x1abar\n")
42162
a62eccb53f30 Make test_tempfile.py work. Make SpooledTempFile work in text and binary mode.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
828
42931
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
829 def test_text_newline_and_encoding(self):
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
830 f = tempfile.SpooledTemporaryFile(mode='w+', max_size=10,
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
831 newline='', encoding='utf-8')
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
832 f.write("\u039B\r\n")
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
833 f.seek(0)
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
834 self.assertEqual(f.read(), "\u039B\r\n")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
835 self.assertFalse(f._rolled)
42931
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
836
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
837 f.write("\u039B" * 20 + "\r\n")
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
838 f.seek(0)
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
839 self.assertEqual(f.read(), "\u039B\r\n" + ("\u039B" * 20) + "\r\n")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
840 self.assertTrue(f._rolled)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
841
45256
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
842 def test_context_manager_before_rollover(self):
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
843 # A SpooledTemporaryFile can be used as a context manager
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
844 with tempfile.SpooledTemporaryFile(max_size=1) as f:
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
845 self.assertFalse(f._rolled)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
846 self.assertFalse(f.closed)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
847 self.assertTrue(f.closed)
45256
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
848 def use_closed():
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
849 with f:
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
850 pass
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
851 self.assertRaises(ValueError, use_closed)
45256
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
852
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
853 def test_context_manager_during_rollover(self):
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
854 # A SpooledTemporaryFile can be used as a context manager
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
855 with tempfile.SpooledTemporaryFile(max_size=1) as f:
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
856 self.assertFalse(f._rolled)
45256
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
857 f.write(b'abc\n')
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
858 f.flush()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
859 self.assertTrue(f._rolled)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
860 self.assertFalse(f.closed)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
861 self.assertTrue(f.closed)
45256
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
862 def use_closed():
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
863 with f:
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
864 pass
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
865 self.assertRaises(ValueError, use_closed)
45256
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
866
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
867 def test_context_manager_after_rollover(self):
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
868 # A SpooledTemporaryFile can be used as a context manager
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
869 f = tempfile.SpooledTemporaryFile(max_size=1)
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
870 f.write(b'abc\n')
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
871 f.flush()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
872 self.assertTrue(f._rolled)
45256
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
873 with f:
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
874 self.assertFalse(f.closed)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
875 self.assertTrue(f.closed)
45256
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
876 def use_closed():
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
877 with f:
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
878 pass
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53646
diff changeset
879 self.assertRaises(ValueError, use_closed)
45256
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
880
bc62854ee9c2 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678-60695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44230
diff changeset
881
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
882 test_classes.append(test_SpooledTemporaryFile)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
883
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
884
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
885 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
886 """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
887
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
888 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
889 # 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
890 # 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
891 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
892 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
893 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
894 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
895
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
896 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
897 # 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
898 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
899 f = tempfile.TemporaryFile(dir=dir)
42925
00cbb33cb864 More str/bytes strictness.
Guido van Rossum <guido@python.org>
parents: 42902
diff changeset
900 f.write(b'blat')
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
901
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
902 # 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
903 # 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
904 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
905 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
906 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
907 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
908 # cleanup
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
909 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
910 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
911 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
912
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
913 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
914 # 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
915 f = tempfile.TemporaryFile()
42925
00cbb33cb864 More str/bytes strictness.
Guido van Rossum <guido@python.org>
parents: 42902
diff changeset
916 f.write(b'abc\n')
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
917 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
918 try:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
919 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
920 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
921 except:
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
922 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
923
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
924 # How to test the mode and bufsize parameters?
42931
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
925 def test_mode_and_encoding(self):
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
926
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
927 def roundtrip(input, *args, **kwargs):
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
928 with tempfile.TemporaryFile(*args, **kwargs) as fileobj:
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
929 fileobj.write(input)
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
930 fileobj.seek(0)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57514
diff changeset
931 self.assertEqual(input, fileobj.read())
42931
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
932
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
933 roundtrip(b"1234", "w+b")
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
934 roundtrip("abdc\n", "w+")
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
935 roundtrip("\u039B", "w+", encoding="utf-16")
aee21e0c9a70 Patch # 1033 by Adam Hupp:
Guido van Rossum <guido@python.org>
parents: 42925
diff changeset
936 roundtrip("foo\r\n", "w+", newline="")
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
937
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
938
24960
b933d19ae851 Standardize behavior: no docstrings in test functions. Also get rid
Guido van Rossum <guido@python.org>
parents: 24879
diff changeset
939 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
940 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
941
65618
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
942
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
943 # Helper for test_del_on_shutdown
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
944 class NulledModules:
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
945 def __init__(self, *modules):
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
946 self.refs = [mod.__dict__ for mod in modules]
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
947 self.contents = [ref.copy() for ref in self.refs]
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
948
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
949 def __enter__(self):
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
950 for d in self.refs:
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
951 for key in d:
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
952 d[key] = None
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
953
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
954 def __exit__(self, *exc_info):
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
955 for d, c in zip(self.refs, self.contents):
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
956 d.clear()
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
957 d.update(c)
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
958
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
959 class test_TemporaryDirectory(TC):
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
960 """Test TemporaryDirectory()."""
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
961
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
962 def do_create(self, dir=None, pre="", suf="", recurse=1):
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
963 if dir is None:
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
964 dir = tempfile.gettempdir()
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
965 try:
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
966 tmp = tempfile.TemporaryDirectory(dir=dir, prefix=pre, suffix=suf)
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
967 except:
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
968 self.failOnException("TemporaryDirectory")
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
969 self.nameCheck(tmp.name, dir, pre, suf)
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
970 # Create a subdirectory and some files
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
971 if recurse:
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
972 self.do_create(tmp.name, pre, suf, recurse-1)
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
973 with open(os.path.join(tmp.name, "test.txt"), "wb") as f:
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
974 f.write(b"Hello world!")
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
975 return tmp
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
976
66843
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
977 def test_mkdtemp_failure(self):
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
978 # Check no additional exception if mkdtemp fails
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
979 # Previously would raise AttributeError instead
66865
2872f46e8197 Actually finish the tests for r87182
Nick Coghlan <ncoghlan@gmail.com>
parents: 66843
diff changeset
980 # (noted as part of Issue #10188)
2872f46e8197 Actually finish the tests for r87182
Nick Coghlan <ncoghlan@gmail.com>
parents: 66843
diff changeset
981 with tempfile.TemporaryDirectory() as nonexistent:
2872f46e8197 Actually finish the tests for r87182
Nick Coghlan <ncoghlan@gmail.com>
parents: 66843
diff changeset
982 pass
2872f46e8197 Actually finish the tests for r87182
Nick Coghlan <ncoghlan@gmail.com>
parents: 66843
diff changeset
983 with self.assertRaises(os.error):
2872f46e8197 Actually finish the tests for r87182
Nick Coghlan <ncoghlan@gmail.com>
parents: 66843
diff changeset
984 tempfile.TemporaryDirectory(dir=nonexistent)
66843
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
985
65618
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
986 def test_explicit_cleanup(self):
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
987 # A TemporaryDirectory is deleted when cleaned up
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
988 dir = tempfile.mkdtemp()
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
989 try:
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
990 d = self.do_create(dir=dir)
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
991 self.assertTrue(os.path.exists(d.name),
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
992 "TemporaryDirectory %s does not exist" % d.name)
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
993 d.cleanup()
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
994 self.assertFalse(os.path.exists(d.name),
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
995 "TemporaryDirectory %s exists after cleanup" % d.name)
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
996 finally:
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
997 os.rmdir(dir)
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
998
71608
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
999 @support.skip_unless_symlink
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1000 def test_cleanup_with_symlink_to_a_directory(self):
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1001 # cleanup() should not follow symlinks to directories (issue #12464)
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1002 d1 = self.do_create()
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1003 d2 = self.do_create()
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1004
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1005 # Symlink d1/foo -> d2
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1006 os.symlink(d2.name, os.path.join(d1.name, "foo"))
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1007
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1008 # This call to cleanup() should not follow the "foo" symlink
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1009 d1.cleanup()
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1010
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1011 self.assertFalse(os.path.exists(d1.name),
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1012 "TemporaryDirectory %s exists after cleanup" % d1.name)
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1013 self.assertTrue(os.path.exists(d2.name),
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1014 "Directory pointed to by a symlink was deleted")
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1015 self.assertEqual(os.listdir(d2.name), ['test.txt'],
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1016 "Contents of the directory pointed to by a symlink "
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1017 "were deleted")
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1018 d2.cleanup()
5f7e71cfbcd6 Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
Charles-François Natali <neologix@free.fr>
parents: 68444
diff changeset
1019
65618
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1020 @support.cpython_only
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1021 def test_del_on_collection(self):
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1022 # A TemporaryDirectory is deleted when garbage collected
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1023 dir = tempfile.mkdtemp()
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1024 try:
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1025 d = self.do_create(dir=dir)
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1026 name = d.name
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1027 del d # Rely on refcounting to invoke __del__
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1028 self.assertFalse(os.path.exists(name),
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1029 "TemporaryDirectory %s exists after __del__" % name)
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1030 finally:
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1031 os.rmdir(dir)
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1032
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1033 @unittest.expectedFailure # See issue #10188
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1034 def test_del_on_shutdown(self):
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1035 # A TemporaryDirectory may be cleaned up during shutdown
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1036 # Make sure it works with the relevant modules nulled out
66843
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1037 with self.do_create() as dir:
65618
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1038 d = self.do_create(dir=dir)
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1039 # Mimic the nulling out of modules that
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1040 # occurs during system shutdown
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1041 modules = [os, os.path]
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1042 if has_stat:
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1043 modules.append(stat)
66843
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1044 # Currently broken, so suppress the warning
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1045 # that is otherwise emitted on stdout
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1046 with support.captured_stderr() as err:
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1047 with NulledModules(*modules):
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1048 d.cleanup()
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1049 # Currently broken, so stop spurious exception by
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1050 # indicating the object has already been closed
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1051 d._closed = True
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1052 # And this assert will fail, as expected by the
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1053 # unittest decorator...
65618
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1054 self.assertFalse(os.path.exists(d.name),
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1055 "TemporaryDirectory %s exists after cleanup" % d.name)
66843
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1056
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1057 def test_warnings_on_cleanup(self):
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1058 # Two kinds of warning on shutdown
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1059 # Issue 10888: may write to stderr if modules are nulled out
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1060 # ResourceWarning will be triggered by __del__
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1061 with self.do_create() as dir:
66872
f7e9ea8c61a0 Captured IO streams with embedded backslashes are always such a fun combination...
Nick Coghlan <ncoghlan@gmail.com>
parents: 66865
diff changeset
1062 if os.sep != '\\':
f7e9ea8c61a0 Captured IO streams with embedded backslashes are always such a fun combination...
Nick Coghlan <ncoghlan@gmail.com>
parents: 66865
diff changeset
1063 # Embed a backslash in order to make sure string escaping
f7e9ea8c61a0 Captured IO streams with embedded backslashes are always such a fun combination...
Nick Coghlan <ncoghlan@gmail.com>
parents: 66865
diff changeset
1064 # in the displayed error message is dealt with correctly
f7e9ea8c61a0 Captured IO streams with embedded backslashes are always such a fun combination...
Nick Coghlan <ncoghlan@gmail.com>
parents: 66865
diff changeset
1065 suffix = '\\check_backslash_handling'
f7e9ea8c61a0 Captured IO streams with embedded backslashes are always such a fun combination...
Nick Coghlan <ncoghlan@gmail.com>
parents: 66865
diff changeset
1066 else:
f7e9ea8c61a0 Captured IO streams with embedded backslashes are always such a fun combination...
Nick Coghlan <ncoghlan@gmail.com>
parents: 66865
diff changeset
1067 suffix = ''
f7e9ea8c61a0 Captured IO streams with embedded backslashes are always such a fun combination...
Nick Coghlan <ncoghlan@gmail.com>
parents: 66865
diff changeset
1068 d = self.do_create(dir=dir, suf=suffix)
66843
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1069
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1070 #Check for the Issue 10888 message
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1071 modules = [os, os.path]
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1072 if has_stat:
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1073 modules.append(stat)
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1074 with support.captured_stderr() as err:
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1075 with NulledModules(*modules):
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1076 d.cleanup()
66872
f7e9ea8c61a0 Captured IO streams with embedded backslashes are always such a fun combination...
Nick Coghlan <ncoghlan@gmail.com>
parents: 66865
diff changeset
1077 message = err.getvalue().replace('\\\\', '\\')
66843
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1078 self.assertIn("while cleaning up", message)
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1079 self.assertIn(d.name, message)
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1080
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1081 # Check for the resource warning
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1082 with support.check_warnings(('Implicitly', ResourceWarning), quiet=False):
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1083 warnings.filterwarnings("always", category=ResourceWarning)
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1084 d.__del__()
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1085 self.assertFalse(os.path.exists(d.name),
612dd5f2aab3 Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
1086 "TemporaryDirectory %s exists after __del__" % d.name)
65618
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1087
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1088 def test_multiple_close(self):
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1089 # Can be cleaned-up many times without error
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1090 d = self.do_create()
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1091 d.cleanup()
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1092 try:
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1093 d.cleanup()
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1094 d.cleanup()
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1095 except:
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1096 self.failOnException("cleanup")
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1097
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1098 def test_context_manager(self):
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1099 # Can be used as a context manager
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1100 d = self.do_create()
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1101 with d as name:
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1102 self.assertTrue(os.path.exists(name))
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1103 self.assertEqual(name, d.name)
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1104 self.assertFalse(os.path.exists(name))
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1105
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1106
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1107 test_classes.append(test_TemporaryDirectory)
b172d7537b99 Issue 5178: Add tempfile.TemporaryDirectory (original patch by Neil Schemenauer)
Nick Coghlan <ncoghlan@gmail.com>
parents: 61259
diff changeset
1108
24708
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
1109 def test_main():
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45256
diff changeset
1110 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
1111
aa8bd0dc4f8a Check-in of the most essential parts of SF 589982 (tempfile.py
Guido van Rossum <guido@python.org>
parents: 20752
diff changeset
1112 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
1113 test_main()