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