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 | |
Mark Dickinson | 7cf0389 | 2010-04-16 13:45:35 +0000 | [diff] [blame] | 16 | # Detect whether we're on a Linux system that uses the (now outdated |
| 17 | # and unmaintained) linuxthreads threading library. There's an issue |
| 18 | # when combining linuxthreads with a failed execv call: see |
| 19 | # http://bugs.python.org/issue4970. |
Mark Dickinson | 89589c9 | 2010-04-16 13:51:27 +0000 | [diff] [blame] | 20 | if (hasattr(os, "confstr_names") and |
| 21 | "CS_GNU_LIBPTHREAD_VERSION" in os.confstr_names): |
Mark Dickinson | 7cf0389 | 2010-04-16 13:45:35 +0000 | [diff] [blame] | 22 | libpthread = os.confstr("CS_GNU_LIBPTHREAD_VERSION") |
| 23 | USING_LINUXTHREADS= libpthread.startswith("linuxthreads") |
| 24 | else: |
| 25 | USING_LINUXTHREADS= False |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 26 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 27 | # Tests creating TESTFN |
| 28 | class FileTests(unittest.TestCase): |
| 29 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 30 | if os.path.exists(support.TESTFN): |
| 31 | os.unlink(support.TESTFN) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 32 | tearDown = setUp |
| 33 | |
| 34 | def test_access(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 35 | f = os.open(support.TESTFN, os.O_CREAT|os.O_RDWR) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 36 | os.close(f) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 37 | self.assertTrue(os.access(support.TESTFN, os.W_OK)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 38 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 39 | def test_closerange(self): |
Antoine Pitrou | b9ee06c | 2008-08-16 22:03:17 +0000 | [diff] [blame] | 40 | first = os.open(support.TESTFN, os.O_CREAT|os.O_RDWR) |
| 41 | # We must allocate two consecutive file descriptors, otherwise |
| 42 | # it will mess up other file descriptors (perhaps even the three |
| 43 | # standard ones). |
| 44 | second = os.dup(first) |
| 45 | try: |
| 46 | retries = 0 |
| 47 | while second != first + 1: |
| 48 | os.close(first) |
| 49 | retries += 1 |
| 50 | if retries > 10: |
| 51 | # XXX test skipped |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 52 | self.skipTest("couldn't allocate two consecutive fds") |
Antoine Pitrou | b9ee06c | 2008-08-16 22:03:17 +0000 | [diff] [blame] | 53 | first, second = second, os.dup(second) |
| 54 | finally: |
| 55 | os.close(second) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 56 | # close a fd that is open, and one that isn't |
Antoine Pitrou | b9ee06c | 2008-08-16 22:03:17 +0000 | [diff] [blame] | 57 | os.closerange(first, first + 2) |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 58 | self.assertRaises(OSError, os.write, first, b"a") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 59 | |
Hirokazu Yamamoto | 4c19e6e | 2008-09-08 23:41:21 +0000 | [diff] [blame] | 60 | def test_rename(self): |
| 61 | path = support.TESTFN |
| 62 | old = sys.getrefcount(path) |
| 63 | self.assertRaises(TypeError, os.rename, path, 0) |
| 64 | new = sys.getrefcount(path) |
| 65 | self.assertEqual(old, new) |
| 66 | |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 67 | def test_read(self): |
| 68 | with open(support.TESTFN, "w+b") as fobj: |
| 69 | fobj.write(b"spam") |
| 70 | fobj.flush() |
| 71 | fd = fobj.fileno() |
| 72 | os.lseek(fd, 0, 0) |
| 73 | s = os.read(fd, 4) |
| 74 | self.assertEqual(type(s), bytes) |
| 75 | self.assertEqual(s, b"spam") |
| 76 | |
| 77 | def test_write(self): |
| 78 | # os.write() accepts bytes- and buffer-like objects but not strings |
| 79 | fd = os.open(support.TESTFN, os.O_CREAT | os.O_WRONLY) |
| 80 | self.assertRaises(TypeError, os.write, fd, "beans") |
| 81 | os.write(fd, b"bacon\n") |
| 82 | os.write(fd, bytearray(b"eggs\n")) |
| 83 | os.write(fd, memoryview(b"spam\n")) |
| 84 | os.close(fd) |
| 85 | with open(support.TESTFN, "rb") as fobj: |
Antoine Pitrou | d62269f | 2008-09-15 23:54:52 +0000 | [diff] [blame] | 86 | self.assertEqual(fobj.read().splitlines(), |
| 87 | [b"bacon", b"eggs", b"spam"]) |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 88 | |
| 89 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 90 | class TemporaryFileTests(unittest.TestCase): |
| 91 | def setUp(self): |
| 92 | self.files = [] |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 93 | os.mkdir(support.TESTFN) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 94 | |
| 95 | def tearDown(self): |
| 96 | for name in self.files: |
| 97 | os.unlink(name) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 98 | os.rmdir(support.TESTFN) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 99 | |
| 100 | def check_tempfile(self, name): |
| 101 | # make sure it doesn't already exist: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 102 | self.assertFalse(os.path.exists(name), |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 103 | "file already exists for temporary file") |
| 104 | # make sure we can create the file |
| 105 | open(name, "w") |
| 106 | self.files.append(name) |
| 107 | |
| 108 | def test_tempnam(self): |
| 109 | if not hasattr(os, "tempnam"): |
| 110 | return |
| 111 | warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, |
| 112 | r"test_os$") |
| 113 | self.check_tempfile(os.tempnam()) |
| 114 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 115 | name = os.tempnam(support.TESTFN) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 116 | self.check_tempfile(name) |
| 117 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 118 | name = os.tempnam(support.TESTFN, "pfx") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 119 | self.assertTrue(os.path.basename(name)[:3] == "pfx") |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 120 | self.check_tempfile(name) |
| 121 | |
| 122 | def test_tmpfile(self): |
| 123 | if not hasattr(os, "tmpfile"): |
| 124 | return |
| 125 | # As with test_tmpnam() below, the Windows implementation of tmpfile() |
| 126 | # attempts to create a file in the root directory of the current drive. |
| 127 | # On Vista and Server 2008, this test will always fail for normal users |
| 128 | # as writing to the root directory requires elevated privileges. With |
| 129 | # XP and below, the semantics of tmpfile() are the same, but the user |
| 130 | # running the test is more likely to have administrative privileges on |
| 131 | # their account already. If that's the case, then os.tmpfile() should |
| 132 | # work. In order to make this test as useful as possible, rather than |
| 133 | # trying to detect Windows versions or whether or not the user has the |
| 134 | # right permissions, just try and create a file in the root directory |
| 135 | # and see if it raises a 'Permission denied' OSError. If it does, then |
| 136 | # test that a subsequent call to os.tmpfile() raises the same error. If |
| 137 | # it doesn't, assume we're on XP or below and the user running the test |
| 138 | # has administrative privileges, and proceed with the test as normal. |
| 139 | if sys.platform == 'win32': |
| 140 | name = '\\python_test_os_test_tmpfile.txt' |
| 141 | if os.path.exists(name): |
| 142 | os.remove(name) |
| 143 | try: |
| 144 | fp = open(name, 'w') |
| 145 | except IOError as first: |
| 146 | # open() failed, assert tmpfile() fails in the same way. |
| 147 | # Although open() raises an IOError and os.tmpfile() raises an |
| 148 | # OSError(), 'args' will be (13, 'Permission denied') in both |
| 149 | # cases. |
| 150 | try: |
| 151 | fp = os.tmpfile() |
| 152 | except OSError as second: |
| 153 | self.assertEqual(first.args, second.args) |
| 154 | else: |
| 155 | self.fail("expected os.tmpfile() to raise OSError") |
| 156 | return |
| 157 | else: |
| 158 | # open() worked, therefore, tmpfile() should work. Close our |
| 159 | # dummy file and proceed with the test as normal. |
| 160 | fp.close() |
| 161 | os.remove(name) |
| 162 | |
| 163 | fp = os.tmpfile() |
| 164 | fp.write("foobar") |
| 165 | fp.seek(0,0) |
| 166 | s = fp.read() |
| 167 | fp.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 168 | self.assertTrue(s == "foobar") |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 169 | |
| 170 | def test_tmpnam(self): |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 171 | if not hasattr(os, "tmpnam"): |
| 172 | return |
| 173 | warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, |
| 174 | r"test_os$") |
| 175 | name = os.tmpnam() |
| 176 | if sys.platform in ("win32",): |
| 177 | # The Windows tmpnam() seems useless. From the MS docs: |
| 178 | # |
| 179 | # The character string that tmpnam creates consists of |
| 180 | # the path prefix, defined by the entry P_tmpdir in the |
| 181 | # file STDIO.H, followed by a sequence consisting of the |
| 182 | # digit characters '0' through '9'; the numerical value |
| 183 | # of this string is in the range 1 - 65,535. Changing the |
| 184 | # definitions of L_tmpnam or P_tmpdir in STDIO.H does not |
| 185 | # change the operation of tmpnam. |
| 186 | # |
| 187 | # The really bizarre part is that, at least under MSVC6, |
| 188 | # P_tmpdir is "\\". That is, the path returned refers to |
| 189 | # the root of the current drive. That's a terrible place to |
| 190 | # put temp files, and, depending on privileges, the user |
| 191 | # 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] | 192 | self.assertFalse(os.path.exists(name), |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 193 | "file already exists for temporary file") |
| 194 | else: |
| 195 | self.check_tempfile(name) |
| 196 | |
Amaury Forgeot d'Arc | e2e36ba | 2008-08-01 00:14:22 +0000 | [diff] [blame] | 197 | def fdopen_helper(self, *args): |
| 198 | fd = os.open(support.TESTFN, os.O_RDONLY) |
| 199 | fp2 = os.fdopen(fd, *args) |
| 200 | fp2.close() |
| 201 | |
| 202 | def test_fdopen(self): |
| 203 | self.fdopen_helper() |
| 204 | self.fdopen_helper('r') |
| 205 | self.fdopen_helper('r', 100) |
| 206 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 207 | # Test attributes on return values from os.*stat* family. |
| 208 | class StatAttributeTests(unittest.TestCase): |
| 209 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 210 | os.mkdir(support.TESTFN) |
| 211 | self.fname = os.path.join(support.TESTFN, "f1") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 212 | f = open(self.fname, 'wb') |
Guido van Rossum | 26d95c3 | 2007-08-27 23:18:54 +0000 | [diff] [blame] | 213 | f.write(b"ABC") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 214 | f.close() |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 215 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 216 | def tearDown(self): |
| 217 | os.unlink(self.fname) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 218 | os.rmdir(support.TESTFN) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 219 | |
| 220 | def test_stat_attributes(self): |
| 221 | if not hasattr(os, "stat"): |
| 222 | return |
| 223 | |
| 224 | import stat |
| 225 | result = os.stat(self.fname) |
| 226 | |
| 227 | # Make sure direct access works |
| 228 | self.assertEquals(result[stat.ST_SIZE], 3) |
| 229 | self.assertEquals(result.st_size, 3) |
| 230 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 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 |
| 240 | self.assertEquals(trunc(getattr(result, attr)), |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 241 | result[getattr(stat, name)]) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 242 | self.assertIn(attr, 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 | |
| 276 | # Use the constructr with a too-long tuple. |
| 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 |
Brett Cannon | cfaf10c | 2008-05-16 00:45:35 +0000 | [diff] [blame] | 295 | self.assertEquals(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): |
| 301 | self.assertEquals(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 | |
| 323 | # Use the constructr with a too-long tuple. |
| 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) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 336 | self.assertEquals(st2.st_mtime, int(st.st_mtime-delta)) |
| 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)) |
| 353 | self.assertEquals(os.stat(self.fname).st_mtime, t1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 354 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 355 | def test_1686475(self): |
| 356 | # Verify that an open file can be stat'ed |
| 357 | try: |
| 358 | os.stat(r"c:\pagefile.sys") |
| 359 | except WindowsError as e: |
Benjamin Peterson | c4fe6f3 | 2008-08-19 18:57:56 +0000 | [diff] [blame] | 360 | if e.errno == 2: # file does not exist; cannot run test |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 361 | return |
| 362 | self.fail("Could not stat pagefile.sys") |
| 363 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 364 | from test import mapping_tests |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 365 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 366 | class EnvironTests(mapping_tests.BasicTestMappingProtocol): |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 367 | """check that os.environ object conform to mapping protocol""" |
Walter Dörwald | 118f931 | 2004-06-02 18:42:25 +0000 | [diff] [blame] | 368 | type2test = None |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 369 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 370 | def setUp(self): |
| 371 | self.__save = dict(os.environ) |
Victor Stinner | 208d28c | 2010-05-07 00:54:14 +0000 | [diff] [blame] | 372 | if os.name not in ('os2', 'nt'): |
| 373 | self.__saveb = dict(os.environb) |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 374 | for key, value in self._reference().items(): |
| 375 | os.environ[key] = value |
| 376 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 377 | def tearDown(self): |
| 378 | os.environ.clear() |
| 379 | os.environ.update(self.__save) |
Victor Stinner | 208d28c | 2010-05-07 00:54:14 +0000 | [diff] [blame] | 380 | if os.name not in ('os2', 'nt'): |
| 381 | os.environb.clear() |
| 382 | os.environb.update(self.__saveb) |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 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") |
| 396 | value = os.popen("/bin/sh -c 'echo $HELLO'").read().strip() |
| 397 | self.assertEquals(value, "World") |
| 398 | |
Christian Heimes | 1a13d59 | 2007-11-08 14:16:55 +0000 | [diff] [blame] | 399 | def test_os_popen_iter(self): |
| 400 | if os.path.exists("/bin/sh"): |
| 401 | popen = os.popen("/bin/sh -c 'echo \"line1\nline2\nline3\"'") |
| 402 | it = iter(popen) |
| 403 | self.assertEquals(next(it), "line1\n") |
| 404 | self.assertEquals(next(it), "line2\n") |
| 405 | self.assertEquals(next(it), "line3\n") |
| 406 | self.assertRaises(StopIteration, next, it) |
| 407 | |
Guido van Rossum | 67aca9e | 2007-06-13 21:51:27 +0000 | [diff] [blame] | 408 | # Verify environ keys and values from the OS are of the |
| 409 | # correct str type. |
| 410 | def test_keyvalue_types(self): |
| 411 | for key, val in os.environ.items(): |
| 412 | self.assertEquals(type(key), str) |
| 413 | self.assertEquals(type(val), str) |
| 414 | |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 415 | def test_items(self): |
| 416 | for key, value in self._reference().items(): |
| 417 | self.assertEqual(os.environ.get(key), value) |
| 418 | |
Ezio Melotti | 19e4acf | 2010-02-22 15:59:01 +0000 | [diff] [blame] | 419 | # Issue 7310 |
| 420 | def test___repr__(self): |
| 421 | """Check that the repr() of os.environ looks like environ({...}).""" |
| 422 | env = os.environ |
| 423 | self.assertTrue(isinstance(env.data, dict)) |
| 424 | self.assertEqual(repr(env), 'environ({!r})'.format(env.data)) |
| 425 | |
Gregory P. Smith | b6e8c7e | 2010-02-27 07:22:22 +0000 | [diff] [blame] | 426 | def test_get_exec_path(self): |
| 427 | defpath_list = os.defpath.split(os.pathsep) |
| 428 | test_path = ['/monty', '/python', '', '/flying/circus'] |
| 429 | test_env = {'PATH': os.pathsep.join(test_path)} |
| 430 | |
| 431 | saved_environ = os.environ |
| 432 | try: |
| 433 | os.environ = dict(test_env) |
| 434 | # Test that defaulting to os.environ works. |
| 435 | self.assertSequenceEqual(test_path, os.get_exec_path()) |
| 436 | self.assertSequenceEqual(test_path, os.get_exec_path(env=None)) |
| 437 | finally: |
| 438 | os.environ = saved_environ |
| 439 | |
| 440 | # No PATH environment variable |
| 441 | self.assertSequenceEqual(defpath_list, os.get_exec_path({})) |
| 442 | # Empty PATH environment variable |
| 443 | self.assertSequenceEqual(('',), os.get_exec_path({'PATH':''})) |
| 444 | # Supplied PATH environment variable |
| 445 | self.assertSequenceEqual(test_path, os.get_exec_path(test_env)) |
| 446 | |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 447 | @unittest.skipIf(sys.platform == "win32", "POSIX specific test") |
| 448 | def test_environb(self): |
| 449 | # os.environ -> os.environb |
| 450 | value = 'euro\u20ac' |
| 451 | try: |
Benjamin Peterson | 180799d | 2010-05-06 22:25:42 +0000 | [diff] [blame] | 452 | value_bytes = value.encode(sys.getfilesystemencoding(), |
| 453 | 'surrogateescape') |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 454 | except UnicodeEncodeError: |
Benjamin Peterson | 180799d | 2010-05-06 22:25:42 +0000 | [diff] [blame] | 455 | msg = "U+20AC character is not encodable to %s" % ( |
| 456 | sys.getfilesystemencoding(),) |
Benjamin Peterson | 932d3f4 | 2010-05-06 22:26:31 +0000 | [diff] [blame] | 457 | self.skipTest(msg) |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 458 | os.environ['unicode'] = value |
| 459 | self.assertEquals(os.environ['unicode'], value) |
| 460 | self.assertEquals(os.environb[b'unicode'], value_bytes) |
| 461 | |
| 462 | # os.environb -> os.environ |
| 463 | value = b'\xff' |
| 464 | os.environb[b'bytes'] = value |
| 465 | self.assertEquals(os.environb[b'bytes'], value) |
| 466 | value_str = value.decode(sys.getfilesystemencoding(), 'surrogateescape') |
| 467 | self.assertEquals(os.environ['bytes'], value_str) |
Ezio Melotti | 19e4acf | 2010-02-22 15:59:01 +0000 | [diff] [blame] | 468 | |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 469 | class WalkTests(unittest.TestCase): |
| 470 | """Tests for os.walk().""" |
| 471 | |
| 472 | def test_traversal(self): |
| 473 | import os |
| 474 | from os.path import join |
| 475 | |
| 476 | # Build: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 477 | # TESTFN/ |
| 478 | # TEST1/ a file kid and two directory kids |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 479 | # tmp1 |
| 480 | # SUB1/ a file kid and a directory kid |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 481 | # tmp2 |
| 482 | # SUB11/ no kids |
| 483 | # SUB2/ a file kid and a dirsymlink kid |
| 484 | # tmp3 |
| 485 | # link/ a symlink to TESTFN.2 |
| 486 | # TEST2/ |
| 487 | # tmp4 a lone file |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 488 | walk_path = join(support.TESTFN, "TEST1") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 489 | sub1_path = join(walk_path, "SUB1") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 490 | sub11_path = join(sub1_path, "SUB11") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 491 | sub2_path = join(walk_path, "SUB2") |
| 492 | tmp1_path = join(walk_path, "tmp1") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 493 | tmp2_path = join(sub1_path, "tmp2") |
| 494 | tmp3_path = join(sub2_path, "tmp3") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 495 | link_path = join(sub2_path, "link") |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 496 | t2_path = join(support.TESTFN, "TEST2") |
| 497 | tmp4_path = join(support.TESTFN, "TEST2", "tmp4") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 498 | |
| 499 | # Create stuff. |
| 500 | os.makedirs(sub11_path) |
| 501 | os.makedirs(sub2_path) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 502 | os.makedirs(t2_path) |
| 503 | for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path: |
Alex Martelli | 01c77c6 | 2006-08-24 02:58:11 +0000 | [diff] [blame] | 504 | f = open(path, "w") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 505 | f.write("I'm " + path + " and proud of it. Blame test_os.\n") |
| 506 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 507 | if hasattr(os, "symlink"): |
| 508 | os.symlink(os.path.abspath(t2_path), link_path) |
| 509 | sub2_tree = (sub2_path, ["link"], ["tmp3"]) |
| 510 | else: |
| 511 | sub2_tree = (sub2_path, [], ["tmp3"]) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 512 | |
| 513 | # Walk top-down. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 514 | all = list(os.walk(walk_path)) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 515 | self.assertEqual(len(all), 4) |
| 516 | # We can't know which order SUB1 and SUB2 will appear in. |
| 517 | # Not flipped: TESTFN, SUB1, SUB11, SUB2 |
| 518 | # flipped: TESTFN, SUB2, SUB1, SUB11 |
| 519 | flipped = all[0][1][0] != "SUB1" |
| 520 | all[0][1].sort() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 521 | self.assertEqual(all[0], (walk_path, ["SUB1", "SUB2"], ["tmp1"])) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 522 | self.assertEqual(all[1 + flipped], (sub1_path, ["SUB11"], ["tmp2"])) |
| 523 | self.assertEqual(all[2 + flipped], (sub11_path, [], [])) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 524 | self.assertEqual(all[3 - 2 * flipped], sub2_tree) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 525 | |
| 526 | # Prune the search. |
| 527 | all = [] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 528 | for root, dirs, files in os.walk(walk_path): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 529 | all.append((root, dirs, files)) |
| 530 | # Don't descend into SUB1. |
| 531 | if 'SUB1' in dirs: |
| 532 | # Note that this also mutates the dirs we appended to all! |
| 533 | dirs.remove('SUB1') |
| 534 | self.assertEqual(len(all), 2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 535 | self.assertEqual(all[0], (walk_path, ["SUB2"], ["tmp1"])) |
| 536 | self.assertEqual(all[1], sub2_tree) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 537 | |
| 538 | # Walk bottom-up. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 539 | all = list(os.walk(walk_path, topdown=False)) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 540 | self.assertEqual(len(all), 4) |
| 541 | # We can't know which order SUB1 and SUB2 will appear in. |
| 542 | # Not flipped: SUB11, SUB1, SUB2, TESTFN |
| 543 | # flipped: SUB2, SUB11, SUB1, TESTFN |
| 544 | flipped = all[3][1][0] != "SUB1" |
| 545 | all[3][1].sort() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 546 | self.assertEqual(all[3], (walk_path, ["SUB1", "SUB2"], ["tmp1"])) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 547 | self.assertEqual(all[flipped], (sub11_path, [], [])) |
| 548 | self.assertEqual(all[flipped + 1], (sub1_path, ["SUB11"], ["tmp2"])) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 549 | self.assertEqual(all[2 - 2 * flipped], sub2_tree) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 550 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 551 | if hasattr(os, "symlink"): |
| 552 | # Walk, following symlinks. |
| 553 | for root, dirs, files in os.walk(walk_path, followlinks=True): |
| 554 | if root == link_path: |
| 555 | self.assertEqual(dirs, []) |
| 556 | self.assertEqual(files, ["tmp4"]) |
| 557 | break |
| 558 | else: |
| 559 | self.fail("Didn't follow symlink with followlinks=True") |
| 560 | |
| 561 | def tearDown(self): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 562 | # Tear everything down. This is a decent use for bottom-up on |
| 563 | # Windows, which doesn't have a recursive delete command. The |
| 564 | # (not so) subtlety is that rmdir will fail unless the dir's |
| 565 | # kids are removed first, so bottom up is essential. |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 566 | for root, dirs, files in os.walk(support.TESTFN, topdown=False): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 567 | for name in files: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 568 | os.remove(os.path.join(root, name)) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 569 | for name in dirs: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 570 | dirname = os.path.join(root, name) |
| 571 | if not os.path.islink(dirname): |
| 572 | os.rmdir(dirname) |
| 573 | else: |
| 574 | os.remove(dirname) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 575 | os.rmdir(support.TESTFN) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 576 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 577 | class MakedirTests(unittest.TestCase): |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 578 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 579 | os.mkdir(support.TESTFN) |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 580 | |
| 581 | def test_makedir(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 582 | base = support.TESTFN |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 583 | path = os.path.join(base, 'dir1', 'dir2', 'dir3') |
| 584 | os.makedirs(path) # Should work |
| 585 | path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4') |
| 586 | os.makedirs(path) |
| 587 | |
| 588 | # Try paths with a '.' in them |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 589 | self.assertRaises(OSError, os.makedirs, os.curdir) |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 590 | path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4', 'dir5', os.curdir) |
| 591 | os.makedirs(path) |
| 592 | path = os.path.join(base, 'dir1', os.curdir, 'dir2', 'dir3', 'dir4', |
| 593 | 'dir5', 'dir6') |
| 594 | os.makedirs(path) |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 595 | |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 596 | def tearDown(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 597 | path = os.path.join(support.TESTFN, 'dir1', 'dir2', 'dir3', |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 598 | 'dir4', 'dir5', 'dir6') |
| 599 | # If the tests failed, the bottom-most directory ('../dir6') |
| 600 | # may not have been created, so we look for the outermost directory |
| 601 | # that exists. |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 602 | while not os.path.exists(path) and path != support.TESTFN: |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 603 | path = os.path.dirname(path) |
| 604 | |
| 605 | os.removedirs(path) |
| 606 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 607 | class DevNullTests(unittest.TestCase): |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 608 | def test_devnull(self): |
Alex Martelli | 01c77c6 | 2006-08-24 02:58:11 +0000 | [diff] [blame] | 609 | f = open(os.devnull, 'w') |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 610 | f.write('hello') |
| 611 | f.close() |
Alex Martelli | 01c77c6 | 2006-08-24 02:58:11 +0000 | [diff] [blame] | 612 | f = open(os.devnull, 'r') |
Tim Peters | 4182cfd | 2004-06-08 20:34:34 +0000 | [diff] [blame] | 613 | self.assertEqual(f.read(), '') |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 614 | f.close() |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 615 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 616 | class URandomTests(unittest.TestCase): |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 617 | def test_urandom(self): |
| 618 | try: |
| 619 | self.assertEqual(len(os.urandom(1)), 1) |
| 620 | self.assertEqual(len(os.urandom(10)), 10) |
| 621 | self.assertEqual(len(os.urandom(100)), 100) |
| 622 | self.assertEqual(len(os.urandom(1000)), 1000) |
| 623 | except NotImplementedError: |
| 624 | pass |
| 625 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 626 | class ExecTests(unittest.TestCase): |
Mark Dickinson | 7cf0389 | 2010-04-16 13:45:35 +0000 | [diff] [blame] | 627 | @unittest.skipIf(USING_LINUXTHREADS, |
| 628 | "avoid triggering a linuxthreads bug: see issue #4970") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 629 | def test_execvpe_with_bad_program(self): |
Mark Dickinson | 7cf0389 | 2010-04-16 13:45:35 +0000 | [diff] [blame] | 630 | self.assertRaises(OSError, os.execvpe, 'no such app-', |
| 631 | ['no such app-'], None) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 632 | |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 633 | def test_execvpe_with_bad_arglist(self): |
| 634 | self.assertRaises(ValueError, os.execvpe, 'notepad', [], None) |
| 635 | |
Gregory P. Smith | 4ae3777 | 2010-05-08 18:05:46 +0000 | [diff] [blame] | 636 | class _stub_out_for_execvpe_test(object): |
| 637 | """ |
| 638 | Stubs out execv, execve and get_exec_path functions when |
| 639 | used as context manager. Records exec calls. The mock execv |
| 640 | and execve functions always raise an exception as they would |
| 641 | normally never return. |
| 642 | """ |
| 643 | def __init__(self): |
| 644 | # A list of tuples containing (function name, first arg, args) |
| 645 | # of calls to execv or execve that have been made. |
| 646 | self.calls = [] |
| 647 | def _mock_execv(self, name, *args): |
| 648 | self.calls.append(('execv', name, args)) |
| 649 | raise RuntimeError("execv called") |
| 650 | |
| 651 | def _mock_execve(self, name, *args): |
| 652 | self.calls.append(('execve', name, args)) |
| 653 | raise OSError(errno.ENOTDIR, "execve called") |
| 654 | |
| 655 | def _mock_get_exec_path(self, env=None): |
Gregory P. Smith | 3ea0062 | 2010-05-09 03:36:42 +0000 | [diff] [blame] | 656 | return [os.sep+'p', os.sep+'pp'] |
Gregory P. Smith | 4ae3777 | 2010-05-08 18:05:46 +0000 | [diff] [blame] | 657 | |
| 658 | def __enter__(self): |
| 659 | self.orig_execv = os.execv |
| 660 | self.orig_execve = os.execve |
| 661 | self.orig_get_exec_path = os.get_exec_path |
| 662 | os.execv = self._mock_execv |
| 663 | os.execve = self._mock_execve |
| 664 | os.get_exec_path = self._mock_get_exec_path |
| 665 | |
| 666 | def __exit__(self, type, value, tb): |
| 667 | os.execv = self.orig_execv |
| 668 | os.execve = self.orig_execve |
| 669 | os.get_exec_path = self.orig_get_exec_path |
| 670 | |
| 671 | @unittest.skipUnless(hasattr(os, '_execvpe'), |
| 672 | "No internal os._execvpe function to test.") |
| 673 | def test_internal_execvpe(self): |
| 674 | exec_stubbed = self._stub_out_for_execvpe_test() |
| 675 | with exec_stubbed: |
Gregory P. Smith | 3ea0062 | 2010-05-09 03:36:42 +0000 | [diff] [blame] | 676 | self.assertRaises(RuntimeError, os._execvpe, os.sep+'f', ['-a']) |
| 677 | self.assertEqual([('execv', os.sep+'f', (['-a'],))], |
| 678 | exec_stubbed.calls) |
Gregory P. Smith | 4ae3777 | 2010-05-08 18:05:46 +0000 | [diff] [blame] | 679 | exec_stubbed.calls = [] |
| 680 | self.assertRaises(OSError, os._execvpe, 'f', ['-a'], |
| 681 | env={'spam': 'beans'}) |
Gregory P. Smith | 3ea0062 | 2010-05-09 03:36:42 +0000 | [diff] [blame] | 682 | self.assertEqual([('execve', os.sep+'p'+os.sep+'f', |
| 683 | (['-a'], {'spam': 'beans'})), |
| 684 | ('execve', os.sep+'pp'+os.sep+'f', |
| 685 | (['-a'], {'spam': 'beans'}))], |
Gregory P. Smith | 4ae3777 | 2010-05-08 18:05:46 +0000 | [diff] [blame] | 686 | exec_stubbed.calls) |
| 687 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 688 | class Win32ErrorTests(unittest.TestCase): |
| 689 | def test_rename(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 690 | self.assertRaises(WindowsError, os.rename, support.TESTFN, support.TESTFN+".bak") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 691 | |
| 692 | def test_remove(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 693 | self.assertRaises(WindowsError, os.remove, support.TESTFN) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 694 | |
| 695 | def test_chdir(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 696 | self.assertRaises(WindowsError, os.chdir, support.TESTFN) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 697 | |
| 698 | def test_mkdir(self): |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 699 | f = open(support.TESTFN, "w") |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 700 | try: |
| 701 | self.assertRaises(WindowsError, os.mkdir, support.TESTFN) |
| 702 | finally: |
| 703 | f.close() |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 704 | os.unlink(support.TESTFN) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 705 | |
| 706 | def test_utime(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 707 | self.assertRaises(WindowsError, os.utime, support.TESTFN, None) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 708 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 709 | def test_chmod(self): |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 710 | self.assertRaises(WindowsError, os.chmod, support.TESTFN, 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 711 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 712 | class TestInvalidFD(unittest.TestCase): |
Benjamin Peterson | 05e782f | 2009-01-19 15:15:02 +0000 | [diff] [blame] | 713 | singles = ["fchdir", "dup", "fdopen", "fdatasync", "fstat", |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 714 | "fstatvfs", "fsync", "tcgetpgrp", "ttyname"] |
| 715 | #singles.append("close") |
| 716 | #We omit close because it doesn'r raise an exception on some platforms |
| 717 | def get_single(f): |
| 718 | def helper(self): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 719 | if hasattr(os, f): |
| 720 | self.check(getattr(os, f)) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 721 | return helper |
| 722 | for f in singles: |
| 723 | locals()["test_"+f] = get_single(f) |
| 724 | |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 725 | def check(self, f, *args): |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 726 | try: |
| 727 | f(support.make_bad_fd(), *args) |
| 728 | except OSError as e: |
| 729 | self.assertEqual(e.errno, errno.EBADF) |
| 730 | else: |
| 731 | self.fail("%r didn't raise a OSError with a bad file descriptor" |
| 732 | % f) |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 733 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 734 | def test_isatty(self): |
| 735 | if hasattr(os, "isatty"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 736 | self.assertEqual(os.isatty(support.make_bad_fd()), False) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 737 | |
| 738 | def test_closerange(self): |
| 739 | if hasattr(os, "closerange"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 740 | fd = support.make_bad_fd() |
R. David Murray | 630cc48 | 2009-07-22 15:20:27 +0000 | [diff] [blame] | 741 | # Make sure none of the descriptors we are about to close are |
| 742 | # currently valid (issue 6542). |
| 743 | for i in range(10): |
| 744 | try: os.fstat(fd+i) |
| 745 | except OSError: |
| 746 | pass |
| 747 | else: |
| 748 | break |
| 749 | if i < 2: |
| 750 | raise unittest.SkipTest( |
| 751 | "Unable to acquire a range of invalid file descriptors") |
| 752 | self.assertEqual(os.closerange(fd, fd + i-1), None) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 753 | |
| 754 | def test_dup2(self): |
| 755 | if hasattr(os, "dup2"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 756 | self.check(os.dup2, 20) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 757 | |
| 758 | def test_fchmod(self): |
| 759 | if hasattr(os, "fchmod"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 760 | self.check(os.fchmod, 0) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 761 | |
| 762 | def test_fchown(self): |
| 763 | if hasattr(os, "fchown"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 764 | self.check(os.fchown, -1, -1) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 765 | |
| 766 | def test_fpathconf(self): |
| 767 | if hasattr(os, "fpathconf"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 768 | self.check(os.fpathconf, "PC_NAME_MAX") |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 769 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 770 | def test_ftruncate(self): |
| 771 | if hasattr(os, "ftruncate"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 772 | self.check(os.ftruncate, 0) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 773 | |
| 774 | def test_lseek(self): |
| 775 | if hasattr(os, "lseek"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 776 | self.check(os.lseek, 0, 0) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 777 | |
| 778 | def test_read(self): |
| 779 | if hasattr(os, "read"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 780 | self.check(os.read, 1) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 781 | |
| 782 | def test_tcsetpgrpt(self): |
| 783 | if hasattr(os, "tcsetpgrp"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 784 | self.check(os.tcsetpgrp, 0) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 785 | |
| 786 | def test_write(self): |
| 787 | if hasattr(os, "write"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 788 | self.check(os.write, b" ") |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 789 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 790 | if sys.platform != 'win32': |
| 791 | class Win32ErrorTests(unittest.TestCase): |
| 792 | pass |
| 793 | |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 794 | class PosixUidGidTests(unittest.TestCase): |
| 795 | if hasattr(os, 'setuid'): |
| 796 | def test_setuid(self): |
| 797 | if os.getuid() != 0: |
| 798 | self.assertRaises(os.error, os.setuid, 0) |
| 799 | self.assertRaises(OverflowError, os.setuid, 1<<32) |
| 800 | |
| 801 | if hasattr(os, 'setgid'): |
| 802 | def test_setgid(self): |
| 803 | if os.getuid() != 0: |
| 804 | self.assertRaises(os.error, os.setgid, 0) |
| 805 | self.assertRaises(OverflowError, os.setgid, 1<<32) |
| 806 | |
| 807 | if hasattr(os, 'seteuid'): |
| 808 | def test_seteuid(self): |
| 809 | if os.getuid() != 0: |
| 810 | self.assertRaises(os.error, os.seteuid, 0) |
| 811 | self.assertRaises(OverflowError, os.seteuid, 1<<32) |
| 812 | |
| 813 | if hasattr(os, 'setegid'): |
| 814 | def test_setegid(self): |
| 815 | if os.getuid() != 0: |
| 816 | self.assertRaises(os.error, os.setegid, 0) |
| 817 | self.assertRaises(OverflowError, os.setegid, 1<<32) |
| 818 | |
| 819 | if hasattr(os, 'setreuid'): |
| 820 | def test_setreuid(self): |
| 821 | if os.getuid() != 0: |
| 822 | self.assertRaises(os.error, os.setreuid, 0, 0) |
| 823 | self.assertRaises(OverflowError, os.setreuid, 1<<32, 0) |
| 824 | self.assertRaises(OverflowError, os.setreuid, 0, 1<<32) |
Benjamin Peterson | ebe87ba | 2010-03-06 20:34:24 +0000 | [diff] [blame] | 825 | |
| 826 | def test_setreuid_neg1(self): |
| 827 | # Needs to accept -1. We run this in a subprocess to avoid |
| 828 | # altering the test runner's process state (issue8045). |
Benjamin Peterson | ebe87ba | 2010-03-06 20:34:24 +0000 | [diff] [blame] | 829 | subprocess.check_call([ |
| 830 | sys.executable, '-c', |
| 831 | 'import os,sys;os.setreuid(-1,-1);sys.exit(0)']) |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 832 | |
| 833 | if hasattr(os, 'setregid'): |
| 834 | def test_setregid(self): |
| 835 | if os.getuid() != 0: |
| 836 | self.assertRaises(os.error, os.setregid, 0, 0) |
| 837 | self.assertRaises(OverflowError, os.setregid, 1<<32, 0) |
| 838 | self.assertRaises(OverflowError, os.setregid, 0, 1<<32) |
Benjamin Peterson | ebe87ba | 2010-03-06 20:34:24 +0000 | [diff] [blame] | 839 | |
| 840 | def test_setregid_neg1(self): |
| 841 | # Needs to accept -1. We run this in a subprocess to avoid |
| 842 | # altering the test runner's process state (issue8045). |
Benjamin Peterson | ebe87ba | 2010-03-06 20:34:24 +0000 | [diff] [blame] | 843 | subprocess.check_call([ |
| 844 | sys.executable, '-c', |
| 845 | 'import os,sys;os.setregid(-1,-1);sys.exit(0)']) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 846 | |
Mark Dickinson | 7061368 | 2009-05-05 21:34:59 +0000 | [diff] [blame] | 847 | @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] | 848 | class Pep383Tests(unittest.TestCase): |
| 849 | filenames = [b'foo\xf6bar', 'foo\xf6bar'.encode("utf-8")] |
| 850 | |
| 851 | def setUp(self): |
| 852 | self.fsencoding = sys.getfilesystemencoding() |
| 853 | sys.setfilesystemencoding("utf-8") |
| 854 | self.dir = support.TESTFN |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 855 | self.bdir = self.dir.encode("utf-8", "surrogateescape") |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 856 | os.mkdir(self.dir) |
| 857 | self.unicodefn = [] |
| 858 | for fn in self.filenames: |
| 859 | f = open(os.path.join(self.bdir, fn), "w") |
| 860 | f.close() |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 861 | self.unicodefn.append(fn.decode("utf-8", "surrogateescape")) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 862 | |
| 863 | def tearDown(self): |
| 864 | shutil.rmtree(self.dir) |
| 865 | sys.setfilesystemencoding(self.fsencoding) |
| 866 | |
| 867 | def test_listdir(self): |
| 868 | expected = set(self.unicodefn) |
| 869 | found = set(os.listdir(support.TESTFN)) |
| 870 | self.assertEquals(found, expected) |
| 871 | |
| 872 | def test_open(self): |
| 873 | for fn in self.unicodefn: |
| 874 | f = open(os.path.join(self.dir, fn)) |
| 875 | f.close() |
| 876 | |
| 877 | def test_stat(self): |
| 878 | for fn in self.unicodefn: |
| 879 | os.stat(os.path.join(self.dir, fn)) |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 880 | else: |
| 881 | class PosixUidGidTests(unittest.TestCase): |
| 882 | pass |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 883 | class Pep383Tests(unittest.TestCase): |
| 884 | pass |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 885 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 886 | @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests") |
| 887 | class Win32KillTests(unittest.TestCase): |
| 888 | def _kill(self, sig, *args): |
| 889 | # Send a subprocess a signal (or in some cases, just an int to be |
| 890 | # the return value) |
| 891 | proc = subprocess.Popen(*args) |
| 892 | os.kill(proc.pid, sig) |
| 893 | self.assertEqual(proc.wait(), sig) |
| 894 | |
| 895 | def test_kill_sigterm(self): |
| 896 | # SIGTERM doesn't mean anything special, but make sure it works |
| 897 | self._kill(signal.SIGTERM, [sys.executable]) |
| 898 | |
| 899 | def test_kill_int(self): |
| 900 | # os.kill on Windows can take an int which gets set as the exit code |
| 901 | self._kill(100, [sys.executable]) |
| 902 | |
| 903 | def _kill_with_event(self, event, name): |
| 904 | # Run a script which has console control handling enabled. |
| 905 | proc = subprocess.Popen([sys.executable, |
| 906 | os.path.join(os.path.dirname(__file__), |
| 907 | "win_console_handler.py")], |
| 908 | creationflags=subprocess.CREATE_NEW_PROCESS_GROUP) |
| 909 | # Let the interpreter startup before we send signals. See #3137. |
| 910 | time.sleep(0.5) |
| 911 | os.kill(proc.pid, event) |
| 912 | # proc.send_signal(event) could also be done here. |
| 913 | # Allow time for the signal to be passed and the process to exit. |
| 914 | time.sleep(0.5) |
| 915 | if not proc.poll(): |
| 916 | # Forcefully kill the process if we weren't able to signal it. |
| 917 | os.kill(proc.pid, signal.SIGINT) |
| 918 | self.fail("subprocess did not stop on {}".format(name)) |
| 919 | |
| 920 | @unittest.skip("subprocesses aren't inheriting CTRL+C property") |
| 921 | def test_CTRL_C_EVENT(self): |
| 922 | from ctypes import wintypes |
| 923 | import ctypes |
| 924 | |
| 925 | # Make a NULL value by creating a pointer with no argument. |
| 926 | NULL = ctypes.POINTER(ctypes.c_int)() |
| 927 | SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler |
| 928 | SetConsoleCtrlHandler.argtypes = (ctypes.POINTER(ctypes.c_int), |
| 929 | wintypes.BOOL) |
| 930 | SetConsoleCtrlHandler.restype = wintypes.BOOL |
| 931 | |
| 932 | # Calling this with NULL and FALSE causes the calling process to |
| 933 | # handle CTRL+C, rather than ignore it. This property is inherited |
| 934 | # by subprocesses. |
| 935 | SetConsoleCtrlHandler(NULL, 0) |
| 936 | |
| 937 | self._kill_with_event(signal.CTRL_C_EVENT, "CTRL_C_EVENT") |
| 938 | |
| 939 | def test_CTRL_BREAK_EVENT(self): |
| 940 | self._kill_with_event(signal.CTRL_BREAK_EVENT, "CTRL_BREAK_EVENT") |
| 941 | |
| 942 | |
Victor Stinner | bf9bcab | 2010-05-09 03:15:33 +0000 | [diff] [blame] | 943 | class MiscTests(unittest.TestCase): |
Benjamin Peterson | 31191a9 | 2010-05-09 03:22:58 +0000 | [diff] [blame] | 944 | |
| 945 | @unittest.skipIf(os.name == "nt", "POSIX specific test") |
Victor Stinner | bf9bcab | 2010-05-09 03:15:33 +0000 | [diff] [blame] | 946 | def test_fsencode(self): |
| 947 | self.assertEquals(os.fsencode(b'ab\xff'), b'ab\xff') |
| 948 | self.assertEquals(os.fsencode('ab\uDCFF'), b'ab\xff') |
| 949 | |
| 950 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 951 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 952 | support.run_unittest( |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 953 | FileTests, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 954 | StatAttributeTests, |
| 955 | EnvironTests, |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 956 | WalkTests, |
| 957 | MakedirTests, |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 958 | DevNullTests, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 959 | URandomTests, |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 960 | ExecTests, |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 961 | Win32ErrorTests, |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 962 | TestInvalidFD, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 963 | PosixUidGidTests, |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 964 | Pep383Tests, |
Victor Stinner | bf9bcab | 2010-05-09 03:15:33 +0000 | [diff] [blame] | 965 | Win32KillTests, |
| 966 | MiscTests, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 967 | ) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 968 | |
| 969 | if __name__ == "__main__": |
| 970 | test_main() |