Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 1 | # As a test suite for the os module, this is woefully inadequate, but this |
| 2 | # does add tests for a few functions which have been determined to be more |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 3 | # portable than they had been thought to be. |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 4 | |
| 5 | import os |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 6 | import errno |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 7 | import unittest |
Jeremy Hylton | a7fc21b | 2001-08-20 20:10:01 +0000 | [diff] [blame] | 8 | import warnings |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9 | import sys |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame^] | 10 | import signal |
| 11 | import subprocess |
| 12 | import time |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 13 | import shutil |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 14 | from test import support |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 15 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame^] | 16 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 17 | # Tests creating TESTFN |
| 18 | class FileTests(unittest.TestCase): |
| 19 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 20 | if os.path.exists(support.TESTFN): |
| 21 | os.unlink(support.TESTFN) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 22 | tearDown = setUp |
| 23 | |
| 24 | def test_access(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 25 | f = os.open(support.TESTFN, os.O_CREAT|os.O_RDWR) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 26 | os.close(f) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 27 | self.assertTrue(os.access(support.TESTFN, os.W_OK)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 28 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 29 | def test_closerange(self): |
Antoine Pitrou | b9ee06c | 2008-08-16 22:03:17 +0000 | [diff] [blame] | 30 | first = os.open(support.TESTFN, os.O_CREAT|os.O_RDWR) |
| 31 | # We must allocate two consecutive file descriptors, otherwise |
| 32 | # it will mess up other file descriptors (perhaps even the three |
| 33 | # standard ones). |
| 34 | second = os.dup(first) |
| 35 | try: |
| 36 | retries = 0 |
| 37 | while second != first + 1: |
| 38 | os.close(first) |
| 39 | retries += 1 |
| 40 | if retries > 10: |
| 41 | # XXX test skipped |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 42 | self.skipTest("couldn't allocate two consecutive fds") |
Antoine Pitrou | b9ee06c | 2008-08-16 22:03:17 +0000 | [diff] [blame] | 43 | first, second = second, os.dup(second) |
| 44 | finally: |
| 45 | os.close(second) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 46 | # close a fd that is open, and one that isn't |
Antoine Pitrou | b9ee06c | 2008-08-16 22:03:17 +0000 | [diff] [blame] | 47 | os.closerange(first, first + 2) |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 48 | self.assertRaises(OSError, os.write, first, b"a") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 49 | |
Hirokazu Yamamoto | 4c19e6e | 2008-09-08 23:41:21 +0000 | [diff] [blame] | 50 | def test_rename(self): |
| 51 | path = support.TESTFN |
| 52 | old = sys.getrefcount(path) |
| 53 | self.assertRaises(TypeError, os.rename, path, 0) |
| 54 | new = sys.getrefcount(path) |
| 55 | self.assertEqual(old, new) |
| 56 | |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 57 | def test_read(self): |
| 58 | with open(support.TESTFN, "w+b") as fobj: |
| 59 | fobj.write(b"spam") |
| 60 | fobj.flush() |
| 61 | fd = fobj.fileno() |
| 62 | os.lseek(fd, 0, 0) |
| 63 | s = os.read(fd, 4) |
| 64 | self.assertEqual(type(s), bytes) |
| 65 | self.assertEqual(s, b"spam") |
| 66 | |
| 67 | def test_write(self): |
| 68 | # os.write() accepts bytes- and buffer-like objects but not strings |
| 69 | fd = os.open(support.TESTFN, os.O_CREAT | os.O_WRONLY) |
| 70 | self.assertRaises(TypeError, os.write, fd, "beans") |
| 71 | os.write(fd, b"bacon\n") |
| 72 | os.write(fd, bytearray(b"eggs\n")) |
| 73 | os.write(fd, memoryview(b"spam\n")) |
| 74 | os.close(fd) |
| 75 | with open(support.TESTFN, "rb") as fobj: |
Antoine Pitrou | d62269f | 2008-09-15 23:54:52 +0000 | [diff] [blame] | 76 | self.assertEqual(fobj.read().splitlines(), |
| 77 | [b"bacon", b"eggs", b"spam"]) |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 78 | |
| 79 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 80 | class TemporaryFileTests(unittest.TestCase): |
| 81 | def setUp(self): |
| 82 | self.files = [] |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 83 | os.mkdir(support.TESTFN) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 84 | |
| 85 | def tearDown(self): |
| 86 | for name in self.files: |
| 87 | os.unlink(name) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 88 | os.rmdir(support.TESTFN) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 89 | |
| 90 | def check_tempfile(self, name): |
| 91 | # make sure it doesn't already exist: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 92 | self.assertFalse(os.path.exists(name), |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 93 | "file already exists for temporary file") |
| 94 | # make sure we can create the file |
| 95 | open(name, "w") |
| 96 | self.files.append(name) |
| 97 | |
| 98 | def test_tempnam(self): |
| 99 | if not hasattr(os, "tempnam"): |
| 100 | return |
| 101 | warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, |
| 102 | r"test_os$") |
| 103 | self.check_tempfile(os.tempnam()) |
| 104 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 105 | name = os.tempnam(support.TESTFN) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 106 | self.check_tempfile(name) |
| 107 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 108 | name = os.tempnam(support.TESTFN, "pfx") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 109 | self.assertTrue(os.path.basename(name)[:3] == "pfx") |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 110 | self.check_tempfile(name) |
| 111 | |
| 112 | def test_tmpfile(self): |
| 113 | if not hasattr(os, "tmpfile"): |
| 114 | return |
| 115 | # As with test_tmpnam() below, the Windows implementation of tmpfile() |
| 116 | # attempts to create a file in the root directory of the current drive. |
| 117 | # On Vista and Server 2008, this test will always fail for normal users |
| 118 | # as writing to the root directory requires elevated privileges. With |
| 119 | # XP and below, the semantics of tmpfile() are the same, but the user |
| 120 | # running the test is more likely to have administrative privileges on |
| 121 | # their account already. If that's the case, then os.tmpfile() should |
| 122 | # work. In order to make this test as useful as possible, rather than |
| 123 | # trying to detect Windows versions or whether or not the user has the |
| 124 | # right permissions, just try and create a file in the root directory |
| 125 | # and see if it raises a 'Permission denied' OSError. If it does, then |
| 126 | # test that a subsequent call to os.tmpfile() raises the same error. If |
| 127 | # it doesn't, assume we're on XP or below and the user running the test |
| 128 | # has administrative privileges, and proceed with the test as normal. |
| 129 | if sys.platform == 'win32': |
| 130 | name = '\\python_test_os_test_tmpfile.txt' |
| 131 | if os.path.exists(name): |
| 132 | os.remove(name) |
| 133 | try: |
| 134 | fp = open(name, 'w') |
| 135 | except IOError as first: |
| 136 | # open() failed, assert tmpfile() fails in the same way. |
| 137 | # Although open() raises an IOError and os.tmpfile() raises an |
| 138 | # OSError(), 'args' will be (13, 'Permission denied') in both |
| 139 | # cases. |
| 140 | try: |
| 141 | fp = os.tmpfile() |
| 142 | except OSError as second: |
| 143 | self.assertEqual(first.args, second.args) |
| 144 | else: |
| 145 | self.fail("expected os.tmpfile() to raise OSError") |
| 146 | return |
| 147 | else: |
| 148 | # open() worked, therefore, tmpfile() should work. Close our |
| 149 | # dummy file and proceed with the test as normal. |
| 150 | fp.close() |
| 151 | os.remove(name) |
| 152 | |
| 153 | fp = os.tmpfile() |
| 154 | fp.write("foobar") |
| 155 | fp.seek(0,0) |
| 156 | s = fp.read() |
| 157 | fp.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 158 | self.assertTrue(s == "foobar") |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 159 | |
| 160 | def test_tmpnam(self): |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 161 | if not hasattr(os, "tmpnam"): |
| 162 | return |
| 163 | warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, |
| 164 | r"test_os$") |
| 165 | name = os.tmpnam() |
| 166 | if sys.platform in ("win32",): |
| 167 | # The Windows tmpnam() seems useless. From the MS docs: |
| 168 | # |
| 169 | # The character string that tmpnam creates consists of |
| 170 | # the path prefix, defined by the entry P_tmpdir in the |
| 171 | # file STDIO.H, followed by a sequence consisting of the |
| 172 | # digit characters '0' through '9'; the numerical value |
| 173 | # of this string is in the range 1 - 65,535. Changing the |
| 174 | # definitions of L_tmpnam or P_tmpdir in STDIO.H does not |
| 175 | # change the operation of tmpnam. |
| 176 | # |
| 177 | # The really bizarre part is that, at least under MSVC6, |
| 178 | # P_tmpdir is "\\". That is, the path returned refers to |
| 179 | # the root of the current drive. That's a terrible place to |
| 180 | # put temp files, and, depending on privileges, the user |
| 181 | # may not even be able to open a file in the root directory. |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 182 | self.assertFalse(os.path.exists(name), |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 183 | "file already exists for temporary file") |
| 184 | else: |
| 185 | self.check_tempfile(name) |
| 186 | |
Amaury Forgeot d'Arc | e2e36ba | 2008-08-01 00:14:22 +0000 | [diff] [blame] | 187 | def fdopen_helper(self, *args): |
| 188 | fd = os.open(support.TESTFN, os.O_RDONLY) |
| 189 | fp2 = os.fdopen(fd, *args) |
| 190 | fp2.close() |
| 191 | |
| 192 | def test_fdopen(self): |
| 193 | self.fdopen_helper() |
| 194 | self.fdopen_helper('r') |
| 195 | self.fdopen_helper('r', 100) |
| 196 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 197 | # Test attributes on return values from os.*stat* family. |
| 198 | class StatAttributeTests(unittest.TestCase): |
| 199 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 200 | os.mkdir(support.TESTFN) |
| 201 | self.fname = os.path.join(support.TESTFN, "f1") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 202 | f = open(self.fname, 'wb') |
Guido van Rossum | 26d95c3 | 2007-08-27 23:18:54 +0000 | [diff] [blame] | 203 | f.write(b"ABC") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 204 | f.close() |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 206 | def tearDown(self): |
| 207 | os.unlink(self.fname) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 208 | os.rmdir(support.TESTFN) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 209 | |
| 210 | def test_stat_attributes(self): |
| 211 | if not hasattr(os, "stat"): |
| 212 | return |
| 213 | |
| 214 | import stat |
| 215 | result = os.stat(self.fname) |
| 216 | |
| 217 | # Make sure direct access works |
| 218 | self.assertEquals(result[stat.ST_SIZE], 3) |
| 219 | self.assertEquals(result.st_size, 3) |
| 220 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 221 | # Make sure all the attributes are there |
| 222 | members = dir(result) |
| 223 | for name in dir(stat): |
| 224 | if name[:3] == 'ST_': |
| 225 | attr = name.lower() |
Martin v. Löwis | 4d394df | 2005-01-23 09:19:22 +0000 | [diff] [blame] | 226 | if name.endswith("TIME"): |
| 227 | def trunc(x): return int(x) |
| 228 | else: |
| 229 | def trunc(x): return x |
| 230 | self.assertEquals(trunc(getattr(result, attr)), |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 231 | result[getattr(stat, name)]) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 232 | self.assertIn(attr, members) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 233 | |
| 234 | try: |
| 235 | result[200] |
| 236 | self.fail("No exception thrown") |
| 237 | except IndexError: |
| 238 | pass |
| 239 | |
| 240 | # Make sure that assignment fails |
| 241 | try: |
| 242 | result.st_mode = 1 |
| 243 | self.fail("No exception thrown") |
Collin Winter | 42dae6a | 2007-03-28 21:44:53 +0000 | [diff] [blame] | 244 | except AttributeError: |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 245 | pass |
| 246 | |
| 247 | try: |
| 248 | result.st_rdev = 1 |
| 249 | self.fail("No exception thrown") |
Guido van Rossum | 1fff878 | 2001-10-18 21:19:31 +0000 | [diff] [blame] | 250 | except (AttributeError, TypeError): |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 251 | pass |
| 252 | |
| 253 | try: |
| 254 | result.parrot = 1 |
| 255 | self.fail("No exception thrown") |
| 256 | except AttributeError: |
| 257 | pass |
| 258 | |
| 259 | # Use the stat_result constructor with a too-short tuple. |
| 260 | try: |
| 261 | result2 = os.stat_result((10,)) |
| 262 | self.fail("No exception thrown") |
| 263 | except TypeError: |
| 264 | pass |
| 265 | |
| 266 | # Use the constructr with a too-long tuple. |
| 267 | try: |
| 268 | result2 = os.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)) |
| 269 | except TypeError: |
| 270 | pass |
| 271 | |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 272 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 273 | def test_statvfs_attributes(self): |
| 274 | if not hasattr(os, "statvfs"): |
| 275 | return |
| 276 | |
Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 277 | try: |
| 278 | result = os.statvfs(self.fname) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 279 | except OSError as e: |
Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 280 | # On AtheOS, glibc always returns ENOSYS |
Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 281 | if e.errno == errno.ENOSYS: |
| 282 | return |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 283 | |
| 284 | # Make sure direct access works |
Brett Cannon | cfaf10c | 2008-05-16 00:45:35 +0000 | [diff] [blame] | 285 | self.assertEquals(result.f_bfree, result[3]) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 286 | |
Brett Cannon | cfaf10c | 2008-05-16 00:45:35 +0000 | [diff] [blame] | 287 | # Make sure all the attributes are there. |
| 288 | members = ('bsize', 'frsize', 'blocks', 'bfree', 'bavail', 'files', |
| 289 | 'ffree', 'favail', 'flag', 'namemax') |
| 290 | for value, member in enumerate(members): |
| 291 | self.assertEquals(getattr(result, 'f_' + member), result[value]) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 292 | |
| 293 | # Make sure that assignment really fails |
| 294 | try: |
| 295 | result.f_bfree = 1 |
| 296 | self.fail("No exception thrown") |
Collin Winter | 42dae6a | 2007-03-28 21:44:53 +0000 | [diff] [blame] | 297 | except AttributeError: |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 298 | pass |
| 299 | |
| 300 | try: |
| 301 | result.parrot = 1 |
| 302 | self.fail("No exception thrown") |
| 303 | except AttributeError: |
| 304 | pass |
| 305 | |
| 306 | # Use the constructor with a too-short tuple. |
| 307 | try: |
| 308 | result2 = os.statvfs_result((10,)) |
| 309 | self.fail("No exception thrown") |
| 310 | except TypeError: |
| 311 | pass |
| 312 | |
| 313 | # Use the constructr with a too-long tuple. |
| 314 | try: |
| 315 | result2 = os.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)) |
| 316 | except TypeError: |
| 317 | pass |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 318 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 319 | def test_utime_dir(self): |
| 320 | delta = 1000000 |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 321 | st = os.stat(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 322 | # round to int, because some systems may support sub-second |
| 323 | # time stamps in stat, but not in utime. |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 324 | os.utime(support.TESTFN, (st.st_atime, int(st.st_mtime-delta))) |
| 325 | st2 = os.stat(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 326 | self.assertEquals(st2.st_mtime, int(st.st_mtime-delta)) |
| 327 | |
| 328 | # Restrict test to Win32, since there is no guarantee other |
| 329 | # systems support centiseconds |
| 330 | if sys.platform == 'win32': |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 331 | def get_file_system(path): |
Hirokazu Yamamoto | 5ef6d18 | 2008-08-20 04:17:24 +0000 | [diff] [blame] | 332 | root = os.path.splitdrive(os.path.abspath(path))[0] + '\\' |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 333 | import ctypes |
Hirokazu Yamamoto | ca765d5 | 2008-08-20 16:18:19 +0000 | [diff] [blame] | 334 | kernel32 = ctypes.windll.kernel32 |
Hirokazu Yamamoto | 5ef6d18 | 2008-08-20 04:17:24 +0000 | [diff] [blame] | 335 | buf = ctypes.create_unicode_buffer("", 100) |
Hirokazu Yamamoto | ca765d5 | 2008-08-20 16:18:19 +0000 | [diff] [blame] | 336 | if kernel32.GetVolumeInformationW(root, None, 0, None, None, None, buf, len(buf)): |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 337 | return buf.value |
| 338 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 339 | if get_file_system(support.TESTFN) == "NTFS": |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 340 | def test_1565150(self): |
| 341 | t1 = 1159195039.25 |
| 342 | os.utime(self.fname, (t1, t1)) |
| 343 | self.assertEquals(os.stat(self.fname).st_mtime, t1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 344 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 345 | def test_1686475(self): |
| 346 | # Verify that an open file can be stat'ed |
| 347 | try: |
| 348 | os.stat(r"c:\pagefile.sys") |
| 349 | except WindowsError as e: |
Benjamin Peterson | c4fe6f3 | 2008-08-19 18:57:56 +0000 | [diff] [blame] | 350 | if e.errno == 2: # file does not exist; cannot run test |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 351 | return |
| 352 | self.fail("Could not stat pagefile.sys") |
| 353 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 354 | from test import mapping_tests |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 355 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 356 | class EnvironTests(mapping_tests.BasicTestMappingProtocol): |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 357 | """check that os.environ object conform to mapping protocol""" |
Walter Dörwald | 118f931 | 2004-06-02 18:42:25 +0000 | [diff] [blame] | 358 | type2test = None |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 359 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 360 | def setUp(self): |
| 361 | self.__save = dict(os.environ) |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 362 | for key, value in self._reference().items(): |
| 363 | os.environ[key] = value |
| 364 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 365 | def tearDown(self): |
| 366 | os.environ.clear() |
| 367 | os.environ.update(self.__save) |
| 368 | |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 369 | def _reference(self): |
| 370 | return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"} |
| 371 | |
| 372 | def _empty_mapping(self): |
| 373 | os.environ.clear() |
| 374 | return os.environ |
| 375 | |
Martin v. Löwis | 1d11de6 | 2005-01-29 13:29:23 +0000 | [diff] [blame] | 376 | # Bug 1110478 |
Martin v. Löwis | 5510f65 | 2005-02-17 21:23:20 +0000 | [diff] [blame] | 377 | def test_update2(self): |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 378 | os.environ.clear() |
Martin v. Löwis | 1d11de6 | 2005-01-29 13:29:23 +0000 | [diff] [blame] | 379 | if os.path.exists("/bin/sh"): |
| 380 | os.environ.update(HELLO="World") |
| 381 | value = os.popen("/bin/sh -c 'echo $HELLO'").read().strip() |
| 382 | self.assertEquals(value, "World") |
| 383 | |
Christian Heimes | 1a13d59 | 2007-11-08 14:16:55 +0000 | [diff] [blame] | 384 | def test_os_popen_iter(self): |
| 385 | if os.path.exists("/bin/sh"): |
| 386 | popen = os.popen("/bin/sh -c 'echo \"line1\nline2\nline3\"'") |
| 387 | it = iter(popen) |
| 388 | self.assertEquals(next(it), "line1\n") |
| 389 | self.assertEquals(next(it), "line2\n") |
| 390 | self.assertEquals(next(it), "line3\n") |
| 391 | self.assertRaises(StopIteration, next, it) |
| 392 | |
Guido van Rossum | 67aca9e | 2007-06-13 21:51:27 +0000 | [diff] [blame] | 393 | # Verify environ keys and values from the OS are of the |
| 394 | # correct str type. |
| 395 | def test_keyvalue_types(self): |
| 396 | for key, val in os.environ.items(): |
| 397 | self.assertEquals(type(key), str) |
| 398 | self.assertEquals(type(val), str) |
| 399 | |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 400 | def test_items(self): |
| 401 | for key, value in self._reference().items(): |
| 402 | self.assertEqual(os.environ.get(key), value) |
| 403 | |
Ezio Melotti | 19e4acf | 2010-02-22 15:59:01 +0000 | [diff] [blame] | 404 | # Issue 7310 |
| 405 | def test___repr__(self): |
| 406 | """Check that the repr() of os.environ looks like environ({...}).""" |
| 407 | env = os.environ |
| 408 | self.assertTrue(isinstance(env.data, dict)) |
| 409 | self.assertEqual(repr(env), 'environ({!r})'.format(env.data)) |
| 410 | |
Gregory P. Smith | b6e8c7e | 2010-02-27 07:22:22 +0000 | [diff] [blame] | 411 | def test_get_exec_path(self): |
| 412 | defpath_list = os.defpath.split(os.pathsep) |
| 413 | test_path = ['/monty', '/python', '', '/flying/circus'] |
| 414 | test_env = {'PATH': os.pathsep.join(test_path)} |
| 415 | |
| 416 | saved_environ = os.environ |
| 417 | try: |
| 418 | os.environ = dict(test_env) |
| 419 | # Test that defaulting to os.environ works. |
| 420 | self.assertSequenceEqual(test_path, os.get_exec_path()) |
| 421 | self.assertSequenceEqual(test_path, os.get_exec_path(env=None)) |
| 422 | finally: |
| 423 | os.environ = saved_environ |
| 424 | |
| 425 | # No PATH environment variable |
| 426 | self.assertSequenceEqual(defpath_list, os.get_exec_path({})) |
| 427 | # Empty PATH environment variable |
| 428 | self.assertSequenceEqual(('',), os.get_exec_path({'PATH':''})) |
| 429 | # Supplied PATH environment variable |
| 430 | self.assertSequenceEqual(test_path, os.get_exec_path(test_env)) |
| 431 | |
Ezio Melotti | 19e4acf | 2010-02-22 15:59:01 +0000 | [diff] [blame] | 432 | |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 433 | class WalkTests(unittest.TestCase): |
| 434 | """Tests for os.walk().""" |
| 435 | |
| 436 | def test_traversal(self): |
| 437 | import os |
| 438 | from os.path import join |
| 439 | |
| 440 | # Build: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 441 | # TESTFN/ |
| 442 | # TEST1/ a file kid and two directory kids |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 443 | # tmp1 |
| 444 | # SUB1/ a file kid and a directory kid |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 445 | # tmp2 |
| 446 | # SUB11/ no kids |
| 447 | # SUB2/ a file kid and a dirsymlink kid |
| 448 | # tmp3 |
| 449 | # link/ a symlink to TESTFN.2 |
| 450 | # TEST2/ |
| 451 | # tmp4 a lone file |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 452 | walk_path = join(support.TESTFN, "TEST1") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 453 | sub1_path = join(walk_path, "SUB1") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 454 | sub11_path = join(sub1_path, "SUB11") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 455 | sub2_path = join(walk_path, "SUB2") |
| 456 | tmp1_path = join(walk_path, "tmp1") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 457 | tmp2_path = join(sub1_path, "tmp2") |
| 458 | tmp3_path = join(sub2_path, "tmp3") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 459 | link_path = join(sub2_path, "link") |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 460 | t2_path = join(support.TESTFN, "TEST2") |
| 461 | tmp4_path = join(support.TESTFN, "TEST2", "tmp4") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 462 | |
| 463 | # Create stuff. |
| 464 | os.makedirs(sub11_path) |
| 465 | os.makedirs(sub2_path) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 466 | os.makedirs(t2_path) |
| 467 | for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path: |
Alex Martelli | 01c77c6 | 2006-08-24 02:58:11 +0000 | [diff] [blame] | 468 | f = open(path, "w") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 469 | f.write("I'm " + path + " and proud of it. Blame test_os.\n") |
| 470 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 471 | if hasattr(os, "symlink"): |
| 472 | os.symlink(os.path.abspath(t2_path), link_path) |
| 473 | sub2_tree = (sub2_path, ["link"], ["tmp3"]) |
| 474 | else: |
| 475 | sub2_tree = (sub2_path, [], ["tmp3"]) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 476 | |
| 477 | # Walk top-down. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 478 | all = list(os.walk(walk_path)) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 479 | self.assertEqual(len(all), 4) |
| 480 | # We can't know which order SUB1 and SUB2 will appear in. |
| 481 | # Not flipped: TESTFN, SUB1, SUB11, SUB2 |
| 482 | # flipped: TESTFN, SUB2, SUB1, SUB11 |
| 483 | flipped = all[0][1][0] != "SUB1" |
| 484 | all[0][1].sort() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 485 | self.assertEqual(all[0], (walk_path, ["SUB1", "SUB2"], ["tmp1"])) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 486 | self.assertEqual(all[1 + flipped], (sub1_path, ["SUB11"], ["tmp2"])) |
| 487 | self.assertEqual(all[2 + flipped], (sub11_path, [], [])) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 488 | self.assertEqual(all[3 - 2 * flipped], sub2_tree) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 489 | |
| 490 | # Prune the search. |
| 491 | all = [] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 492 | for root, dirs, files in os.walk(walk_path): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 493 | all.append((root, dirs, files)) |
| 494 | # Don't descend into SUB1. |
| 495 | if 'SUB1' in dirs: |
| 496 | # Note that this also mutates the dirs we appended to all! |
| 497 | dirs.remove('SUB1') |
| 498 | self.assertEqual(len(all), 2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 499 | self.assertEqual(all[0], (walk_path, ["SUB2"], ["tmp1"])) |
| 500 | self.assertEqual(all[1], sub2_tree) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 501 | |
| 502 | # Walk bottom-up. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 503 | all = list(os.walk(walk_path, topdown=False)) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 504 | self.assertEqual(len(all), 4) |
| 505 | # We can't know which order SUB1 and SUB2 will appear in. |
| 506 | # Not flipped: SUB11, SUB1, SUB2, TESTFN |
| 507 | # flipped: SUB2, SUB11, SUB1, TESTFN |
| 508 | flipped = all[3][1][0] != "SUB1" |
| 509 | all[3][1].sort() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 510 | self.assertEqual(all[3], (walk_path, ["SUB1", "SUB2"], ["tmp1"])) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 511 | self.assertEqual(all[flipped], (sub11_path, [], [])) |
| 512 | self.assertEqual(all[flipped + 1], (sub1_path, ["SUB11"], ["tmp2"])) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 513 | self.assertEqual(all[2 - 2 * flipped], sub2_tree) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 514 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 515 | if hasattr(os, "symlink"): |
| 516 | # Walk, following symlinks. |
| 517 | for root, dirs, files in os.walk(walk_path, followlinks=True): |
| 518 | if root == link_path: |
| 519 | self.assertEqual(dirs, []) |
| 520 | self.assertEqual(files, ["tmp4"]) |
| 521 | break |
| 522 | else: |
| 523 | self.fail("Didn't follow symlink with followlinks=True") |
| 524 | |
| 525 | def tearDown(self): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 526 | # Tear everything down. This is a decent use for bottom-up on |
| 527 | # Windows, which doesn't have a recursive delete command. The |
| 528 | # (not so) subtlety is that rmdir will fail unless the dir's |
| 529 | # kids are removed first, so bottom up is essential. |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 530 | for root, dirs, files in os.walk(support.TESTFN, topdown=False): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 531 | for name in files: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 532 | os.remove(os.path.join(root, name)) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 533 | for name in dirs: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 534 | dirname = os.path.join(root, name) |
| 535 | if not os.path.islink(dirname): |
| 536 | os.rmdir(dirname) |
| 537 | else: |
| 538 | os.remove(dirname) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 539 | os.rmdir(support.TESTFN) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 540 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 541 | class MakedirTests(unittest.TestCase): |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 542 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 543 | os.mkdir(support.TESTFN) |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 544 | |
| 545 | def test_makedir(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 546 | base = support.TESTFN |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 547 | path = os.path.join(base, 'dir1', 'dir2', 'dir3') |
| 548 | os.makedirs(path) # Should work |
| 549 | path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4') |
| 550 | os.makedirs(path) |
| 551 | |
| 552 | # Try paths with a '.' in them |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 553 | self.assertRaises(OSError, os.makedirs, os.curdir) |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 554 | path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4', 'dir5', os.curdir) |
| 555 | os.makedirs(path) |
| 556 | path = os.path.join(base, 'dir1', os.curdir, 'dir2', 'dir3', 'dir4', |
| 557 | 'dir5', 'dir6') |
| 558 | os.makedirs(path) |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 559 | |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 560 | def tearDown(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 561 | path = os.path.join(support.TESTFN, 'dir1', 'dir2', 'dir3', |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 562 | 'dir4', 'dir5', 'dir6') |
| 563 | # If the tests failed, the bottom-most directory ('../dir6') |
| 564 | # may not have been created, so we look for the outermost directory |
| 565 | # that exists. |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 566 | while not os.path.exists(path) and path != support.TESTFN: |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 567 | path = os.path.dirname(path) |
| 568 | |
| 569 | os.removedirs(path) |
| 570 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 571 | class DevNullTests(unittest.TestCase): |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 572 | def test_devnull(self): |
Alex Martelli | 01c77c6 | 2006-08-24 02:58:11 +0000 | [diff] [blame] | 573 | f = open(os.devnull, 'w') |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 574 | f.write('hello') |
| 575 | f.close() |
Alex Martelli | 01c77c6 | 2006-08-24 02:58:11 +0000 | [diff] [blame] | 576 | f = open(os.devnull, 'r') |
Tim Peters | 4182cfd | 2004-06-08 20:34:34 +0000 | [diff] [blame] | 577 | self.assertEqual(f.read(), '') |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 578 | f.close() |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 579 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 580 | class URandomTests(unittest.TestCase): |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 581 | def test_urandom(self): |
| 582 | try: |
| 583 | self.assertEqual(len(os.urandom(1)), 1) |
| 584 | self.assertEqual(len(os.urandom(10)), 10) |
| 585 | self.assertEqual(len(os.urandom(100)), 100) |
| 586 | self.assertEqual(len(os.urandom(1000)), 1000) |
| 587 | except NotImplementedError: |
| 588 | pass |
| 589 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 590 | class ExecTests(unittest.TestCase): |
| 591 | def test_execvpe_with_bad_program(self): |
Thomas Heller | bd315c5 | 2007-08-30 17:57:21 +0000 | [diff] [blame] | 592 | self.assertRaises(OSError, os.execvpe, 'no such app-', ['no such app-'], None) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 593 | |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 594 | def test_execvpe_with_bad_arglist(self): |
| 595 | self.assertRaises(ValueError, os.execvpe, 'notepad', [], None) |
| 596 | |
Antoine Pitrou | 1119a64 | 2010-01-17 12:16:23 +0000 | [diff] [blame] | 597 | class ArgTests(unittest.TestCase): |
| 598 | def test_bytearray(self): |
| 599 | # Issue #7561: posix module didn't release bytearray exports properly. |
| 600 | b = bytearray(os.sep.encode('ascii')) |
| 601 | self.assertRaises(OSError, os.mkdir, b) |
| 602 | # Check object is still resizable. |
| 603 | b[:] = b'' |
| 604 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 605 | class Win32ErrorTests(unittest.TestCase): |
| 606 | def test_rename(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 607 | self.assertRaises(WindowsError, os.rename, support.TESTFN, support.TESTFN+".bak") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 608 | |
| 609 | def test_remove(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 610 | self.assertRaises(WindowsError, os.remove, support.TESTFN) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 611 | |
| 612 | def test_chdir(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 613 | self.assertRaises(WindowsError, os.chdir, support.TESTFN) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 614 | |
| 615 | def test_mkdir(self): |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 616 | f = open(support.TESTFN, "w") |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 617 | try: |
| 618 | self.assertRaises(WindowsError, os.mkdir, support.TESTFN) |
| 619 | finally: |
| 620 | f.close() |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 621 | os.unlink(support.TESTFN) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 622 | |
| 623 | def test_utime(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 624 | self.assertRaises(WindowsError, os.utime, support.TESTFN, None) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 625 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 626 | def test_chmod(self): |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 627 | self.assertRaises(WindowsError, os.chmod, support.TESTFN, 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 628 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 629 | class TestInvalidFD(unittest.TestCase): |
Benjamin Peterson | 05e782f | 2009-01-19 15:15:02 +0000 | [diff] [blame] | 630 | singles = ["fchdir", "dup", "fdopen", "fdatasync", "fstat", |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 631 | "fstatvfs", "fsync", "tcgetpgrp", "ttyname"] |
| 632 | #singles.append("close") |
| 633 | #We omit close because it doesn'r raise an exception on some platforms |
| 634 | def get_single(f): |
| 635 | def helper(self): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 636 | if hasattr(os, f): |
| 637 | self.check(getattr(os, f)) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 638 | return helper |
| 639 | for f in singles: |
| 640 | locals()["test_"+f] = get_single(f) |
| 641 | |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 642 | def check(self, f, *args): |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 643 | try: |
| 644 | f(support.make_bad_fd(), *args) |
| 645 | except OSError as e: |
| 646 | self.assertEqual(e.errno, errno.EBADF) |
| 647 | else: |
| 648 | self.fail("%r didn't raise a OSError with a bad file descriptor" |
| 649 | % f) |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 650 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 651 | def test_isatty(self): |
| 652 | if hasattr(os, "isatty"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 653 | self.assertEqual(os.isatty(support.make_bad_fd()), False) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 654 | |
| 655 | def test_closerange(self): |
| 656 | if hasattr(os, "closerange"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 657 | fd = support.make_bad_fd() |
R. David Murray | 630cc48 | 2009-07-22 15:20:27 +0000 | [diff] [blame] | 658 | # Make sure none of the descriptors we are about to close are |
| 659 | # currently valid (issue 6542). |
| 660 | for i in range(10): |
| 661 | try: os.fstat(fd+i) |
| 662 | except OSError: |
| 663 | pass |
| 664 | else: |
| 665 | break |
| 666 | if i < 2: |
| 667 | raise unittest.SkipTest( |
| 668 | "Unable to acquire a range of invalid file descriptors") |
| 669 | self.assertEqual(os.closerange(fd, fd + i-1), None) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 670 | |
| 671 | def test_dup2(self): |
| 672 | if hasattr(os, "dup2"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 673 | self.check(os.dup2, 20) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 674 | |
| 675 | def test_fchmod(self): |
| 676 | if hasattr(os, "fchmod"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 677 | self.check(os.fchmod, 0) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 678 | |
| 679 | def test_fchown(self): |
| 680 | if hasattr(os, "fchown"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 681 | self.check(os.fchown, -1, -1) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 682 | |
| 683 | def test_fpathconf(self): |
| 684 | if hasattr(os, "fpathconf"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 685 | self.check(os.fpathconf, "PC_NAME_MAX") |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 686 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 687 | def test_ftruncate(self): |
| 688 | if hasattr(os, "ftruncate"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 689 | self.check(os.ftruncate, 0) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 690 | |
| 691 | def test_lseek(self): |
| 692 | if hasattr(os, "lseek"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 693 | self.check(os.lseek, 0, 0) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 694 | |
| 695 | def test_read(self): |
| 696 | if hasattr(os, "read"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 697 | self.check(os.read, 1) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 698 | |
| 699 | def test_tcsetpgrpt(self): |
| 700 | if hasattr(os, "tcsetpgrp"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 701 | self.check(os.tcsetpgrp, 0) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 702 | |
| 703 | def test_write(self): |
| 704 | if hasattr(os, "write"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 705 | self.check(os.write, b" ") |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 706 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 707 | if sys.platform != 'win32': |
| 708 | class Win32ErrorTests(unittest.TestCase): |
| 709 | pass |
| 710 | |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 711 | class PosixUidGidTests(unittest.TestCase): |
| 712 | if hasattr(os, 'setuid'): |
| 713 | def test_setuid(self): |
| 714 | if os.getuid() != 0: |
| 715 | self.assertRaises(os.error, os.setuid, 0) |
| 716 | self.assertRaises(OverflowError, os.setuid, 1<<32) |
| 717 | |
| 718 | if hasattr(os, 'setgid'): |
| 719 | def test_setgid(self): |
| 720 | if os.getuid() != 0: |
| 721 | self.assertRaises(os.error, os.setgid, 0) |
| 722 | self.assertRaises(OverflowError, os.setgid, 1<<32) |
| 723 | |
| 724 | if hasattr(os, 'seteuid'): |
| 725 | def test_seteuid(self): |
| 726 | if os.getuid() != 0: |
| 727 | self.assertRaises(os.error, os.seteuid, 0) |
| 728 | self.assertRaises(OverflowError, os.seteuid, 1<<32) |
| 729 | |
| 730 | if hasattr(os, 'setegid'): |
| 731 | def test_setegid(self): |
| 732 | if os.getuid() != 0: |
| 733 | self.assertRaises(os.error, os.setegid, 0) |
| 734 | self.assertRaises(OverflowError, os.setegid, 1<<32) |
| 735 | |
| 736 | if hasattr(os, 'setreuid'): |
| 737 | def test_setreuid(self): |
| 738 | if os.getuid() != 0: |
| 739 | self.assertRaises(os.error, os.setreuid, 0, 0) |
| 740 | self.assertRaises(OverflowError, os.setreuid, 1<<32, 0) |
| 741 | self.assertRaises(OverflowError, os.setreuid, 0, 1<<32) |
Benjamin Peterson | ebe87ba | 2010-03-06 20:34:24 +0000 | [diff] [blame] | 742 | |
| 743 | def test_setreuid_neg1(self): |
| 744 | # Needs to accept -1. We run this in a subprocess to avoid |
| 745 | # altering the test runner's process state (issue8045). |
Benjamin Peterson | ebe87ba | 2010-03-06 20:34:24 +0000 | [diff] [blame] | 746 | subprocess.check_call([ |
| 747 | sys.executable, '-c', |
| 748 | 'import os,sys;os.setreuid(-1,-1);sys.exit(0)']) |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 749 | |
| 750 | if hasattr(os, 'setregid'): |
| 751 | def test_setregid(self): |
| 752 | if os.getuid() != 0: |
| 753 | self.assertRaises(os.error, os.setregid, 0, 0) |
| 754 | self.assertRaises(OverflowError, os.setregid, 1<<32, 0) |
| 755 | self.assertRaises(OverflowError, os.setregid, 0, 1<<32) |
Benjamin Peterson | ebe87ba | 2010-03-06 20:34:24 +0000 | [diff] [blame] | 756 | |
| 757 | def test_setregid_neg1(self): |
| 758 | # Needs to accept -1. We run this in a subprocess to avoid |
| 759 | # altering the test runner's process state (issue8045). |
Benjamin Peterson | ebe87ba | 2010-03-06 20:34:24 +0000 | [diff] [blame] | 760 | subprocess.check_call([ |
| 761 | sys.executable, '-c', |
| 762 | 'import os,sys;os.setregid(-1,-1);sys.exit(0)']) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 763 | |
Mark Dickinson | 7061368 | 2009-05-05 21:34:59 +0000 | [diff] [blame] | 764 | @unittest.skipIf(sys.platform == 'darwin', "tests don't apply to OS X") |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 765 | class Pep383Tests(unittest.TestCase): |
| 766 | filenames = [b'foo\xf6bar', 'foo\xf6bar'.encode("utf-8")] |
| 767 | |
| 768 | def setUp(self): |
| 769 | self.fsencoding = sys.getfilesystemencoding() |
| 770 | sys.setfilesystemencoding("utf-8") |
| 771 | self.dir = support.TESTFN |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 772 | self.bdir = self.dir.encode("utf-8", "surrogateescape") |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 773 | os.mkdir(self.dir) |
| 774 | self.unicodefn = [] |
| 775 | for fn in self.filenames: |
| 776 | f = open(os.path.join(self.bdir, fn), "w") |
| 777 | f.close() |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 778 | self.unicodefn.append(fn.decode("utf-8", "surrogateescape")) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 779 | |
| 780 | def tearDown(self): |
| 781 | shutil.rmtree(self.dir) |
| 782 | sys.setfilesystemencoding(self.fsencoding) |
| 783 | |
| 784 | def test_listdir(self): |
| 785 | expected = set(self.unicodefn) |
| 786 | found = set(os.listdir(support.TESTFN)) |
| 787 | self.assertEquals(found, expected) |
| 788 | |
| 789 | def test_open(self): |
| 790 | for fn in self.unicodefn: |
| 791 | f = open(os.path.join(self.dir, fn)) |
| 792 | f.close() |
| 793 | |
| 794 | def test_stat(self): |
| 795 | for fn in self.unicodefn: |
| 796 | os.stat(os.path.join(self.dir, fn)) |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 797 | else: |
| 798 | class PosixUidGidTests(unittest.TestCase): |
| 799 | pass |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 800 | class Pep383Tests(unittest.TestCase): |
| 801 | pass |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 802 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame^] | 803 | @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests") |
| 804 | class Win32KillTests(unittest.TestCase): |
| 805 | def _kill(self, sig, *args): |
| 806 | # Send a subprocess a signal (or in some cases, just an int to be |
| 807 | # the return value) |
| 808 | proc = subprocess.Popen(*args) |
| 809 | os.kill(proc.pid, sig) |
| 810 | self.assertEqual(proc.wait(), sig) |
| 811 | |
| 812 | def test_kill_sigterm(self): |
| 813 | # SIGTERM doesn't mean anything special, but make sure it works |
| 814 | self._kill(signal.SIGTERM, [sys.executable]) |
| 815 | |
| 816 | def test_kill_int(self): |
| 817 | # os.kill on Windows can take an int which gets set as the exit code |
| 818 | self._kill(100, [sys.executable]) |
| 819 | |
| 820 | def _kill_with_event(self, event, name): |
| 821 | # Run a script which has console control handling enabled. |
| 822 | proc = subprocess.Popen([sys.executable, |
| 823 | os.path.join(os.path.dirname(__file__), |
| 824 | "win_console_handler.py")], |
| 825 | creationflags=subprocess.CREATE_NEW_PROCESS_GROUP) |
| 826 | # Let the interpreter startup before we send signals. See #3137. |
| 827 | time.sleep(0.5) |
| 828 | os.kill(proc.pid, event) |
| 829 | # proc.send_signal(event) could also be done here. |
| 830 | # Allow time for the signal to be passed and the process to exit. |
| 831 | time.sleep(0.5) |
| 832 | if not proc.poll(): |
| 833 | # Forcefully kill the process if we weren't able to signal it. |
| 834 | os.kill(proc.pid, signal.SIGINT) |
| 835 | self.fail("subprocess did not stop on {}".format(name)) |
| 836 | |
| 837 | @unittest.skip("subprocesses aren't inheriting CTRL+C property") |
| 838 | def test_CTRL_C_EVENT(self): |
| 839 | from ctypes import wintypes |
| 840 | import ctypes |
| 841 | |
| 842 | # Make a NULL value by creating a pointer with no argument. |
| 843 | NULL = ctypes.POINTER(ctypes.c_int)() |
| 844 | SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler |
| 845 | SetConsoleCtrlHandler.argtypes = (ctypes.POINTER(ctypes.c_int), |
| 846 | wintypes.BOOL) |
| 847 | SetConsoleCtrlHandler.restype = wintypes.BOOL |
| 848 | |
| 849 | # Calling this with NULL and FALSE causes the calling process to |
| 850 | # handle CTRL+C, rather than ignore it. This property is inherited |
| 851 | # by subprocesses. |
| 852 | SetConsoleCtrlHandler(NULL, 0) |
| 853 | |
| 854 | self._kill_with_event(signal.CTRL_C_EVENT, "CTRL_C_EVENT") |
| 855 | |
| 856 | def test_CTRL_BREAK_EVENT(self): |
| 857 | self._kill_with_event(signal.CTRL_BREAK_EVENT, "CTRL_BREAK_EVENT") |
| 858 | |
| 859 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 860 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 861 | support.run_unittest( |
Antoine Pitrou | 1119a64 | 2010-01-17 12:16:23 +0000 | [diff] [blame] | 862 | ArgTests, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 863 | FileTests, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 864 | StatAttributeTests, |
| 865 | EnvironTests, |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 866 | WalkTests, |
| 867 | MakedirTests, |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 868 | DevNullTests, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 869 | URandomTests, |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 870 | ExecTests, |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 871 | Win32ErrorTests, |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 872 | TestInvalidFD, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 873 | PosixUidGidTests, |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame^] | 874 | Pep383Tests, |
| 875 | Win32KillTests |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 876 | ) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 877 | |
| 878 | if __name__ == "__main__": |
| 879 | test_main() |