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