Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 1 | # tempfile.py unit tests. |
Tim Peters | c57a285 | 2001-10-29 21:46:08 +0000 | [diff] [blame] | 2 | import tempfile |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 3 | import os |
| 4 | import sys |
| 5 | import re |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 6 | import warnings |
Tim Peters | c57a285 | 2001-10-29 21:46:08 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 8 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | from test import support |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 10 | |
Fred Drake | 7633d23 | 2002-10-17 22:09:03 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 12 | if hasattr(os, 'stat'): |
| 13 | import stat |
| 14 | has_stat = 1 |
| 15 | else: |
| 16 | has_stat = 0 |
| 17 | |
| 18 | has_textmode = (tempfile._text_openflags != tempfile._bin_openflags) |
Guido van Rossum | 7874106 | 2002-08-17 11:41:01 +0000 | [diff] [blame] | 19 | has_spawnl = hasattr(os, 'spawnl') |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 20 | |
Neal Norwitz | 68ee012 | 2002-08-16 19:28:59 +0000 | [diff] [blame] | 21 | # TEST_FILES may need to be tweaked for systems depending on the maximum |
| 22 | # number of files that can be opened at one time (see ulimit -n) |
Ronald Oussoren | 94f2528 | 2010-05-05 19:11:21 +0000 | [diff] [blame] | 23 | if sys.platform in ('openbsd3', 'openbsd4'): |
Martin v. Löwis | 9996828 | 2004-09-15 06:02:54 +0000 | [diff] [blame] | 24 | TEST_FILES = 48 |
Jack Jansen | ce92147 | 2003-01-08 16:30:34 +0000 | [diff] [blame] | 25 | else: |
| 26 | TEST_FILES = 100 |
Neal Norwitz | 68ee012 | 2002-08-16 19:28:59 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 28 | # This is organized as one test for each chunk of code in tempfile.py, |
| 29 | # in order of their appearance in the file. Testing which requires |
| 30 | # threads is not done here. |
| 31 | |
| 32 | # Common functionality. |
| 33 | class TC(unittest.TestCase): |
| 34 | |
| 35 | str_check = re.compile(r"[a-zA-Z0-9_-]{6}$") |
| 36 | |
Brett Cannon | e1adece | 2010-03-20 22:19:55 +0000 | [diff] [blame] | 37 | def setUp(self): |
| 38 | self._warnings_manager = support.check_warnings() |
| 39 | self._warnings_manager.__enter__() |
| 40 | warnings.filterwarnings("ignore", category=RuntimeWarning, |
| 41 | message="mktemp", module=__name__) |
| 42 | |
| 43 | def tearDown(self): |
| 44 | self._warnings_manager.__exit__(None, None, None) |
| 45 | |
| 46 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 47 | def failOnException(self, what, ei=None): |
| 48 | if ei is None: |
| 49 | ei = sys.exc_info() |
| 50 | self.fail("%s raised %s: %s" % (what, ei[0], ei[1])) |
| 51 | |
| 52 | def nameCheck(self, name, dir, pre, suf): |
| 53 | (ndir, nbase) = os.path.split(name) |
| 54 | npre = nbase[:len(pre)] |
| 55 | nsuf = nbase[len(nbase)-len(suf):] |
| 56 | |
Martin v. Löwis | d662548 | 2003-10-12 17:37:01 +0000 | [diff] [blame] | 57 | # check for equality of the absolute paths! |
| 58 | self.assertEqual(os.path.abspath(ndir), os.path.abspath(dir), |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 59 | "file '%s' not in directory '%s'" % (name, dir)) |
| 60 | self.assertEqual(npre, pre, |
| 61 | "file '%s' does not begin with '%s'" % (nbase, pre)) |
| 62 | self.assertEqual(nsuf, suf, |
| 63 | "file '%s' does not end with '%s'" % (nbase, suf)) |
| 64 | |
| 65 | nbase = nbase[len(pre):len(nbase)-len(suf)] |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 66 | self.assertTrue(self.str_check.match(nbase), |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 67 | "random string '%s' does not match /^[a-zA-Z0-9_-]{6}$/" |
| 68 | % nbase) |
| 69 | |
| 70 | test_classes = [] |
| 71 | |
| 72 | class test_exports(TC): |
| 73 | def test_exports(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 74 | # There are no surprising symbols in the tempfile module |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 75 | dict = tempfile.__dict__ |
| 76 | |
| 77 | expected = { |
| 78 | "NamedTemporaryFile" : 1, |
| 79 | "TemporaryFile" : 1, |
| 80 | "mkstemp" : 1, |
| 81 | "mkdtemp" : 1, |
| 82 | "mktemp" : 1, |
| 83 | "TMP_MAX" : 1, |
| 84 | "gettempprefix" : 1, |
| 85 | "gettempdir" : 1, |
| 86 | "tempdir" : 1, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 87 | "template" : 1, |
Nick Coghlan | 543af75 | 2010-10-24 11:23:25 +0000 | [diff] [blame] | 88 | "SpooledTemporaryFile" : 1, |
| 89 | "TemporaryDirectory" : 1, |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | unexp = [] |
| 93 | for key in dict: |
| 94 | if key[0] != '_' and key not in expected: |
| 95 | unexp.append(key) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 96 | self.assertTrue(len(unexp) == 0, |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 97 | "unexpected keys: %s" % unexp) |
| 98 | |
| 99 | test_classes.append(test_exports) |
| 100 | |
| 101 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 102 | class test__RandomNameSequence(TC): |
| 103 | """Test the internal iterator object _RandomNameSequence.""" |
| 104 | |
| 105 | def setUp(self): |
| 106 | self.r = tempfile._RandomNameSequence() |
Brett Cannon | e1adece | 2010-03-20 22:19:55 +0000 | [diff] [blame] | 107 | super().setUp() |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 108 | |
| 109 | def test_get_six_char_str(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 110 | # _RandomNameSequence returns a six-character string |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 111 | s = next(self.r) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 112 | self.nameCheck(s, '', '', '') |
| 113 | |
| 114 | def test_many(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 115 | # _RandomNameSequence returns no duplicate strings (stochastic) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 116 | |
| 117 | dict = {} |
| 118 | r = self.r |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 119 | for i in range(TEST_FILES): |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 120 | s = next(r) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 121 | self.nameCheck(s, '', '', '') |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 122 | self.assertNotIn(s, dict) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 123 | dict[s] = 1 |
| 124 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 125 | def supports_iter(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 126 | # _RandomNameSequence supports the iterator protocol |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 127 | |
| 128 | i = 0 |
| 129 | r = self.r |
| 130 | try: |
| 131 | for s in r: |
| 132 | i += 1 |
| 133 | if i == 20: |
| 134 | break |
| 135 | except: |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 136 | self.failOnException("iteration") |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 137 | |
| 138 | test_classes.append(test__RandomNameSequence) |
| 139 | |
| 140 | |
| 141 | class test__candidate_tempdir_list(TC): |
| 142 | """Test the internal function _candidate_tempdir_list.""" |
| 143 | |
| 144 | def test_nonempty_list(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 145 | # _candidate_tempdir_list returns a nonempty list of strings |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 146 | |
| 147 | cand = tempfile._candidate_tempdir_list() |
| 148 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 149 | self.assertFalse(len(cand) == 0) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 150 | for c in cand: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 151 | self.assertIsInstance(c, str) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 152 | |
| 153 | def test_wanted_dirs(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 154 | # _candidate_tempdir_list contains the expected directories |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 155 | |
| 156 | # Make sure the interesting environment variables are all set. |
Hirokazu Yamamoto | 7195963 | 2009-04-27 01:44:28 +0000 | [diff] [blame] | 157 | with support.EnvironmentVarGuard() as env: |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 158 | for envname in 'TMPDIR', 'TEMP', 'TMP': |
| 159 | dirname = os.getenv(envname) |
| 160 | if not dirname: |
Walter Dörwald | 155374d | 2009-05-01 19:58:58 +0000 | [diff] [blame] | 161 | env[envname] = os.path.abspath(envname) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 162 | |
| 163 | cand = tempfile._candidate_tempdir_list() |
| 164 | |
| 165 | for envname in 'TMPDIR', 'TEMP', 'TMP': |
| 166 | dirname = os.getenv(envname) |
| 167 | if not dirname: raise ValueError |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 168 | self.assertIn(dirname, cand) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 169 | |
| 170 | try: |
| 171 | dirname = os.getcwd() |
| 172 | except (AttributeError, os.error): |
| 173 | dirname = os.curdir |
| 174 | |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 175 | self.assertIn(dirname, cand) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 176 | |
| 177 | # Not practical to try to verify the presence of OS-specific |
| 178 | # paths in this list. |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 179 | |
| 180 | test_classes.append(test__candidate_tempdir_list) |
| 181 | |
| 182 | |
| 183 | # We test _get_default_tempdir by testing gettempdir. |
| 184 | |
| 185 | |
| 186 | class test__get_candidate_names(TC): |
| 187 | """Test the internal function _get_candidate_names.""" |
| 188 | |
| 189 | def test_retval(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 190 | # _get_candidate_names returns a _RandomNameSequence object |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 191 | obj = tempfile._get_candidate_names() |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 192 | self.assertIsInstance(obj, tempfile._RandomNameSequence) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 193 | |
| 194 | def test_same_thing(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 195 | # _get_candidate_names always returns the same object |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 196 | a = tempfile._get_candidate_names() |
| 197 | b = tempfile._get_candidate_names() |
| 198 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 199 | self.assertTrue(a is b) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 200 | |
| 201 | test_classes.append(test__get_candidate_names) |
| 202 | |
| 203 | |
| 204 | class test__mkstemp_inner(TC): |
| 205 | """Test the internal function _mkstemp_inner.""" |
| 206 | |
| 207 | class mkstemped: |
| 208 | _bflags = tempfile._bin_openflags |
| 209 | _tflags = tempfile._text_openflags |
| 210 | _close = os.close |
| 211 | _unlink = os.unlink |
| 212 | |
| 213 | def __init__(self, dir, pre, suf, bin): |
| 214 | if bin: flags = self._bflags |
| 215 | else: flags = self._tflags |
| 216 | |
| 217 | (self.fd, self.name) = tempfile._mkstemp_inner(dir, pre, suf, flags) |
| 218 | |
| 219 | def write(self, str): |
| 220 | os.write(self.fd, str) |
| 221 | |
| 222 | def __del__(self): |
| 223 | self._close(self.fd) |
| 224 | self._unlink(self.name) |
Tim Peters | a0d55de | 2002-08-09 18:01:01 +0000 | [diff] [blame] | 225 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 226 | def do_create(self, dir=None, pre="", suf="", bin=1): |
| 227 | if dir is None: |
| 228 | dir = tempfile.gettempdir() |
| 229 | try: |
| 230 | file = self.mkstemped(dir, pre, suf, bin) |
| 231 | except: |
| 232 | self.failOnException("_mkstemp_inner") |
| 233 | |
| 234 | self.nameCheck(file.name, dir, pre, suf) |
| 235 | return file |
| 236 | |
| 237 | def test_basic(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 238 | # _mkstemp_inner can create files |
Guido van Rossum | ec42ffd | 2007-08-27 23:40:36 +0000 | [diff] [blame] | 239 | self.do_create().write(b"blat") |
| 240 | self.do_create(pre="a").write(b"blat") |
| 241 | self.do_create(suf="b").write(b"blat") |
| 242 | self.do_create(pre="a", suf="b").write(b"blat") |
| 243 | self.do_create(pre="aa", suf=".txt").write(b"blat") |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 244 | |
| 245 | def test_basic_many(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 246 | # _mkstemp_inner can create many files (stochastic) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 247 | extant = list(range(TEST_FILES)) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 248 | for i in extant: |
| 249 | extant[i] = self.do_create(pre="aa") |
| 250 | |
| 251 | def test_choose_directory(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 252 | # _mkstemp_inner can create files in a user-selected directory |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 253 | dir = tempfile.mkdtemp() |
| 254 | try: |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 255 | self.do_create(dir=dir).write(b"blat") |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 256 | finally: |
| 257 | os.rmdir(dir) |
| 258 | |
| 259 | def test_file_mode(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 260 | # _mkstemp_inner creates files with the proper mode |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 261 | if not has_stat: |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 262 | return # ugh, can't use SkipTest. |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 263 | |
| 264 | file = self.do_create() |
| 265 | mode = stat.S_IMODE(os.stat(file.name).st_mode) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 266 | expected = 0o600 |
Ronald Oussoren | 94f2528 | 2010-05-05 19:11:21 +0000 | [diff] [blame] | 267 | if sys.platform in ('win32', 'os2emx'): |
Tim Peters | ca3ac7f | 2002-08-09 18:13:51 +0000 | [diff] [blame] | 268 | # There's no distinction among 'user', 'group' and 'world'; |
| 269 | # replicate the 'user' bits. |
| 270 | user = expected >> 6 |
| 271 | expected = user * (1 + 8 + 64) |
| 272 | self.assertEqual(mode, expected) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 273 | |
| 274 | def test_noinherit(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 275 | # _mkstemp_inner file handles are not inherited by child processes |
Guido van Rossum | 7874106 | 2002-08-17 11:41:01 +0000 | [diff] [blame] | 276 | if not has_spawnl: |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 277 | return # ugh, can't use SkipTest. |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 278 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 279 | if support.verbose: |
Guido van Rossum | 7874106 | 2002-08-17 11:41:01 +0000 | [diff] [blame] | 280 | v="v" |
| 281 | else: |
| 282 | v="q" |
| 283 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 284 | file = self.do_create() |
Guido van Rossum | 7874106 | 2002-08-17 11:41:01 +0000 | [diff] [blame] | 285 | fd = "%d" % file.fd |
| 286 | |
| 287 | try: |
| 288 | me = __file__ |
| 289 | except NameError: |
| 290 | me = sys.argv[0] |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 291 | |
| 292 | # We have to exec something, so that FD_CLOEXEC will take |
Guido van Rossum | 7874106 | 2002-08-17 11:41:01 +0000 | [diff] [blame] | 293 | # effect. The core of this test is therefore in |
| 294 | # tf_inherit_check.py, which see. |
| 295 | tester = os.path.join(os.path.dirname(os.path.abspath(me)), |
| 296 | "tf_inherit_check.py") |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 297 | |
Martin v. Löwis | d4210bc | 2003-10-23 15:55:28 +0000 | [diff] [blame] | 298 | # On Windows a spawn* /path/ with embedded spaces shouldn't be quoted, |
| 299 | # but an arg with embedded spaces should be decorated with double |
| 300 | # quotes on each end |
Christian Heimes | 895627f | 2007-12-08 17:28:33 +0000 | [diff] [blame] | 301 | if sys.platform in ('win32',): |
Martin v. Löwis | d4210bc | 2003-10-23 15:55:28 +0000 | [diff] [blame] | 302 | decorated = '"%s"' % sys.executable |
| 303 | tester = '"%s"' % tester |
| 304 | else: |
| 305 | decorated = sys.executable |
| 306 | |
| 307 | retval = os.spawnl(os.P_WAIT, sys.executable, decorated, tester, v, fd) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 308 | self.assertFalse(retval < 0, |
Guido van Rossum | 7874106 | 2002-08-17 11:41:01 +0000 | [diff] [blame] | 309 | "child process caught fatal signal %d" % -retval) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 310 | self.assertFalse(retval > 0, "child process reports failure %d"%retval) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 311 | |
| 312 | def test_textmode(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 313 | # _mkstemp_inner can create files in text mode |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 314 | if not has_textmode: |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 315 | return # ugh, can't use SkipTest. |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 316 | |
Amaury Forgeot d'Arc | 7d0bddd | 2009-11-30 00:08:56 +0000 | [diff] [blame] | 317 | # A text file is truncated at the first Ctrl+Z byte |
| 318 | f = self.do_create(bin=0) |
| 319 | f.write(b"blat\x1a") |
| 320 | f.write(b"extra\n") |
| 321 | os.lseek(f.fd, 0, os.SEEK_SET) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 322 | self.assertEqual(os.read(f.fd, 20), b"blat") |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 323 | |
| 324 | test_classes.append(test__mkstemp_inner) |
| 325 | |
| 326 | |
| 327 | class test_gettempprefix(TC): |
| 328 | """Test gettempprefix().""" |
| 329 | |
| 330 | def test_sane_template(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 331 | # gettempprefix returns a nonempty prefix string |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 332 | p = tempfile.gettempprefix() |
| 333 | |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 334 | self.assertIsInstance(p, str) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 335 | self.assertTrue(len(p) > 0) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 336 | |
| 337 | def test_usable_template(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 338 | # gettempprefix returns a usable prefix string |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 339 | |
| 340 | # Create a temp directory, avoiding use of the prefix. |
| 341 | # Then attempt to create a file whose name is |
| 342 | # prefix + 'xxxxxx.xxx' in that directory. |
| 343 | p = tempfile.gettempprefix() + "xxxxxx.xxx" |
| 344 | d = tempfile.mkdtemp(prefix="") |
| 345 | try: |
| 346 | p = os.path.join(d, p) |
| 347 | try: |
| 348 | fd = os.open(p, os.O_RDWR | os.O_CREAT) |
| 349 | except: |
| 350 | self.failOnException("os.open") |
| 351 | os.close(fd) |
| 352 | os.unlink(p) |
| 353 | finally: |
| 354 | os.rmdir(d) |
| 355 | |
| 356 | test_classes.append(test_gettempprefix) |
| 357 | |
| 358 | |
| 359 | class test_gettempdir(TC): |
| 360 | """Test gettempdir().""" |
| 361 | |
| 362 | def test_directory_exists(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 363 | # gettempdir returns a directory which exists |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 364 | |
| 365 | dir = tempfile.gettempdir() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 366 | self.assertTrue(os.path.isabs(dir) or dir == os.curdir, |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 367 | "%s is not an absolute path" % dir) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 368 | self.assertTrue(os.path.isdir(dir), |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 369 | "%s is not a directory" % dir) |
| 370 | |
| 371 | def test_directory_writable(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 372 | # gettempdir returns a directory writable by the user |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 373 | |
| 374 | # sneaky: just instantiate a NamedTemporaryFile, which |
| 375 | # defaults to writing into the directory returned by |
| 376 | # gettempdir. |
| 377 | try: |
| 378 | file = tempfile.NamedTemporaryFile() |
Guido van Rossum | ec42ffd | 2007-08-27 23:40:36 +0000 | [diff] [blame] | 379 | file.write(b"blat") |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 380 | file.close() |
| 381 | except: |
| 382 | self.failOnException("create file in %s" % tempfile.gettempdir()) |
| 383 | |
| 384 | def test_same_thing(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 385 | # gettempdir always returns the same object |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 386 | a = tempfile.gettempdir() |
| 387 | b = tempfile.gettempdir() |
| 388 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 389 | self.assertTrue(a is b) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 390 | |
| 391 | test_classes.append(test_gettempdir) |
| 392 | |
| 393 | |
| 394 | class test_mkstemp(TC): |
| 395 | """Test mkstemp().""" |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 396 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 397 | def do_create(self, dir=None, pre="", suf=""): |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 398 | if dir is None: |
| 399 | dir = tempfile.gettempdir() |
| 400 | try: |
| 401 | (fd, name) = tempfile.mkstemp(dir=dir, prefix=pre, suffix=suf) |
Martin v. Löwis | d662548 | 2003-10-12 17:37:01 +0000 | [diff] [blame] | 402 | (ndir, nbase) = os.path.split(name) |
| 403 | adir = os.path.abspath(dir) |
| 404 | self.assertEqual(adir, ndir, |
| 405 | "Directory '%s' incorrectly returned as '%s'" % (adir, ndir)) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 406 | except: |
| 407 | self.failOnException("mkstemp") |
| 408 | |
| 409 | try: |
| 410 | self.nameCheck(name, dir, pre, suf) |
| 411 | finally: |
| 412 | os.close(fd) |
| 413 | os.unlink(name) |
| 414 | |
| 415 | def test_basic(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 416 | # mkstemp can create files |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 417 | self.do_create() |
| 418 | self.do_create(pre="a") |
| 419 | self.do_create(suf="b") |
| 420 | self.do_create(pre="a", suf="b") |
| 421 | self.do_create(pre="aa", suf=".txt") |
Martin v. Löwis | d662548 | 2003-10-12 17:37:01 +0000 | [diff] [blame] | 422 | self.do_create(dir=".") |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 423 | |
| 424 | def test_choose_directory(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 425 | # mkstemp can create directories in a user-selected directory |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 426 | dir = tempfile.mkdtemp() |
| 427 | try: |
| 428 | self.do_create(dir=dir) |
| 429 | finally: |
| 430 | os.rmdir(dir) |
| 431 | |
| 432 | test_classes.append(test_mkstemp) |
| 433 | |
| 434 | |
| 435 | class test_mkdtemp(TC): |
| 436 | """Test mkdtemp().""" |
| 437 | |
| 438 | def do_create(self, dir=None, pre="", suf=""): |
| 439 | if dir is None: |
| 440 | dir = tempfile.gettempdir() |
| 441 | try: |
| 442 | name = tempfile.mkdtemp(dir=dir, prefix=pre, suffix=suf) |
| 443 | except: |
| 444 | self.failOnException("mkdtemp") |
| 445 | |
| 446 | try: |
| 447 | self.nameCheck(name, dir, pre, suf) |
| 448 | return name |
| 449 | except: |
| 450 | os.rmdir(name) |
| 451 | raise |
| 452 | |
| 453 | def test_basic(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 454 | # mkdtemp can create directories |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 455 | os.rmdir(self.do_create()) |
| 456 | os.rmdir(self.do_create(pre="a")) |
| 457 | os.rmdir(self.do_create(suf="b")) |
| 458 | os.rmdir(self.do_create(pre="a", suf="b")) |
| 459 | os.rmdir(self.do_create(pre="aa", suf=".txt")) |
Tim Peters | a0d55de | 2002-08-09 18:01:01 +0000 | [diff] [blame] | 460 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 461 | def test_basic_many(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 462 | # mkdtemp can create many directories (stochastic) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 463 | extant = list(range(TEST_FILES)) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 464 | try: |
| 465 | for i in extant: |
| 466 | extant[i] = self.do_create(pre="aa") |
| 467 | finally: |
| 468 | for i in extant: |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 469 | if(isinstance(i, str)): |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 470 | os.rmdir(i) |
| 471 | |
| 472 | def test_choose_directory(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 473 | # mkdtemp can create directories in a user-selected directory |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 474 | dir = tempfile.mkdtemp() |
| 475 | try: |
| 476 | os.rmdir(self.do_create(dir=dir)) |
| 477 | finally: |
| 478 | os.rmdir(dir) |
| 479 | |
| 480 | def test_mode(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 481 | # mkdtemp creates directories with the proper mode |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 482 | if not has_stat: |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 483 | return # ugh, can't use SkipTest. |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 484 | |
| 485 | dir = self.do_create() |
| 486 | try: |
| 487 | mode = stat.S_IMODE(os.stat(dir).st_mode) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 488 | mode &= 0o777 # Mask off sticky bits inherited from /tmp |
| 489 | expected = 0o700 |
Ronald Oussoren | 94f2528 | 2010-05-05 19:11:21 +0000 | [diff] [blame] | 490 | if sys.platform in ('win32', 'os2emx'): |
Tim Peters | ca3ac7f | 2002-08-09 18:13:51 +0000 | [diff] [blame] | 491 | # There's no distinction among 'user', 'group' and 'world'; |
| 492 | # replicate the 'user' bits. |
| 493 | user = expected >> 6 |
| 494 | expected = user * (1 + 8 + 64) |
| 495 | self.assertEqual(mode, expected) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 496 | finally: |
| 497 | os.rmdir(dir) |
| 498 | |
| 499 | test_classes.append(test_mkdtemp) |
| 500 | |
| 501 | |
| 502 | class test_mktemp(TC): |
| 503 | """Test mktemp().""" |
| 504 | |
| 505 | # For safety, all use of mktemp must occur in a private directory. |
| 506 | # We must also suppress the RuntimeWarning it generates. |
| 507 | def setUp(self): |
| 508 | self.dir = tempfile.mkdtemp() |
Brett Cannon | e1adece | 2010-03-20 22:19:55 +0000 | [diff] [blame] | 509 | super().setUp() |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 510 | |
| 511 | def tearDown(self): |
| 512 | if self.dir: |
| 513 | os.rmdir(self.dir) |
| 514 | self.dir = None |
Brett Cannon | e1adece | 2010-03-20 22:19:55 +0000 | [diff] [blame] | 515 | super().tearDown() |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 516 | |
| 517 | class mktemped: |
| 518 | _unlink = os.unlink |
| 519 | _bflags = tempfile._bin_openflags |
| 520 | |
| 521 | def __init__(self, dir, pre, suf): |
| 522 | self.name = tempfile.mktemp(dir=dir, prefix=pre, suffix=suf) |
| 523 | # Create the file. This will raise an exception if it's |
| 524 | # mysteriously appeared in the meanwhile. |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 525 | os.close(os.open(self.name, self._bflags, 0o600)) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 526 | |
| 527 | def __del__(self): |
| 528 | self._unlink(self.name) |
| 529 | |
| 530 | def do_create(self, pre="", suf=""): |
| 531 | try: |
| 532 | file = self.mktemped(self.dir, pre, suf) |
| 533 | except: |
| 534 | self.failOnException("mktemp") |
| 535 | |
| 536 | self.nameCheck(file.name, self.dir, pre, suf) |
| 537 | return file |
| 538 | |
| 539 | def test_basic(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 540 | # mktemp can choose usable file names |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 541 | self.do_create() |
| 542 | self.do_create(pre="a") |
| 543 | self.do_create(suf="b") |
| 544 | self.do_create(pre="a", suf="b") |
| 545 | self.do_create(pre="aa", suf=".txt") |
| 546 | |
| 547 | def test_many(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 548 | # mktemp can choose many usable file names (stochastic) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 549 | extant = list(range(TEST_FILES)) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 550 | for i in extant: |
| 551 | extant[i] = self.do_create(pre="aa") |
| 552 | |
Fred Drake | 8bec483 | 2002-11-22 20:13:43 +0000 | [diff] [blame] | 553 | ## def test_warning(self): |
| 554 | ## # mktemp issues a warning when used |
| 555 | ## warnings.filterwarnings("error", |
| 556 | ## category=RuntimeWarning, |
| 557 | ## message="mktemp") |
| 558 | ## self.assertRaises(RuntimeWarning, |
| 559 | ## tempfile.mktemp, dir=self.dir) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 560 | |
| 561 | test_classes.append(test_mktemp) |
| 562 | |
| 563 | |
| 564 | # We test _TemporaryFileWrapper by testing NamedTemporaryFile. |
| 565 | |
| 566 | |
| 567 | class test_NamedTemporaryFile(TC): |
| 568 | """Test NamedTemporaryFile().""" |
| 569 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 570 | def do_create(self, dir=None, pre="", suf="", delete=True): |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 571 | if dir is None: |
| 572 | dir = tempfile.gettempdir() |
| 573 | try: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 574 | file = tempfile.NamedTemporaryFile(dir=dir, prefix=pre, suffix=suf, |
| 575 | delete=delete) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 576 | except: |
| 577 | self.failOnException("NamedTemporaryFile") |
| 578 | |
| 579 | self.nameCheck(file.name, dir, pre, suf) |
| 580 | return file |
| 581 | |
| 582 | |
| 583 | def test_basic(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 584 | # NamedTemporaryFile can create files |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 585 | self.do_create() |
| 586 | self.do_create(pre="a") |
| 587 | self.do_create(suf="b") |
| 588 | self.do_create(pre="a", suf="b") |
| 589 | self.do_create(pre="aa", suf=".txt") |
| 590 | |
| 591 | def test_creates_named(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 592 | # NamedTemporaryFile creates files with names |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 593 | f = tempfile.NamedTemporaryFile() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 594 | self.assertTrue(os.path.exists(f.name), |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 595 | "NamedTemporaryFile %s does not exist" % f.name) |
| 596 | |
| 597 | def test_del_on_close(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 598 | # A NamedTemporaryFile is deleted when closed |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 599 | dir = tempfile.mkdtemp() |
| 600 | try: |
| 601 | f = tempfile.NamedTemporaryFile(dir=dir) |
Guido van Rossum | ec42ffd | 2007-08-27 23:40:36 +0000 | [diff] [blame] | 602 | f.write(b'blat') |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 603 | f.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 604 | self.assertFalse(os.path.exists(f.name), |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 605 | "NamedTemporaryFile %s exists after close" % f.name) |
| 606 | finally: |
| 607 | os.rmdir(dir) |
| 608 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 609 | def test_dis_del_on_close(self): |
| 610 | # Tests that delete-on-close can be disabled |
| 611 | dir = tempfile.mkdtemp() |
| 612 | tmp = None |
| 613 | try: |
| 614 | f = tempfile.NamedTemporaryFile(dir=dir, delete=False) |
| 615 | tmp = f.name |
Guido van Rossum | ec42ffd | 2007-08-27 23:40:36 +0000 | [diff] [blame] | 616 | f.write(b'blat') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 617 | f.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 618 | self.assertTrue(os.path.exists(f.name), |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 619 | "NamedTemporaryFile %s missing after close" % f.name) |
| 620 | finally: |
| 621 | if tmp is not None: |
| 622 | os.unlink(tmp) |
| 623 | os.rmdir(dir) |
| 624 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 625 | def test_multiple_close(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 626 | # A NamedTemporaryFile can be closed many times without error |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 627 | f = tempfile.NamedTemporaryFile() |
Guido van Rossum | ec42ffd | 2007-08-27 23:40:36 +0000 | [diff] [blame] | 628 | f.write(b'abc\n') |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 629 | f.close() |
| 630 | try: |
| 631 | f.close() |
| 632 | f.close() |
| 633 | except: |
| 634 | self.failOnException("close") |
| 635 | |
Christian Heimes | 3ecfea71 | 2008-02-09 20:51:34 +0000 | [diff] [blame] | 636 | def test_context_manager(self): |
| 637 | # A NamedTemporaryFile can be used as a context manager |
| 638 | with tempfile.NamedTemporaryFile() as f: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 639 | self.assertTrue(os.path.exists(f.name)) |
| 640 | self.assertFalse(os.path.exists(f.name)) |
Christian Heimes | 3ecfea71 | 2008-02-09 20:51:34 +0000 | [diff] [blame] | 641 | def use_closed(): |
| 642 | with f: |
| 643 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 644 | self.assertRaises(ValueError, use_closed) |
Christian Heimes | 3ecfea71 | 2008-02-09 20:51:34 +0000 | [diff] [blame] | 645 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 646 | # How to test the mode and bufsize parameters? |
| 647 | |
| 648 | test_classes.append(test_NamedTemporaryFile) |
| 649 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 650 | class test_SpooledTemporaryFile(TC): |
| 651 | """Test SpooledTemporaryFile().""" |
| 652 | |
| 653 | def do_create(self, max_size=0, dir=None, pre="", suf=""): |
| 654 | if dir is None: |
| 655 | dir = tempfile.gettempdir() |
| 656 | try: |
| 657 | file = tempfile.SpooledTemporaryFile(max_size=max_size, dir=dir, prefix=pre, suffix=suf) |
| 658 | except: |
| 659 | self.failOnException("SpooledTemporaryFile") |
| 660 | |
| 661 | return file |
| 662 | |
| 663 | |
| 664 | def test_basic(self): |
| 665 | # SpooledTemporaryFile can create files |
| 666 | f = self.do_create() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 667 | self.assertFalse(f._rolled) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 668 | f = self.do_create(max_size=100, pre="a", suf=".txt") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 669 | self.assertFalse(f._rolled) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 670 | |
| 671 | def test_del_on_close(self): |
| 672 | # A SpooledTemporaryFile is deleted when closed |
| 673 | dir = tempfile.mkdtemp() |
| 674 | try: |
| 675 | f = tempfile.SpooledTemporaryFile(max_size=10, dir=dir) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 676 | self.assertFalse(f._rolled) |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 677 | f.write(b'blat ' * 5) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 678 | self.assertTrue(f._rolled) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 679 | filename = f.name |
| 680 | f.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 681 | self.assertFalse(isinstance(filename, str) and os.path.exists(filename), |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 682 | "SpooledTemporaryFile %s exists after close" % filename) |
| 683 | finally: |
| 684 | os.rmdir(dir) |
| 685 | |
| 686 | def test_rewrite_small(self): |
| 687 | # A SpooledTemporaryFile can be written to multiple within the max_size |
| 688 | f = self.do_create(max_size=30) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 689 | self.assertFalse(f._rolled) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 690 | for i in range(5): |
| 691 | f.seek(0, 0) |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 692 | f.write(b'x' * 20) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 693 | self.assertFalse(f._rolled) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 694 | |
| 695 | def test_write_sequential(self): |
| 696 | # A SpooledTemporaryFile should hold exactly max_size bytes, and roll |
| 697 | # over afterward |
| 698 | f = self.do_create(max_size=30) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 699 | self.assertFalse(f._rolled) |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 700 | f.write(b'x' * 20) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 701 | self.assertFalse(f._rolled) |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 702 | f.write(b'x' * 10) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 703 | self.assertFalse(f._rolled) |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 704 | f.write(b'x') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 705 | self.assertTrue(f._rolled) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 706 | |
R David Murray | d89ee79 | 2011-03-14 09:55:46 -0400 | [diff] [blame] | 707 | def test_writelines(self): |
| 708 | # Verify writelines with a SpooledTemporaryFile |
| 709 | f = self.do_create() |
| 710 | f.writelines((b'x', b'y', b'z')) |
| 711 | f.seek(0) |
| 712 | buf = f.read() |
| 713 | self.assertEqual(buf, b'xyz') |
| 714 | |
| 715 | def test_writelines_sequential(self): |
| 716 | # A SpooledTemporaryFile should hold exactly max_size bytes, and roll |
| 717 | # over afterward |
| 718 | f = self.do_create(max_size=35) |
| 719 | f.writelines((b'x' * 20, b'x' * 10, b'x' * 5)) |
| 720 | self.assertFalse(f._rolled) |
| 721 | f.write(b'x') |
| 722 | self.assertTrue(f._rolled) |
| 723 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 724 | def test_sparse(self): |
| 725 | # A SpooledTemporaryFile that is written late in the file will extend |
| 726 | # when that occurs |
| 727 | f = self.do_create(max_size=30) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 728 | self.assertFalse(f._rolled) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 729 | f.seek(100, 0) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 730 | self.assertFalse(f._rolled) |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 731 | f.write(b'x') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 732 | self.assertTrue(f._rolled) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 733 | |
| 734 | def test_fileno(self): |
| 735 | # A SpooledTemporaryFile should roll over to a real file on fileno() |
| 736 | f = self.do_create(max_size=30) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 737 | self.assertFalse(f._rolled) |
| 738 | self.assertTrue(f.fileno() > 0) |
| 739 | self.assertTrue(f._rolled) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 740 | |
Christian Heimes | 3ecfea71 | 2008-02-09 20:51:34 +0000 | [diff] [blame] | 741 | def test_multiple_close_before_rollover(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 742 | # A SpooledTemporaryFile can be closed many times without error |
| 743 | f = tempfile.SpooledTemporaryFile() |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 744 | f.write(b'abc\n') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 745 | self.assertFalse(f._rolled) |
Christian Heimes | 3ecfea71 | 2008-02-09 20:51:34 +0000 | [diff] [blame] | 746 | f.close() |
| 747 | try: |
| 748 | f.close() |
| 749 | f.close() |
| 750 | except: |
| 751 | self.failOnException("close") |
| 752 | |
| 753 | def test_multiple_close_after_rollover(self): |
| 754 | # A SpooledTemporaryFile can be closed many times without error |
| 755 | f = tempfile.SpooledTemporaryFile(max_size=1) |
| 756 | f.write(b'abc\n') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 757 | self.assertTrue(f._rolled) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 758 | f.close() |
| 759 | try: |
| 760 | f.close() |
| 761 | f.close() |
| 762 | except: |
| 763 | self.failOnException("close") |
| 764 | |
| 765 | def test_bound_methods(self): |
| 766 | # It should be OK to steal a bound method from a SpooledTemporaryFile |
| 767 | # and use it independently; when the file rolls over, those bound |
| 768 | # methods should continue to function |
| 769 | f = self.do_create(max_size=30) |
| 770 | read = f.read |
| 771 | write = f.write |
| 772 | seek = f.seek |
| 773 | |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 774 | write(b"a" * 35) |
| 775 | write(b"b" * 35) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 776 | seek(0, 0) |
Guido van Rossum | 9a63470 | 2007-07-09 10:24:45 +0000 | [diff] [blame] | 777 | self.assertEqual(read(70), b'a'*35 + b'b'*35) |
| 778 | |
| 779 | def test_text_mode(self): |
| 780 | # Creating a SpooledTemporaryFile with a text mode should produce |
| 781 | # a file object reading and writing (Unicode) text strings. |
| 782 | f = tempfile.SpooledTemporaryFile(mode='w+', max_size=10) |
| 783 | f.write("abc\n") |
| 784 | f.seek(0) |
| 785 | self.assertEqual(f.read(), "abc\n") |
| 786 | f.write("def\n") |
| 787 | f.seek(0) |
| 788 | self.assertEqual(f.read(), "abc\ndef\n") |
| 789 | f.write("xyzzy\n") |
| 790 | f.seek(0) |
| 791 | self.assertEqual(f.read(), "abc\ndef\nxyzzy\n") |
Amaury Forgeot d'Arc | 7d0bddd | 2009-11-30 00:08:56 +0000 | [diff] [blame] | 792 | # Check that Ctrl+Z doesn't truncate the file |
| 793 | f.write("foo\x1abar\n") |
| 794 | f.seek(0) |
| 795 | self.assertEqual(f.read(), "abc\ndef\nxyzzy\nfoo\x1abar\n") |
Guido van Rossum | 9a63470 | 2007-07-09 10:24:45 +0000 | [diff] [blame] | 796 | |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 797 | def test_text_newline_and_encoding(self): |
| 798 | f = tempfile.SpooledTemporaryFile(mode='w+', max_size=10, |
| 799 | newline='', encoding='utf-8') |
| 800 | f.write("\u039B\r\n") |
| 801 | f.seek(0) |
| 802 | self.assertEqual(f.read(), "\u039B\r\n") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 803 | self.assertFalse(f._rolled) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 804 | |
| 805 | f.write("\u039B" * 20 + "\r\n") |
| 806 | f.seek(0) |
| 807 | self.assertEqual(f.read(), "\u039B\r\n" + ("\u039B" * 20) + "\r\n") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 808 | self.assertTrue(f._rolled) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 809 | |
Christian Heimes | 3ecfea71 | 2008-02-09 20:51:34 +0000 | [diff] [blame] | 810 | def test_context_manager_before_rollover(self): |
| 811 | # A SpooledTemporaryFile can be used as a context manager |
| 812 | with tempfile.SpooledTemporaryFile(max_size=1) as f: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 813 | self.assertFalse(f._rolled) |
| 814 | self.assertFalse(f.closed) |
| 815 | self.assertTrue(f.closed) |
Christian Heimes | 3ecfea71 | 2008-02-09 20:51:34 +0000 | [diff] [blame] | 816 | def use_closed(): |
| 817 | with f: |
| 818 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 819 | self.assertRaises(ValueError, use_closed) |
Christian Heimes | 3ecfea71 | 2008-02-09 20:51:34 +0000 | [diff] [blame] | 820 | |
| 821 | def test_context_manager_during_rollover(self): |
| 822 | # A SpooledTemporaryFile can be used as a context manager |
| 823 | with tempfile.SpooledTemporaryFile(max_size=1) as f: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 824 | self.assertFalse(f._rolled) |
Christian Heimes | 3ecfea71 | 2008-02-09 20:51:34 +0000 | [diff] [blame] | 825 | f.write(b'abc\n') |
| 826 | f.flush() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 827 | self.assertTrue(f._rolled) |
| 828 | self.assertFalse(f.closed) |
| 829 | self.assertTrue(f.closed) |
Christian Heimes | 3ecfea71 | 2008-02-09 20:51:34 +0000 | [diff] [blame] | 830 | def use_closed(): |
| 831 | with f: |
| 832 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 833 | self.assertRaises(ValueError, use_closed) |
Christian Heimes | 3ecfea71 | 2008-02-09 20:51:34 +0000 | [diff] [blame] | 834 | |
| 835 | def test_context_manager_after_rollover(self): |
| 836 | # A SpooledTemporaryFile can be used as a context manager |
| 837 | f = tempfile.SpooledTemporaryFile(max_size=1) |
| 838 | f.write(b'abc\n') |
| 839 | f.flush() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 840 | self.assertTrue(f._rolled) |
Christian Heimes | 3ecfea71 | 2008-02-09 20:51:34 +0000 | [diff] [blame] | 841 | with f: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 842 | self.assertFalse(f.closed) |
| 843 | self.assertTrue(f.closed) |
Christian Heimes | 3ecfea71 | 2008-02-09 20:51:34 +0000 | [diff] [blame] | 844 | def use_closed(): |
| 845 | with f: |
| 846 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 847 | self.assertRaises(ValueError, use_closed) |
Christian Heimes | 3ecfea71 | 2008-02-09 20:51:34 +0000 | [diff] [blame] | 848 | |
| 849 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 850 | test_classes.append(test_SpooledTemporaryFile) |
| 851 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 852 | |
| 853 | class test_TemporaryFile(TC): |
| 854 | """Test TemporaryFile().""" |
| 855 | |
| 856 | def test_basic(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 857 | # TemporaryFile can create files |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 858 | # No point in testing the name params - the file has no name. |
| 859 | try: |
| 860 | tempfile.TemporaryFile() |
| 861 | except: |
| 862 | self.failOnException("TemporaryFile") |
| 863 | |
| 864 | def test_has_no_name(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 865 | # TemporaryFile creates files with no names (on this system) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 866 | dir = tempfile.mkdtemp() |
| 867 | f = tempfile.TemporaryFile(dir=dir) |
Guido van Rossum | ec42ffd | 2007-08-27 23:40:36 +0000 | [diff] [blame] | 868 | f.write(b'blat') |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 869 | |
| 870 | # Sneaky: because this file has no name, it should not prevent |
| 871 | # us from removing the directory it was created in. |
| 872 | try: |
| 873 | os.rmdir(dir) |
| 874 | except: |
| 875 | ei = sys.exc_info() |
| 876 | # cleanup |
| 877 | f.close() |
| 878 | os.rmdir(dir) |
| 879 | self.failOnException("rmdir", ei) |
| 880 | |
| 881 | def test_multiple_close(self): |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 882 | # A TemporaryFile can be closed many times without error |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 883 | f = tempfile.TemporaryFile() |
Guido van Rossum | ec42ffd | 2007-08-27 23:40:36 +0000 | [diff] [blame] | 884 | f.write(b'abc\n') |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 885 | f.close() |
| 886 | try: |
| 887 | f.close() |
| 888 | f.close() |
| 889 | except: |
| 890 | self.failOnException("close") |
| 891 | |
| 892 | # How to test the mode and bufsize parameters? |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 893 | def test_mode_and_encoding(self): |
| 894 | |
| 895 | def roundtrip(input, *args, **kwargs): |
| 896 | with tempfile.TemporaryFile(*args, **kwargs) as fileobj: |
| 897 | fileobj.write(input) |
| 898 | fileobj.seek(0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 899 | self.assertEqual(input, fileobj.read()) |
Guido van Rossum | f0c7416 | 2007-08-28 03:29:45 +0000 | [diff] [blame] | 900 | |
| 901 | roundtrip(b"1234", "w+b") |
| 902 | roundtrip("abdc\n", "w+") |
| 903 | roundtrip("\u039B", "w+", encoding="utf-16") |
| 904 | roundtrip("foo\r\n", "w+", newline="") |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 905 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 906 | |
Guido van Rossum | 8ccd9b6 | 2002-08-22 20:02:03 +0000 | [diff] [blame] | 907 | if tempfile.NamedTemporaryFile is not tempfile.TemporaryFile: |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 908 | test_classes.append(test_TemporaryFile) |
| 909 | |
Nick Coghlan | 543af75 | 2010-10-24 11:23:25 +0000 | [diff] [blame] | 910 | |
| 911 | # Helper for test_del_on_shutdown |
| 912 | class NulledModules: |
| 913 | def __init__(self, *modules): |
| 914 | self.refs = [mod.__dict__ for mod in modules] |
| 915 | self.contents = [ref.copy() for ref in self.refs] |
| 916 | |
| 917 | def __enter__(self): |
| 918 | for d in self.refs: |
| 919 | for key in d: |
| 920 | d[key] = None |
| 921 | |
| 922 | def __exit__(self, *exc_info): |
| 923 | for d, c in zip(self.refs, self.contents): |
| 924 | d.clear() |
| 925 | d.update(c) |
| 926 | |
| 927 | class test_TemporaryDirectory(TC): |
| 928 | """Test TemporaryDirectory().""" |
| 929 | |
| 930 | def do_create(self, dir=None, pre="", suf="", recurse=1): |
| 931 | if dir is None: |
| 932 | dir = tempfile.gettempdir() |
| 933 | try: |
| 934 | tmp = tempfile.TemporaryDirectory(dir=dir, prefix=pre, suffix=suf) |
| 935 | except: |
| 936 | self.failOnException("TemporaryDirectory") |
| 937 | self.nameCheck(tmp.name, dir, pre, suf) |
| 938 | # Create a subdirectory and some files |
| 939 | if recurse: |
| 940 | self.do_create(tmp.name, pre, suf, recurse-1) |
| 941 | with open(os.path.join(tmp.name, "test.txt"), "wb") as f: |
| 942 | f.write(b"Hello world!") |
| 943 | return tmp |
| 944 | |
Nick Coghlan | 6b22f3f | 2010-12-12 15:24:21 +0000 | [diff] [blame] | 945 | def test_mkdtemp_failure(self): |
| 946 | # Check no additional exception if mkdtemp fails |
| 947 | # Previously would raise AttributeError instead |
Nick Coghlan | 3c54ea6 | 2010-12-13 03:02:43 +0000 | [diff] [blame] | 948 | # (noted as part of Issue #10188) |
| 949 | with tempfile.TemporaryDirectory() as nonexistent: |
| 950 | pass |
| 951 | with self.assertRaises(os.error): |
| 952 | tempfile.TemporaryDirectory(dir=nonexistent) |
Nick Coghlan | 6b22f3f | 2010-12-12 15:24:21 +0000 | [diff] [blame] | 953 | |
Nick Coghlan | 543af75 | 2010-10-24 11:23:25 +0000 | [diff] [blame] | 954 | def test_explicit_cleanup(self): |
| 955 | # A TemporaryDirectory is deleted when cleaned up |
| 956 | dir = tempfile.mkdtemp() |
| 957 | try: |
| 958 | d = self.do_create(dir=dir) |
| 959 | self.assertTrue(os.path.exists(d.name), |
| 960 | "TemporaryDirectory %s does not exist" % d.name) |
| 961 | d.cleanup() |
| 962 | self.assertFalse(os.path.exists(d.name), |
| 963 | "TemporaryDirectory %s exists after cleanup" % d.name) |
| 964 | finally: |
| 965 | os.rmdir(dir) |
| 966 | |
| 967 | @support.cpython_only |
| 968 | def test_del_on_collection(self): |
| 969 | # A TemporaryDirectory is deleted when garbage collected |
| 970 | dir = tempfile.mkdtemp() |
| 971 | try: |
| 972 | d = self.do_create(dir=dir) |
| 973 | name = d.name |
| 974 | del d # Rely on refcounting to invoke __del__ |
| 975 | self.assertFalse(os.path.exists(name), |
| 976 | "TemporaryDirectory %s exists after __del__" % name) |
| 977 | finally: |
| 978 | os.rmdir(dir) |
| 979 | |
| 980 | @unittest.expectedFailure # See issue #10188 |
| 981 | def test_del_on_shutdown(self): |
| 982 | # A TemporaryDirectory may be cleaned up during shutdown |
| 983 | # Make sure it works with the relevant modules nulled out |
Nick Coghlan | 6b22f3f | 2010-12-12 15:24:21 +0000 | [diff] [blame] | 984 | with self.do_create() as dir: |
Nick Coghlan | 543af75 | 2010-10-24 11:23:25 +0000 | [diff] [blame] | 985 | d = self.do_create(dir=dir) |
| 986 | # Mimic the nulling out of modules that |
| 987 | # occurs during system shutdown |
| 988 | modules = [os, os.path] |
| 989 | if has_stat: |
| 990 | modules.append(stat) |
Nick Coghlan | 6b22f3f | 2010-12-12 15:24:21 +0000 | [diff] [blame] | 991 | # Currently broken, so suppress the warning |
| 992 | # that is otherwise emitted on stdout |
| 993 | with support.captured_stderr() as err: |
| 994 | with NulledModules(*modules): |
| 995 | d.cleanup() |
| 996 | # Currently broken, so stop spurious exception by |
| 997 | # indicating the object has already been closed |
| 998 | d._closed = True |
| 999 | # And this assert will fail, as expected by the |
| 1000 | # unittest decorator... |
Nick Coghlan | 543af75 | 2010-10-24 11:23:25 +0000 | [diff] [blame] | 1001 | self.assertFalse(os.path.exists(d.name), |
| 1002 | "TemporaryDirectory %s exists after cleanup" % d.name) |
Nick Coghlan | 6b22f3f | 2010-12-12 15:24:21 +0000 | [diff] [blame] | 1003 | |
| 1004 | def test_warnings_on_cleanup(self): |
| 1005 | # Two kinds of warning on shutdown |
| 1006 | # Issue 10888: may write to stderr if modules are nulled out |
| 1007 | # ResourceWarning will be triggered by __del__ |
| 1008 | with self.do_create() as dir: |
Nick Coghlan | e98e8a3 | 2010-12-13 16:32:51 +0000 | [diff] [blame] | 1009 | if os.sep != '\\': |
| 1010 | # Embed a backslash in order to make sure string escaping |
| 1011 | # in the displayed error message is dealt with correctly |
| 1012 | suffix = '\\check_backslash_handling' |
| 1013 | else: |
| 1014 | suffix = '' |
| 1015 | d = self.do_create(dir=dir, suf=suffix) |
Nick Coghlan | 6b22f3f | 2010-12-12 15:24:21 +0000 | [diff] [blame] | 1016 | |
| 1017 | #Check for the Issue 10888 message |
| 1018 | modules = [os, os.path] |
| 1019 | if has_stat: |
| 1020 | modules.append(stat) |
| 1021 | with support.captured_stderr() as err: |
| 1022 | with NulledModules(*modules): |
| 1023 | d.cleanup() |
Nick Coghlan | e98e8a3 | 2010-12-13 16:32:51 +0000 | [diff] [blame] | 1024 | message = err.getvalue().replace('\\\\', '\\') |
Nick Coghlan | 6b22f3f | 2010-12-12 15:24:21 +0000 | [diff] [blame] | 1025 | self.assertIn("while cleaning up", message) |
| 1026 | self.assertIn(d.name, message) |
| 1027 | |
| 1028 | # Check for the resource warning |
| 1029 | with support.check_warnings(('Implicitly', ResourceWarning), quiet=False): |
| 1030 | warnings.filterwarnings("always", category=ResourceWarning) |
| 1031 | d.__del__() |
| 1032 | self.assertFalse(os.path.exists(d.name), |
| 1033 | "TemporaryDirectory %s exists after __del__" % d.name) |
Nick Coghlan | 543af75 | 2010-10-24 11:23:25 +0000 | [diff] [blame] | 1034 | |
| 1035 | def test_multiple_close(self): |
| 1036 | # Can be cleaned-up many times without error |
| 1037 | d = self.do_create() |
| 1038 | d.cleanup() |
| 1039 | try: |
| 1040 | d.cleanup() |
| 1041 | d.cleanup() |
| 1042 | except: |
| 1043 | self.failOnException("cleanup") |
| 1044 | |
| 1045 | def test_context_manager(self): |
| 1046 | # Can be used as a context manager |
| 1047 | d = self.do_create() |
| 1048 | with d as name: |
| 1049 | self.assertTrue(os.path.exists(name)) |
| 1050 | self.assertEqual(name, d.name) |
| 1051 | self.assertFalse(os.path.exists(name)) |
| 1052 | |
| 1053 | |
| 1054 | test_classes.append(test_TemporaryDirectory) |
| 1055 | |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 1056 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1057 | support.run_unittest(*test_classes) |
Guido van Rossum | 0e54871 | 2002-08-09 16:14:33 +0000 | [diff] [blame] | 1058 | |
| 1059 | if __name__ == "__main__": |
| 1060 | test_main() |