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