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 |
Hirokazu Yamamoto | 54c950f | 2010-10-08 08:38:15 +0000 | [diff] [blame] | 16 | import mmap |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 17 | import platform |
| 18 | import re |
Hirokazu Yamamoto | 54c950f | 2010-10-08 08:38:15 +0000 | [diff] [blame] | 19 | import uuid |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 20 | import asyncore |
| 21 | import asynchat |
| 22 | import socket |
Charles-François Natali | 7372b06 | 2012-02-05 15:15:38 +0100 | [diff] [blame] | 23 | import itertools |
| 24 | import stat |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 25 | import locale |
| 26 | import codecs |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 27 | import decimal |
| 28 | import fractions |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame^] | 29 | import pickle |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 30 | try: |
| 31 | import threading |
| 32 | except ImportError: |
| 33 | threading = None |
Antoine Pitrou | ec34ab5 | 2013-08-16 20:44:38 +0200 | [diff] [blame] | 34 | try: |
| 35 | import resource |
| 36 | except ImportError: |
| 37 | resource = None |
Victor Stinner | 7ba6b0f | 2013-09-08 11:47:54 +0200 | [diff] [blame] | 38 | try: |
| 39 | import fcntl |
| 40 | except ImportError: |
| 41 | fcntl = None |
Antoine Pitrou | ec34ab5 | 2013-08-16 20:44:38 +0200 | [diff] [blame] | 42 | |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 43 | from test.script_helper import assert_python_ok |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 44 | |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 45 | with warnings.catch_warnings(): |
| 46 | warnings.simplefilter("ignore", DeprecationWarning) |
| 47 | os.stat_float_times(True) |
Victor Stinner | 1aa54a4 | 2012-02-08 04:09:37 +0100 | [diff] [blame] | 48 | st = os.stat(__file__) |
| 49 | stat_supports_subsecond = ( |
| 50 | # check if float and int timestamps are different |
| 51 | (st.st_atime != st[7]) |
| 52 | or (st.st_mtime != st[8]) |
| 53 | or (st.st_ctime != st[9])) |
| 54 | |
Mark Dickinson | 7cf0389 | 2010-04-16 13:45:35 +0000 | [diff] [blame] | 55 | # Detect whether we're on a Linux system that uses the (now outdated |
| 56 | # and unmaintained) linuxthreads threading library. There's an issue |
| 57 | # when combining linuxthreads with a failed execv call: see |
| 58 | # http://bugs.python.org/issue4970. |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 59 | if hasattr(sys, 'thread_info') and sys.thread_info.version: |
| 60 | USING_LINUXTHREADS = sys.thread_info.version.startswith("linuxthreads") |
| 61 | else: |
| 62 | USING_LINUXTHREADS = False |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 63 | |
Stefan Krah | ebee49a | 2013-01-17 15:31:00 +0100 | [diff] [blame] | 64 | # Issue #14110: Some tests fail on FreeBSD if the user is in the wheel group. |
| 65 | HAVE_WHEEL_GROUP = sys.platform.startswith('freebsd') and os.getgid() == 0 |
| 66 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 67 | # Tests creating TESTFN |
| 68 | class FileTests(unittest.TestCase): |
| 69 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 70 | if os.path.exists(support.TESTFN): |
| 71 | os.unlink(support.TESTFN) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 72 | tearDown = setUp |
| 73 | |
| 74 | def test_access(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 75 | f = os.open(support.TESTFN, os.O_CREAT|os.O_RDWR) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 76 | os.close(f) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 77 | self.assertTrue(os.access(support.TESTFN, os.W_OK)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 78 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 79 | def test_closerange(self): |
Antoine Pitrou | b9ee06c | 2008-08-16 22:03:17 +0000 | [diff] [blame] | 80 | first = os.open(support.TESTFN, os.O_CREAT|os.O_RDWR) |
| 81 | # We must allocate two consecutive file descriptors, otherwise |
| 82 | # it will mess up other file descriptors (perhaps even the three |
| 83 | # standard ones). |
| 84 | second = os.dup(first) |
| 85 | try: |
| 86 | retries = 0 |
| 87 | while second != first + 1: |
| 88 | os.close(first) |
| 89 | retries += 1 |
| 90 | if retries > 10: |
| 91 | # XXX test skipped |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 92 | self.skipTest("couldn't allocate two consecutive fds") |
Antoine Pitrou | b9ee06c | 2008-08-16 22:03:17 +0000 | [diff] [blame] | 93 | first, second = second, os.dup(second) |
| 94 | finally: |
| 95 | os.close(second) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 96 | # close a fd that is open, and one that isn't |
Antoine Pitrou | b9ee06c | 2008-08-16 22:03:17 +0000 | [diff] [blame] | 97 | os.closerange(first, first + 2) |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 98 | self.assertRaises(OSError, os.write, first, b"a") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 99 | |
Benjamin Peterson | 1cc6df9 | 2010-06-30 17:39:45 +0000 | [diff] [blame] | 100 | @support.cpython_only |
Hirokazu Yamamoto | 4c19e6e | 2008-09-08 23:41:21 +0000 | [diff] [blame] | 101 | def test_rename(self): |
| 102 | path = support.TESTFN |
| 103 | old = sys.getrefcount(path) |
| 104 | self.assertRaises(TypeError, os.rename, path, 0) |
| 105 | new = sys.getrefcount(path) |
| 106 | self.assertEqual(old, new) |
| 107 | |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 108 | def test_read(self): |
| 109 | with open(support.TESTFN, "w+b") as fobj: |
| 110 | fobj.write(b"spam") |
| 111 | fobj.flush() |
| 112 | fd = fobj.fileno() |
| 113 | os.lseek(fd, 0, 0) |
| 114 | s = os.read(fd, 4) |
| 115 | self.assertEqual(type(s), bytes) |
| 116 | self.assertEqual(s, b"spam") |
| 117 | |
| 118 | def test_write(self): |
| 119 | # os.write() accepts bytes- and buffer-like objects but not strings |
| 120 | fd = os.open(support.TESTFN, os.O_CREAT | os.O_WRONLY) |
| 121 | self.assertRaises(TypeError, os.write, fd, "beans") |
| 122 | os.write(fd, b"bacon\n") |
| 123 | os.write(fd, bytearray(b"eggs\n")) |
| 124 | os.write(fd, memoryview(b"spam\n")) |
| 125 | os.close(fd) |
| 126 | with open(support.TESTFN, "rb") as fobj: |
Antoine Pitrou | d62269f | 2008-09-15 23:54:52 +0000 | [diff] [blame] | 127 | self.assertEqual(fobj.read().splitlines(), |
| 128 | [b"bacon", b"eggs", b"spam"]) |
Antoine Pitrou | 9cadb1b | 2008-09-15 23:02:56 +0000 | [diff] [blame] | 129 | |
Victor Stinner | e0daff1 | 2011-03-20 23:36:35 +0100 | [diff] [blame] | 130 | def write_windows_console(self, *args): |
| 131 | retcode = subprocess.call(args, |
| 132 | # use a new console to not flood the test output |
| 133 | creationflags=subprocess.CREATE_NEW_CONSOLE, |
| 134 | # use a shell to hide the console window (SW_HIDE) |
| 135 | shell=True) |
| 136 | self.assertEqual(retcode, 0) |
| 137 | |
| 138 | @unittest.skipUnless(sys.platform == 'win32', |
| 139 | 'test specific to the Windows console') |
| 140 | def test_write_windows_console(self): |
| 141 | # Issue #11395: the Windows console returns an error (12: not enough |
| 142 | # space error) on writing into stdout if stdout mode is binary and the |
| 143 | # length is greater than 66,000 bytes (or less, depending on heap |
| 144 | # usage). |
| 145 | code = "print('x' * 100000)" |
| 146 | self.write_windows_console(sys.executable, "-c", code) |
| 147 | self.write_windows_console(sys.executable, "-u", "-c", code) |
| 148 | |
Amaury Forgeot d'Arc | e2e36ba | 2008-08-01 00:14:22 +0000 | [diff] [blame] | 149 | def fdopen_helper(self, *args): |
| 150 | fd = os.open(support.TESTFN, os.O_RDONLY) |
Victor Stinner | bef7fdf | 2011-07-01 13:45:30 +0200 | [diff] [blame] | 151 | f = os.fdopen(fd, *args) |
| 152 | f.close() |
Amaury Forgeot d'Arc | e2e36ba | 2008-08-01 00:14:22 +0000 | [diff] [blame] | 153 | |
| 154 | def test_fdopen(self): |
Victor Stinner | bef7fdf | 2011-07-01 13:45:30 +0200 | [diff] [blame] | 155 | fd = os.open(support.TESTFN, os.O_CREAT|os.O_RDWR) |
| 156 | os.close(fd) |
| 157 | |
Amaury Forgeot d'Arc | e2e36ba | 2008-08-01 00:14:22 +0000 | [diff] [blame] | 158 | self.fdopen_helper() |
| 159 | self.fdopen_helper('r') |
| 160 | self.fdopen_helper('r', 100) |
| 161 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 162 | def test_replace(self): |
| 163 | TESTFN2 = support.TESTFN + ".2" |
| 164 | with open(support.TESTFN, 'w') as f: |
| 165 | f.write("1") |
| 166 | with open(TESTFN2, 'w') as f: |
| 167 | f.write("2") |
| 168 | self.addCleanup(os.unlink, TESTFN2) |
| 169 | os.replace(support.TESTFN, TESTFN2) |
| 170 | self.assertRaises(FileNotFoundError, os.stat, support.TESTFN) |
| 171 | with open(TESTFN2, 'r') as f: |
| 172 | self.assertEqual(f.read(), "1") |
| 173 | |
Victor Stinner | bef7fdf | 2011-07-01 13:45:30 +0200 | [diff] [blame] | 174 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 175 | # Test attributes on return values from os.*stat* family. |
| 176 | class StatAttributeTests(unittest.TestCase): |
| 177 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 178 | os.mkdir(support.TESTFN) |
| 179 | self.fname = os.path.join(support.TESTFN, "f1") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 180 | f = open(self.fname, 'wb') |
Guido van Rossum | 26d95c3 | 2007-08-27 23:18:54 +0000 | [diff] [blame] | 181 | f.write(b"ABC") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 182 | f.close() |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 183 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 184 | def tearDown(self): |
| 185 | os.unlink(self.fname) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 186 | os.rmdir(support.TESTFN) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 187 | |
Antoine Pitrou | 3842529 | 2010-09-21 18:19:07 +0000 | [diff] [blame] | 188 | def check_stat_attributes(self, fname): |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 189 | if not hasattr(os, "stat"): |
| 190 | return |
| 191 | |
Antoine Pitrou | 3842529 | 2010-09-21 18:19:07 +0000 | [diff] [blame] | 192 | result = os.stat(fname) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 193 | |
| 194 | # Make sure direct access works |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 195 | self.assertEqual(result[stat.ST_SIZE], 3) |
| 196 | self.assertEqual(result.st_size, 3) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 197 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 198 | # Make sure all the attributes are there |
| 199 | members = dir(result) |
| 200 | for name in dir(stat): |
| 201 | if name[:3] == 'ST_': |
| 202 | attr = name.lower() |
Martin v. Löwis | 4d394df | 2005-01-23 09:19:22 +0000 | [diff] [blame] | 203 | if name.endswith("TIME"): |
| 204 | def trunc(x): return int(x) |
| 205 | else: |
| 206 | def trunc(x): return x |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 207 | self.assertEqual(trunc(getattr(result, attr)), |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 208 | result[getattr(stat, name)]) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 209 | self.assertIn(attr, members) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 210 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 211 | # Make sure that the st_?time and st_?time_ns fields roughly agree |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 212 | # (they should always agree up to around tens-of-microseconds) |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 213 | for name in 'st_atime st_mtime st_ctime'.split(): |
| 214 | floaty = int(getattr(result, name) * 100000) |
| 215 | nanosecondy = getattr(result, name + "_ns") // 10000 |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 216 | self.assertAlmostEqual(floaty, nanosecondy, delta=2) |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 217 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 218 | try: |
| 219 | result[200] |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 220 | self.fail("No exception raised") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 221 | except IndexError: |
| 222 | pass |
| 223 | |
| 224 | # Make sure that assignment fails |
| 225 | try: |
| 226 | result.st_mode = 1 |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 227 | self.fail("No exception raised") |
Collin Winter | 42dae6a | 2007-03-28 21:44:53 +0000 | [diff] [blame] | 228 | except AttributeError: |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 229 | pass |
| 230 | |
| 231 | try: |
| 232 | result.st_rdev = 1 |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 233 | self.fail("No exception raised") |
Guido van Rossum | 1fff878 | 2001-10-18 21:19:31 +0000 | [diff] [blame] | 234 | except (AttributeError, TypeError): |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 235 | pass |
| 236 | |
| 237 | try: |
| 238 | result.parrot = 1 |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 239 | self.fail("No exception raised") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 240 | except AttributeError: |
| 241 | pass |
| 242 | |
| 243 | # Use the stat_result constructor with a too-short tuple. |
| 244 | try: |
| 245 | result2 = os.stat_result((10,)) |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 246 | self.fail("No exception raised") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 247 | except TypeError: |
| 248 | pass |
| 249 | |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 250 | # Use the constructor with a too-long tuple. |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 251 | try: |
| 252 | result2 = os.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)) |
| 253 | except TypeError: |
| 254 | pass |
| 255 | |
Antoine Pitrou | 3842529 | 2010-09-21 18:19:07 +0000 | [diff] [blame] | 256 | def test_stat_attributes(self): |
| 257 | self.check_stat_attributes(self.fname) |
| 258 | |
| 259 | def test_stat_attributes_bytes(self): |
| 260 | try: |
| 261 | fname = self.fname.encode(sys.getfilesystemencoding()) |
| 262 | except UnicodeEncodeError: |
| 263 | self.skipTest("cannot encode %a for the filesystem" % self.fname) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 264 | with warnings.catch_warnings(): |
| 265 | warnings.simplefilter("ignore", DeprecationWarning) |
| 266 | self.check_stat_attributes(fname) |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 267 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame^] | 268 | def test_stat_result_pickle(self): |
| 269 | result = os.stat(self.fname) |
| 270 | p = pickle.dumps(result) |
| 271 | self.assertIn(b'\x03cos\nstat_result\n', p) |
| 272 | unpickled = pickle.loads(p) |
| 273 | self.assertEqual(result, unpickled) |
| 274 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 275 | def test_statvfs_attributes(self): |
| 276 | if not hasattr(os, "statvfs"): |
| 277 | return |
| 278 | |
Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 279 | try: |
| 280 | result = os.statvfs(self.fname) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 281 | except OSError as e: |
Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 282 | # On AtheOS, glibc always returns ENOSYS |
Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 283 | if e.errno == errno.ENOSYS: |
| 284 | return |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 285 | |
| 286 | # Make sure direct access works |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 287 | self.assertEqual(result.f_bfree, result[3]) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 288 | |
Brett Cannon | cfaf10c | 2008-05-16 00:45:35 +0000 | [diff] [blame] | 289 | # Make sure all the attributes are there. |
| 290 | members = ('bsize', 'frsize', 'blocks', 'bfree', 'bavail', 'files', |
| 291 | 'ffree', 'favail', 'flag', 'namemax') |
| 292 | for value, member in enumerate(members): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 293 | self.assertEqual(getattr(result, 'f_' + member), result[value]) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 294 | |
| 295 | # Make sure that assignment really fails |
| 296 | try: |
| 297 | result.f_bfree = 1 |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 298 | self.fail("No exception raised") |
Collin Winter | 42dae6a | 2007-03-28 21:44:53 +0000 | [diff] [blame] | 299 | except AttributeError: |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 300 | pass |
| 301 | |
| 302 | try: |
| 303 | result.parrot = 1 |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 304 | self.fail("No exception raised") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 305 | except AttributeError: |
| 306 | pass |
| 307 | |
| 308 | # Use the constructor with a too-short tuple. |
| 309 | try: |
| 310 | result2 = os.statvfs_result((10,)) |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 311 | self.fail("No exception raised") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 312 | except TypeError: |
| 313 | pass |
| 314 | |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 315 | # Use the constructor with a too-long tuple. |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 316 | try: |
| 317 | result2 = os.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)) |
| 318 | except TypeError: |
| 319 | pass |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 320 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame^] | 321 | @unittest.skipUnless(hasattr(os, 'statvfs'), |
| 322 | "need os.statvfs()") |
| 323 | def test_statvfs_result_pickle(self): |
| 324 | try: |
| 325 | result = os.statvfs(self.fname) |
| 326 | except OSError as e: |
| 327 | # On AtheOS, glibc always returns ENOSYS |
| 328 | if e.errno == errno.ENOSYS: |
| 329 | return |
| 330 | p = pickle.dumps(result) |
| 331 | self.assertIn(b'\x03cos\nstatvfs_result\n', p) |
| 332 | unpickled = pickle.loads(p) |
| 333 | self.assertEqual(result, unpickled) |
| 334 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 335 | def test_utime_dir(self): |
| 336 | delta = 1000000 |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 337 | st = os.stat(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 338 | # round to int, because some systems may support sub-second |
| 339 | # time stamps in stat, but not in utime. |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 340 | os.utime(support.TESTFN, (st.st_atime, int(st.st_mtime-delta))) |
| 341 | st2 = os.stat(support.TESTFN) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 342 | self.assertEqual(st2.st_mtime, int(st.st_mtime-delta)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 343 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 344 | def _test_utime(self, filename, attr, utime, delta): |
Brian Curtin | 0277aa3 | 2011-11-06 13:50:15 -0600 | [diff] [blame] | 345 | # Issue #13327 removed the requirement to pass None as the |
Brian Curtin | 52fbea1 | 2011-11-06 13:41:17 -0600 | [diff] [blame] | 346 | # second argument. Check that the previous methods of passing |
| 347 | # a time tuple or None work in addition to no argument. |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 348 | st0 = os.stat(filename) |
Brian Curtin | 52fbea1 | 2011-11-06 13:41:17 -0600 | [diff] [blame] | 349 | # Doesn't set anything new, but sets the time tuple way |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 350 | utime(filename, (attr(st0, "st_atime"), attr(st0, "st_mtime"))) |
| 351 | # Setting the time to the time you just read, then reading again, |
| 352 | # should always return exactly the same times. |
| 353 | st1 = os.stat(filename) |
| 354 | self.assertEqual(attr(st0, "st_mtime"), attr(st1, "st_mtime")) |
| 355 | self.assertEqual(attr(st0, "st_atime"), attr(st1, "st_atime")) |
Brian Curtin | 52fbea1 | 2011-11-06 13:41:17 -0600 | [diff] [blame] | 356 | # Set to the current time in the old explicit way. |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 357 | os.utime(filename, None) |
Brian Curtin | 52fbea1 | 2011-11-06 13:41:17 -0600 | [diff] [blame] | 358 | st2 = os.stat(support.TESTFN) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 359 | # Set to the current time in the new way |
| 360 | os.utime(filename) |
| 361 | st3 = os.stat(filename) |
| 362 | self.assertAlmostEqual(attr(st2, "st_mtime"), attr(st3, "st_mtime"), delta=delta) |
| 363 | |
| 364 | def test_utime(self): |
| 365 | def utime(file, times): |
| 366 | return os.utime(file, times) |
| 367 | self._test_utime(self.fname, getattr, utime, 10) |
| 368 | self._test_utime(support.TESTFN, getattr, utime, 10) |
| 369 | |
| 370 | |
| 371 | def _test_utime_ns(self, set_times_ns, test_dir=True): |
| 372 | def getattr_ns(o, attr): |
| 373 | return getattr(o, attr + "_ns") |
| 374 | ten_s = 10 * 1000 * 1000 * 1000 |
| 375 | self._test_utime(self.fname, getattr_ns, set_times_ns, ten_s) |
| 376 | if test_dir: |
| 377 | self._test_utime(support.TESTFN, getattr_ns, set_times_ns, ten_s) |
| 378 | |
| 379 | def test_utime_ns(self): |
| 380 | def utime_ns(file, times): |
| 381 | return os.utime(file, ns=times) |
| 382 | self._test_utime_ns(utime_ns) |
| 383 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 384 | requires_utime_dir_fd = unittest.skipUnless( |
| 385 | os.utime in os.supports_dir_fd, |
| 386 | "dir_fd support for utime required for this test.") |
| 387 | requires_utime_fd = unittest.skipUnless( |
| 388 | os.utime in os.supports_fd, |
| 389 | "fd support for utime required for this test.") |
| 390 | requires_utime_nofollow_symlinks = unittest.skipUnless( |
| 391 | os.utime in os.supports_follow_symlinks, |
| 392 | "follow_symlinks support for utime required for this test.") |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 393 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 394 | @requires_utime_nofollow_symlinks |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 395 | def test_lutimes_ns(self): |
| 396 | def lutimes_ns(file, times): |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 397 | return os.utime(file, ns=times, follow_symlinks=False) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 398 | self._test_utime_ns(lutimes_ns) |
| 399 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 400 | @requires_utime_fd |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 401 | def test_futimes_ns(self): |
| 402 | def futimes_ns(file, times): |
| 403 | with open(file, "wb") as f: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 404 | os.utime(f.fileno(), ns=times) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 405 | self._test_utime_ns(futimes_ns, test_dir=False) |
| 406 | |
| 407 | def _utime_invalid_arguments(self, name, arg): |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 408 | with self.assertRaises(ValueError): |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 409 | getattr(os, name)(arg, (5, 5), ns=(5, 5)) |
| 410 | |
| 411 | def test_utime_invalid_arguments(self): |
| 412 | self._utime_invalid_arguments('utime', self.fname) |
| 413 | |
Brian Curtin | 52fbea1 | 2011-11-06 13:41:17 -0600 | [diff] [blame] | 414 | |
Victor Stinner | 1aa54a4 | 2012-02-08 04:09:37 +0100 | [diff] [blame] | 415 | @unittest.skipUnless(stat_supports_subsecond, |
| 416 | "os.stat() doesn't has a subsecond resolution") |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 417 | def _test_utime_subsecond(self, set_time_func): |
Victor Stinner | be557de | 2012-02-08 03:01:11 +0100 | [diff] [blame] | 418 | asec, amsec = 1, 901 |
| 419 | atime = asec + amsec * 1e-3 |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 420 | msec, mmsec = 2, 901 |
Victor Stinner | be557de | 2012-02-08 03:01:11 +0100 | [diff] [blame] | 421 | mtime = msec + mmsec * 1e-3 |
| 422 | filename = self.fname |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 423 | os.utime(filename, (0, 0)) |
| 424 | set_time_func(filename, atime, mtime) |
Victor Stinner | 034d0aa | 2012-06-05 01:22:15 +0200 | [diff] [blame] | 425 | with warnings.catch_warnings(): |
| 426 | warnings.simplefilter("ignore", DeprecationWarning) |
| 427 | os.stat_float_times(True) |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 428 | st = os.stat(filename) |
| 429 | self.assertAlmostEqual(st.st_atime, atime, places=3) |
| 430 | self.assertAlmostEqual(st.st_mtime, mtime, places=3) |
Victor Stinner | be557de | 2012-02-08 03:01:11 +0100 | [diff] [blame] | 431 | |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 432 | def test_utime_subsecond(self): |
| 433 | def set_time(filename, atime, mtime): |
| 434 | os.utime(filename, (atime, mtime)) |
| 435 | self._test_utime_subsecond(set_time) |
| 436 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 437 | @requires_utime_fd |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 438 | def test_futimes_subsecond(self): |
| 439 | def set_time(filename, atime, mtime): |
| 440 | with open(filename, "wb") as f: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 441 | os.utime(f.fileno(), times=(atime, mtime)) |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 442 | self._test_utime_subsecond(set_time) |
| 443 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 444 | @requires_utime_fd |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 445 | def test_futimens_subsecond(self): |
| 446 | def set_time(filename, atime, mtime): |
| 447 | with open(filename, "wb") as f: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 448 | os.utime(f.fileno(), times=(atime, mtime)) |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 449 | self._test_utime_subsecond(set_time) |
| 450 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 451 | @requires_utime_dir_fd |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 452 | def test_futimesat_subsecond(self): |
| 453 | def set_time(filename, atime, mtime): |
| 454 | dirname = os.path.dirname(filename) |
| 455 | dirfd = os.open(dirname, os.O_RDONLY) |
| 456 | try: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 457 | os.utime(os.path.basename(filename), dir_fd=dirfd, |
| 458 | times=(atime, mtime)) |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 459 | finally: |
| 460 | os.close(dirfd) |
| 461 | self._test_utime_subsecond(set_time) |
| 462 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 463 | @requires_utime_nofollow_symlinks |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 464 | def test_lutimes_subsecond(self): |
| 465 | def set_time(filename, atime, mtime): |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 466 | os.utime(filename, (atime, mtime), follow_symlinks=False) |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 467 | self._test_utime_subsecond(set_time) |
| 468 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 469 | @requires_utime_dir_fd |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 470 | def test_utimensat_subsecond(self): |
| 471 | def set_time(filename, atime, mtime): |
| 472 | dirname = os.path.dirname(filename) |
| 473 | dirfd = os.open(dirname, os.O_RDONLY) |
| 474 | try: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 475 | os.utime(os.path.basename(filename), dir_fd=dirfd, |
| 476 | times=(atime, mtime)) |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 477 | finally: |
| 478 | os.close(dirfd) |
| 479 | self._test_utime_subsecond(set_time) |
Victor Stinner | be557de | 2012-02-08 03:01:11 +0100 | [diff] [blame] | 480 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 481 | # Restrict test to Win32, since there is no guarantee other |
| 482 | # systems support centiseconds |
| 483 | if sys.platform == 'win32': |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 484 | def get_file_system(path): |
Hirokazu Yamamoto | 5ef6d18 | 2008-08-20 04:17:24 +0000 | [diff] [blame] | 485 | root = os.path.splitdrive(os.path.abspath(path))[0] + '\\' |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 486 | import ctypes |
Hirokazu Yamamoto | ca765d5 | 2008-08-20 16:18:19 +0000 | [diff] [blame] | 487 | kernel32 = ctypes.windll.kernel32 |
Hirokazu Yamamoto | 5ef6d18 | 2008-08-20 04:17:24 +0000 | [diff] [blame] | 488 | buf = ctypes.create_unicode_buffer("", 100) |
Hirokazu Yamamoto | ca765d5 | 2008-08-20 16:18:19 +0000 | [diff] [blame] | 489 | if kernel32.GetVolumeInformationW(root, None, 0, None, None, None, buf, len(buf)): |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 490 | return buf.value |
| 491 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 492 | if get_file_system(support.TESTFN) == "NTFS": |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 493 | def test_1565150(self): |
| 494 | t1 = 1159195039.25 |
| 495 | os.utime(self.fname, (t1, t1)) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 496 | self.assertEqual(os.stat(self.fname).st_mtime, t1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 497 | |
Amaury Forgeot d'Arc | a251a85 | 2011-01-03 00:19:11 +0000 | [diff] [blame] | 498 | def test_large_time(self): |
| 499 | t1 = 5000000000 # some day in 2128 |
| 500 | os.utime(self.fname, (t1, t1)) |
| 501 | self.assertEqual(os.stat(self.fname).st_mtime, t1) |
| 502 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 503 | def test_1686475(self): |
| 504 | # Verify that an open file can be stat'ed |
| 505 | try: |
| 506 | os.stat(r"c:\pagefile.sys") |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 507 | except FileNotFoundError: |
| 508 | pass # file does not exist; cannot run test |
| 509 | except OSError as e: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 510 | self.fail("Could not stat pagefile.sys") |
| 511 | |
Richard Oudkerk | 2240ac1 | 2012-07-06 12:05:32 +0100 | [diff] [blame] | 512 | @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()") |
| 513 | def test_15261(self): |
| 514 | # Verify that stat'ing a closed fd does not cause crash |
| 515 | r, w = os.pipe() |
| 516 | try: |
| 517 | os.stat(r) # should not raise error |
| 518 | finally: |
| 519 | os.close(r) |
| 520 | os.close(w) |
| 521 | with self.assertRaises(OSError) as ctx: |
| 522 | os.stat(r) |
| 523 | self.assertEqual(ctx.exception.errno, errno.EBADF) |
| 524 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 525 | from test import mapping_tests |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 526 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 527 | class EnvironTests(mapping_tests.BasicTestMappingProtocol): |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 528 | """check that os.environ object conform to mapping protocol""" |
Walter Dörwald | 118f931 | 2004-06-02 18:42:25 +0000 | [diff] [blame] | 529 | type2test = None |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 530 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 531 | def setUp(self): |
| 532 | self.__save = dict(os.environ) |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 533 | if os.supports_bytes_environ: |
Victor Stinner | 208d28c | 2010-05-07 00:54:14 +0000 | [diff] [blame] | 534 | self.__saveb = dict(os.environb) |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 535 | for key, value in self._reference().items(): |
| 536 | os.environ[key] = value |
| 537 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 538 | def tearDown(self): |
| 539 | os.environ.clear() |
| 540 | os.environ.update(self.__save) |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 541 | if os.supports_bytes_environ: |
Victor Stinner | 208d28c | 2010-05-07 00:54:14 +0000 | [diff] [blame] | 542 | os.environb.clear() |
| 543 | os.environb.update(self.__saveb) |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 544 | |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 545 | def _reference(self): |
| 546 | return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"} |
| 547 | |
| 548 | def _empty_mapping(self): |
| 549 | os.environ.clear() |
| 550 | return os.environ |
| 551 | |
Martin v. Löwis | 1d11de6 | 2005-01-29 13:29:23 +0000 | [diff] [blame] | 552 | # Bug 1110478 |
Ezio Melotti | c7e139b | 2012-09-26 20:01:34 +0300 | [diff] [blame] | 553 | @unittest.skipUnless(os.path.exists('/bin/sh'), 'requires /bin/sh') |
Martin v. Löwis | 5510f65 | 2005-02-17 21:23:20 +0000 | [diff] [blame] | 554 | def test_update2(self): |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 555 | os.environ.clear() |
Ezio Melotti | c7e139b | 2012-09-26 20:01:34 +0300 | [diff] [blame] | 556 | os.environ.update(HELLO="World") |
| 557 | with os.popen("/bin/sh -c 'echo $HELLO'") as popen: |
| 558 | value = popen.read().strip() |
| 559 | self.assertEqual(value, "World") |
Martin v. Löwis | 1d11de6 | 2005-01-29 13:29:23 +0000 | [diff] [blame] | 560 | |
Ezio Melotti | c7e139b | 2012-09-26 20:01:34 +0300 | [diff] [blame] | 561 | @unittest.skipUnless(os.path.exists('/bin/sh'), 'requires /bin/sh') |
Christian Heimes | 1a13d59 | 2007-11-08 14:16:55 +0000 | [diff] [blame] | 562 | def test_os_popen_iter(self): |
Ezio Melotti | c7e139b | 2012-09-26 20:01:34 +0300 | [diff] [blame] | 563 | with os.popen( |
| 564 | "/bin/sh -c 'echo \"line1\nline2\nline3\"'") as popen: |
| 565 | it = iter(popen) |
| 566 | self.assertEqual(next(it), "line1\n") |
| 567 | self.assertEqual(next(it), "line2\n") |
| 568 | self.assertEqual(next(it), "line3\n") |
| 569 | self.assertRaises(StopIteration, next, it) |
Christian Heimes | 1a13d59 | 2007-11-08 14:16:55 +0000 | [diff] [blame] | 570 | |
Guido van Rossum | 67aca9e | 2007-06-13 21:51:27 +0000 | [diff] [blame] | 571 | # Verify environ keys and values from the OS are of the |
| 572 | # correct str type. |
| 573 | def test_keyvalue_types(self): |
| 574 | for key, val in os.environ.items(): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 575 | self.assertEqual(type(key), str) |
| 576 | self.assertEqual(type(val), str) |
Guido van Rossum | 67aca9e | 2007-06-13 21:51:27 +0000 | [diff] [blame] | 577 | |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame] | 578 | def test_items(self): |
| 579 | for key, value in self._reference().items(): |
| 580 | self.assertEqual(os.environ.get(key), value) |
| 581 | |
Ezio Melotti | 19e4acf | 2010-02-22 15:59:01 +0000 | [diff] [blame] | 582 | # Issue 7310 |
| 583 | def test___repr__(self): |
| 584 | """Check that the repr() of os.environ looks like environ({...}).""" |
| 585 | env = os.environ |
Victor Stinner | 96f0de9 | 2010-07-29 00:29:00 +0000 | [diff] [blame] | 586 | self.assertEqual(repr(env), 'environ({{{}}})'.format(', '.join( |
| 587 | '{!r}: {!r}'.format(key, value) |
| 588 | for key, value in env.items()))) |
Ezio Melotti | 19e4acf | 2010-02-22 15:59:01 +0000 | [diff] [blame] | 589 | |
Gregory P. Smith | b6e8c7e | 2010-02-27 07:22:22 +0000 | [diff] [blame] | 590 | def test_get_exec_path(self): |
| 591 | defpath_list = os.defpath.split(os.pathsep) |
| 592 | test_path = ['/monty', '/python', '', '/flying/circus'] |
| 593 | test_env = {'PATH': os.pathsep.join(test_path)} |
| 594 | |
| 595 | saved_environ = os.environ |
| 596 | try: |
| 597 | os.environ = dict(test_env) |
| 598 | # Test that defaulting to os.environ works. |
| 599 | self.assertSequenceEqual(test_path, os.get_exec_path()) |
| 600 | self.assertSequenceEqual(test_path, os.get_exec_path(env=None)) |
| 601 | finally: |
| 602 | os.environ = saved_environ |
| 603 | |
| 604 | # No PATH environment variable |
| 605 | self.assertSequenceEqual(defpath_list, os.get_exec_path({})) |
| 606 | # Empty PATH environment variable |
| 607 | self.assertSequenceEqual(('',), os.get_exec_path({'PATH':''})) |
| 608 | # Supplied PATH environment variable |
| 609 | self.assertSequenceEqual(test_path, os.get_exec_path(test_env)) |
| 610 | |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 611 | if os.supports_bytes_environ: |
| 612 | # env cannot contain 'PATH' and b'PATH' keys |
Victor Stinner | 38430e2 | 2010-08-19 17:10:18 +0000 | [diff] [blame] | 613 | try: |
Victor Stinner | 6f35eda | 2010-10-29 00:38:58 +0000 | [diff] [blame] | 614 | # ignore BytesWarning warning |
| 615 | with warnings.catch_warnings(record=True): |
| 616 | mixed_env = {'PATH': '1', b'PATH': b'2'} |
Victor Stinner | 38430e2 | 2010-08-19 17:10:18 +0000 | [diff] [blame] | 617 | except BytesWarning: |
Victor Stinner | 6f35eda | 2010-10-29 00:38:58 +0000 | [diff] [blame] | 618 | # mixed_env cannot be created with python -bb |
Victor Stinner | 38430e2 | 2010-08-19 17:10:18 +0000 | [diff] [blame] | 619 | pass |
| 620 | else: |
| 621 | self.assertRaises(ValueError, os.get_exec_path, mixed_env) |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 622 | |
| 623 | # bytes key and/or value |
| 624 | self.assertSequenceEqual(os.get_exec_path({b'PATH': b'abc'}), |
| 625 | ['abc']) |
| 626 | self.assertSequenceEqual(os.get_exec_path({b'PATH': 'abc'}), |
| 627 | ['abc']) |
| 628 | self.assertSequenceEqual(os.get_exec_path({'PATH': b'abc'}), |
| 629 | ['abc']) |
| 630 | |
| 631 | @unittest.skipUnless(os.supports_bytes_environ, |
| 632 | "os.environb required for this test.") |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 633 | def test_environb(self): |
| 634 | # os.environ -> os.environb |
| 635 | value = 'euro\u20ac' |
| 636 | try: |
Benjamin Peterson | 180799d | 2010-05-06 22:25:42 +0000 | [diff] [blame] | 637 | value_bytes = value.encode(sys.getfilesystemencoding(), |
| 638 | 'surrogateescape') |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 639 | except UnicodeEncodeError: |
Benjamin Peterson | 180799d | 2010-05-06 22:25:42 +0000 | [diff] [blame] | 640 | msg = "U+20AC character is not encodable to %s" % ( |
| 641 | sys.getfilesystemencoding(),) |
Benjamin Peterson | 932d3f4 | 2010-05-06 22:26:31 +0000 | [diff] [blame] | 642 | self.skipTest(msg) |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 643 | os.environ['unicode'] = value |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 644 | self.assertEqual(os.environ['unicode'], value) |
| 645 | self.assertEqual(os.environb[b'unicode'], value_bytes) |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 646 | |
| 647 | # os.environb -> os.environ |
| 648 | value = b'\xff' |
| 649 | os.environb[b'bytes'] = value |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 650 | self.assertEqual(os.environb[b'bytes'], value) |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 651 | value_str = value.decode(sys.getfilesystemencoding(), 'surrogateescape') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 652 | self.assertEqual(os.environ['bytes'], value_str) |
Ezio Melotti | 19e4acf | 2010-02-22 15:59:01 +0000 | [diff] [blame] | 653 | |
Charles-François Natali | 2966f10 | 2011-11-26 11:32:46 +0100 | [diff] [blame] | 654 | # On FreeBSD < 7 and OS X < 10.6, unsetenv() doesn't return a value (issue |
| 655 | # #13415). |
| 656 | @support.requires_freebsd_version(7) |
| 657 | @support.requires_mac_ver(10, 6) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 658 | def test_unset_error(self): |
| 659 | if sys.platform == "win32": |
| 660 | # an environment variable is limited to 32,767 characters |
| 661 | key = 'x' * 50000 |
Victor Stinner | b3f8268 | 2011-11-22 22:30:19 +0100 | [diff] [blame] | 662 | self.assertRaises(ValueError, os.environ.__delitem__, key) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 663 | else: |
| 664 | # "=" is not allowed in a variable name |
| 665 | key = 'key=' |
Victor Stinner | b3f8268 | 2011-11-22 22:30:19 +0100 | [diff] [blame] | 666 | self.assertRaises(OSError, os.environ.__delitem__, key) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 667 | |
Victor Stinner | 6d10139 | 2013-04-14 16:35:04 +0200 | [diff] [blame] | 668 | def test_key_type(self): |
| 669 | missing = 'missingkey' |
| 670 | self.assertNotIn(missing, os.environ) |
| 671 | |
Victor Stinner | 839e5ea | 2013-04-14 16:43:03 +0200 | [diff] [blame] | 672 | with self.assertRaises(KeyError) as cm: |
Victor Stinner | 6d10139 | 2013-04-14 16:35:04 +0200 | [diff] [blame] | 673 | os.environ[missing] |
Victor Stinner | 839e5ea | 2013-04-14 16:43:03 +0200 | [diff] [blame] | 674 | self.assertIs(cm.exception.args[0], missing) |
Victor Stinner | 0c2dd0c | 2013-08-23 19:19:15 +0200 | [diff] [blame] | 675 | self.assertTrue(cm.exception.__suppress_context__) |
Victor Stinner | 6d10139 | 2013-04-14 16:35:04 +0200 | [diff] [blame] | 676 | |
Victor Stinner | 839e5ea | 2013-04-14 16:43:03 +0200 | [diff] [blame] | 677 | with self.assertRaises(KeyError) as cm: |
Victor Stinner | 6d10139 | 2013-04-14 16:35:04 +0200 | [diff] [blame] | 678 | del os.environ[missing] |
Victor Stinner | 839e5ea | 2013-04-14 16:43:03 +0200 | [diff] [blame] | 679 | self.assertIs(cm.exception.args[0], missing) |
Victor Stinner | 0c2dd0c | 2013-08-23 19:19:15 +0200 | [diff] [blame] | 680 | self.assertTrue(cm.exception.__suppress_context__) |
| 681 | |
Victor Stinner | 6d10139 | 2013-04-14 16:35:04 +0200 | [diff] [blame] | 682 | |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 683 | class WalkTests(unittest.TestCase): |
| 684 | """Tests for os.walk().""" |
| 685 | |
Charles-François Natali | 7372b06 | 2012-02-05 15:15:38 +0100 | [diff] [blame] | 686 | def setUp(self): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 687 | import os |
| 688 | from os.path import join |
| 689 | |
| 690 | # Build: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 691 | # TESTFN/ |
| 692 | # TEST1/ a file kid and two directory kids |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 693 | # tmp1 |
| 694 | # SUB1/ a file kid and a directory kid |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 695 | # tmp2 |
| 696 | # SUB11/ no kids |
| 697 | # SUB2/ a file kid and a dirsymlink kid |
| 698 | # tmp3 |
| 699 | # link/ a symlink to TESTFN.2 |
Hynek Schlawack | 66bfcc1 | 2012-05-15 16:32:21 +0200 | [diff] [blame] | 700 | # broken_link |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 701 | # TEST2/ |
| 702 | # tmp4 a lone file |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 703 | walk_path = join(support.TESTFN, "TEST1") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 704 | sub1_path = join(walk_path, "SUB1") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 705 | sub11_path = join(sub1_path, "SUB11") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 706 | sub2_path = join(walk_path, "SUB2") |
| 707 | tmp1_path = join(walk_path, "tmp1") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 708 | tmp2_path = join(sub1_path, "tmp2") |
| 709 | tmp3_path = join(sub2_path, "tmp3") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 710 | link_path = join(sub2_path, "link") |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 711 | t2_path = join(support.TESTFN, "TEST2") |
| 712 | tmp4_path = join(support.TESTFN, "TEST2", "tmp4") |
Hynek Schlawack | 66bfcc1 | 2012-05-15 16:32:21 +0200 | [diff] [blame] | 713 | link_path = join(sub2_path, "link") |
| 714 | broken_link_path = join(sub2_path, "broken_link") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 715 | |
| 716 | # Create stuff. |
| 717 | os.makedirs(sub11_path) |
| 718 | os.makedirs(sub2_path) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 719 | os.makedirs(t2_path) |
| 720 | for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path: |
Alex Martelli | 01c77c6 | 2006-08-24 02:58:11 +0000 | [diff] [blame] | 721 | f = open(path, "w") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 722 | f.write("I'm " + path + " and proud of it. Blame test_os.\n") |
| 723 | f.close() |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 724 | if support.can_symlink(): |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 725 | os.symlink(os.path.abspath(t2_path), link_path) |
Jason R. Coombs | b501b56 | 2013-05-27 23:52:43 -0400 | [diff] [blame] | 726 | os.symlink('broken', broken_link_path, True) |
Hynek Schlawack | 66bfcc1 | 2012-05-15 16:32:21 +0200 | [diff] [blame] | 727 | sub2_tree = (sub2_path, ["link"], ["broken_link", "tmp3"]) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 728 | else: |
| 729 | sub2_tree = (sub2_path, [], ["tmp3"]) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 730 | |
| 731 | # Walk top-down. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 732 | all = list(os.walk(walk_path)) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 733 | self.assertEqual(len(all), 4) |
| 734 | # We can't know which order SUB1 and SUB2 will appear in. |
| 735 | # Not flipped: TESTFN, SUB1, SUB11, SUB2 |
| 736 | # flipped: TESTFN, SUB2, SUB1, SUB11 |
| 737 | flipped = all[0][1][0] != "SUB1" |
| 738 | all[0][1].sort() |
Hynek Schlawack | c96f5a0 | 2012-05-15 17:55:38 +0200 | [diff] [blame] | 739 | all[3 - 2 * flipped][-1].sort() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 740 | self.assertEqual(all[0], (walk_path, ["SUB1", "SUB2"], ["tmp1"])) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 741 | self.assertEqual(all[1 + flipped], (sub1_path, ["SUB11"], ["tmp2"])) |
| 742 | self.assertEqual(all[2 + flipped], (sub11_path, [], [])) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 743 | self.assertEqual(all[3 - 2 * flipped], sub2_tree) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 744 | |
| 745 | # Prune the search. |
| 746 | all = [] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 747 | for root, dirs, files in os.walk(walk_path): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 748 | all.append((root, dirs, files)) |
| 749 | # Don't descend into SUB1. |
| 750 | if 'SUB1' in dirs: |
| 751 | # Note that this also mutates the dirs we appended to all! |
| 752 | dirs.remove('SUB1') |
| 753 | self.assertEqual(len(all), 2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 754 | self.assertEqual(all[0], (walk_path, ["SUB2"], ["tmp1"])) |
Hynek Schlawack | 39bf90d | 2012-05-15 18:40:17 +0200 | [diff] [blame] | 755 | all[1][-1].sort() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 756 | self.assertEqual(all[1], sub2_tree) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 757 | |
| 758 | # Walk bottom-up. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 759 | all = list(os.walk(walk_path, topdown=False)) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 760 | self.assertEqual(len(all), 4) |
| 761 | # We can't know which order SUB1 and SUB2 will appear in. |
| 762 | # Not flipped: SUB11, SUB1, SUB2, TESTFN |
| 763 | # flipped: SUB2, SUB11, SUB1, TESTFN |
| 764 | flipped = all[3][1][0] != "SUB1" |
| 765 | all[3][1].sort() |
Hynek Schlawack | 39bf90d | 2012-05-15 18:40:17 +0200 | [diff] [blame] | 766 | all[2 - 2 * flipped][-1].sort() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 767 | self.assertEqual(all[3], (walk_path, ["SUB1", "SUB2"], ["tmp1"])) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 768 | self.assertEqual(all[flipped], (sub11_path, [], [])) |
| 769 | self.assertEqual(all[flipped + 1], (sub1_path, ["SUB11"], ["tmp2"])) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 770 | self.assertEqual(all[2 - 2 * flipped], sub2_tree) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 771 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 772 | if support.can_symlink(): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 773 | # Walk, following symlinks. |
| 774 | for root, dirs, files in os.walk(walk_path, followlinks=True): |
| 775 | if root == link_path: |
| 776 | self.assertEqual(dirs, []) |
| 777 | self.assertEqual(files, ["tmp4"]) |
| 778 | break |
| 779 | else: |
| 780 | self.fail("Didn't follow symlink with followlinks=True") |
| 781 | |
| 782 | def tearDown(self): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 783 | # Tear everything down. This is a decent use for bottom-up on |
| 784 | # Windows, which doesn't have a recursive delete command. The |
| 785 | # (not so) subtlety is that rmdir will fail unless the dir's |
| 786 | # kids are removed first, so bottom up is essential. |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 787 | for root, dirs, files in os.walk(support.TESTFN, topdown=False): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 788 | for name in files: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 789 | os.remove(os.path.join(root, name)) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 790 | for name in dirs: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 791 | dirname = os.path.join(root, name) |
| 792 | if not os.path.islink(dirname): |
| 793 | os.rmdir(dirname) |
| 794 | else: |
| 795 | os.remove(dirname) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 796 | os.rmdir(support.TESTFN) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 797 | |
Charles-François Natali | 7372b06 | 2012-02-05 15:15:38 +0100 | [diff] [blame] | 798 | |
| 799 | @unittest.skipUnless(hasattr(os, 'fwalk'), "Test needs os.fwalk()") |
| 800 | class FwalkTests(WalkTests): |
| 801 | """Tests for os.fwalk().""" |
| 802 | |
Larry Hastings | c48fe98 | 2012-06-25 04:49:05 -0700 | [diff] [blame] | 803 | def _compare_to_walk(self, walk_kwargs, fwalk_kwargs): |
| 804 | """ |
| 805 | compare with walk() results. |
| 806 | """ |
Larry Hastings | b403806 | 2012-07-15 10:57:38 -0700 | [diff] [blame] | 807 | walk_kwargs = walk_kwargs.copy() |
| 808 | fwalk_kwargs = fwalk_kwargs.copy() |
| 809 | for topdown, follow_symlinks in itertools.product((True, False), repeat=2): |
| 810 | walk_kwargs.update(topdown=topdown, followlinks=follow_symlinks) |
| 811 | fwalk_kwargs.update(topdown=topdown, follow_symlinks=follow_symlinks) |
Larry Hastings | c48fe98 | 2012-06-25 04:49:05 -0700 | [diff] [blame] | 812 | |
Charles-François Natali | 7372b06 | 2012-02-05 15:15:38 +0100 | [diff] [blame] | 813 | expected = {} |
Larry Hastings | c48fe98 | 2012-06-25 04:49:05 -0700 | [diff] [blame] | 814 | for root, dirs, files in os.walk(**walk_kwargs): |
Charles-François Natali | 7372b06 | 2012-02-05 15:15:38 +0100 | [diff] [blame] | 815 | expected[root] = (set(dirs), set(files)) |
| 816 | |
Larry Hastings | c48fe98 | 2012-06-25 04:49:05 -0700 | [diff] [blame] | 817 | for root, dirs, files, rootfd in os.fwalk(**fwalk_kwargs): |
Charles-François Natali | 7372b06 | 2012-02-05 15:15:38 +0100 | [diff] [blame] | 818 | self.assertIn(root, expected) |
| 819 | self.assertEqual(expected[root], (set(dirs), set(files))) |
| 820 | |
Larry Hastings | c48fe98 | 2012-06-25 04:49:05 -0700 | [diff] [blame] | 821 | def test_compare_to_walk(self): |
| 822 | kwargs = {'top': support.TESTFN} |
| 823 | self._compare_to_walk(kwargs, kwargs) |
| 824 | |
Charles-François Natali | 7372b06 | 2012-02-05 15:15:38 +0100 | [diff] [blame] | 825 | def test_dir_fd(self): |
Larry Hastings | c48fe98 | 2012-06-25 04:49:05 -0700 | [diff] [blame] | 826 | try: |
| 827 | fd = os.open(".", os.O_RDONLY) |
| 828 | walk_kwargs = {'top': support.TESTFN} |
| 829 | fwalk_kwargs = walk_kwargs.copy() |
| 830 | fwalk_kwargs['dir_fd'] = fd |
| 831 | self._compare_to_walk(walk_kwargs, fwalk_kwargs) |
| 832 | finally: |
| 833 | os.close(fd) |
| 834 | |
| 835 | def test_yields_correct_dir_fd(self): |
Charles-François Natali | 7372b06 | 2012-02-05 15:15:38 +0100 | [diff] [blame] | 836 | # check returned file descriptors |
Larry Hastings | b403806 | 2012-07-15 10:57:38 -0700 | [diff] [blame] | 837 | for topdown, follow_symlinks in itertools.product((True, False), repeat=2): |
| 838 | args = support.TESTFN, topdown, None |
| 839 | for root, dirs, files, rootfd in os.fwalk(*args, follow_symlinks=follow_symlinks): |
Charles-François Natali | 7372b06 | 2012-02-05 15:15:38 +0100 | [diff] [blame] | 840 | # check that the FD is valid |
| 841 | os.fstat(rootfd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 842 | # redundant check |
| 843 | os.stat(rootfd) |
| 844 | # check that listdir() returns consistent information |
| 845 | self.assertEqual(set(os.listdir(rootfd)), set(dirs) | set(files)) |
Charles-François Natali | 7372b06 | 2012-02-05 15:15:38 +0100 | [diff] [blame] | 846 | |
| 847 | def test_fd_leak(self): |
| 848 | # Since we're opening a lot of FDs, we must be careful to avoid leaks: |
| 849 | # we both check that calling fwalk() a large number of times doesn't |
| 850 | # yield EMFILE, and that the minimum allocated FD hasn't changed. |
| 851 | minfd = os.dup(1) |
| 852 | os.close(minfd) |
| 853 | for i in range(256): |
| 854 | for x in os.fwalk(support.TESTFN): |
| 855 | pass |
| 856 | newfd = os.dup(1) |
| 857 | self.addCleanup(os.close, newfd) |
| 858 | self.assertEqual(newfd, minfd) |
| 859 | |
| 860 | def tearDown(self): |
| 861 | # cleanup |
| 862 | for root, dirs, files, rootfd in os.fwalk(support.TESTFN, topdown=False): |
| 863 | for name in files: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 864 | os.unlink(name, dir_fd=rootfd) |
Charles-François Natali | 7372b06 | 2012-02-05 15:15:38 +0100 | [diff] [blame] | 865 | for name in dirs: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 866 | st = os.stat(name, dir_fd=rootfd, follow_symlinks=False) |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 867 | if stat.S_ISDIR(st.st_mode): |
| 868 | os.rmdir(name, dir_fd=rootfd) |
| 869 | else: |
| 870 | os.unlink(name, dir_fd=rootfd) |
Charles-François Natali | 7372b06 | 2012-02-05 15:15:38 +0100 | [diff] [blame] | 871 | os.rmdir(support.TESTFN) |
| 872 | |
| 873 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 874 | class MakedirTests(unittest.TestCase): |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 875 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 876 | os.mkdir(support.TESTFN) |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 877 | |
| 878 | def test_makedir(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 879 | base = support.TESTFN |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 880 | path = os.path.join(base, 'dir1', 'dir2', 'dir3') |
| 881 | os.makedirs(path) # Should work |
| 882 | path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4') |
| 883 | os.makedirs(path) |
| 884 | |
| 885 | # Try paths with a '.' in them |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 886 | self.assertRaises(OSError, os.makedirs, os.curdir) |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 887 | path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4', 'dir5', os.curdir) |
| 888 | os.makedirs(path) |
| 889 | path = os.path.join(base, 'dir1', os.curdir, 'dir2', 'dir3', 'dir4', |
| 890 | 'dir5', 'dir6') |
| 891 | os.makedirs(path) |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 892 | |
Terry Reedy | 5a22b65 | 2010-12-02 07:05:56 +0000 | [diff] [blame] | 893 | def test_exist_ok_existing_directory(self): |
| 894 | path = os.path.join(support.TESTFN, 'dir1') |
| 895 | mode = 0o777 |
| 896 | old_mask = os.umask(0o022) |
| 897 | os.makedirs(path, mode) |
| 898 | self.assertRaises(OSError, os.makedirs, path, mode) |
| 899 | self.assertRaises(OSError, os.makedirs, path, mode, exist_ok=False) |
| 900 | self.assertRaises(OSError, os.makedirs, path, 0o776, exist_ok=True) |
| 901 | os.makedirs(path, mode=mode, exist_ok=True) |
| 902 | os.umask(old_mask) |
| 903 | |
Terry Jan Reedy | 4a0b6f7 | 2013-08-10 20:58:59 -0400 | [diff] [blame] | 904 | @unittest.skipUnless(hasattr(os, 'chown'), 'test needs os.chown') |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 905 | def test_chown_uid_gid_arguments_must_be_index(self): |
| 906 | stat = os.stat(support.TESTFN) |
| 907 | uid = stat.st_uid |
| 908 | gid = stat.st_gid |
| 909 | for value in (-1.0, -1j, decimal.Decimal(-1), fractions.Fraction(-2, 2)): |
| 910 | self.assertRaises(TypeError, os.chown, support.TESTFN, value, gid) |
| 911 | self.assertRaises(TypeError, os.chown, support.TESTFN, uid, value) |
| 912 | self.assertIsNone(os.chown(support.TESTFN, uid, gid)) |
| 913 | self.assertIsNone(os.chown(support.TESTFN, -1, -1)) |
| 914 | |
Gregory P. Smith | a81c856 | 2012-06-03 14:30:44 -0700 | [diff] [blame] | 915 | def test_exist_ok_s_isgid_directory(self): |
| 916 | path = os.path.join(support.TESTFN, 'dir1') |
| 917 | S_ISGID = stat.S_ISGID |
| 918 | mode = 0o777 |
| 919 | old_mask = os.umask(0o022) |
| 920 | try: |
| 921 | existing_testfn_mode = stat.S_IMODE( |
| 922 | os.lstat(support.TESTFN).st_mode) |
Ned Deily | c622f42 | 2012-08-08 20:57:24 -0700 | [diff] [blame] | 923 | try: |
| 924 | os.chmod(support.TESTFN, existing_testfn_mode | S_ISGID) |
Ned Deily | 3a2b97e | 2012-08-08 21:03:02 -0700 | [diff] [blame] | 925 | except PermissionError: |
Ned Deily | c622f42 | 2012-08-08 20:57:24 -0700 | [diff] [blame] | 926 | raise unittest.SkipTest('Cannot set S_ISGID for dir.') |
Gregory P. Smith | a81c856 | 2012-06-03 14:30:44 -0700 | [diff] [blame] | 927 | if (os.lstat(support.TESTFN).st_mode & S_ISGID != S_ISGID): |
| 928 | raise unittest.SkipTest('No support for S_ISGID dir mode.') |
| 929 | # The os should apply S_ISGID from the parent dir for us, but |
| 930 | # this test need not depend on that behavior. Be explicit. |
| 931 | os.makedirs(path, mode | S_ISGID) |
| 932 | # http://bugs.python.org/issue14992 |
| 933 | # Should not fail when the bit is already set. |
| 934 | os.makedirs(path, mode, exist_ok=True) |
| 935 | # remove the bit. |
| 936 | os.chmod(path, stat.S_IMODE(os.lstat(path).st_mode) & ~S_ISGID) |
| 937 | with self.assertRaises(OSError): |
| 938 | # Should fail when the bit is not already set when demanded. |
| 939 | os.makedirs(path, mode | S_ISGID, exist_ok=True) |
| 940 | finally: |
| 941 | os.umask(old_mask) |
Terry Reedy | 5a22b65 | 2010-12-02 07:05:56 +0000 | [diff] [blame] | 942 | |
| 943 | def test_exist_ok_existing_regular_file(self): |
| 944 | base = support.TESTFN |
| 945 | path = os.path.join(support.TESTFN, 'dir1') |
| 946 | f = open(path, 'w') |
| 947 | f.write('abc') |
| 948 | f.close() |
| 949 | self.assertRaises(OSError, os.makedirs, path) |
| 950 | self.assertRaises(OSError, os.makedirs, path, exist_ok=False) |
| 951 | self.assertRaises(OSError, os.makedirs, path, exist_ok=True) |
| 952 | os.remove(path) |
| 953 | |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 954 | def tearDown(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 955 | path = os.path.join(support.TESTFN, 'dir1', 'dir2', 'dir3', |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 956 | 'dir4', 'dir5', 'dir6') |
| 957 | # If the tests failed, the bottom-most directory ('../dir6') |
| 958 | # may not have been created, so we look for the outermost directory |
| 959 | # that exists. |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 960 | while not os.path.exists(path) and path != support.TESTFN: |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 961 | path = os.path.dirname(path) |
| 962 | |
| 963 | os.removedirs(path) |
| 964 | |
Andrew Svetlov | 405faed | 2012-12-25 12:18:09 +0200 | [diff] [blame] | 965 | |
| 966 | class RemoveDirsTests(unittest.TestCase): |
| 967 | def setUp(self): |
| 968 | os.makedirs(support.TESTFN) |
| 969 | |
| 970 | def tearDown(self): |
| 971 | support.rmtree(support.TESTFN) |
| 972 | |
| 973 | def test_remove_all(self): |
| 974 | dira = os.path.join(support.TESTFN, 'dira') |
| 975 | os.mkdir(dira) |
| 976 | dirb = os.path.join(dira, 'dirb') |
| 977 | os.mkdir(dirb) |
| 978 | os.removedirs(dirb) |
| 979 | self.assertFalse(os.path.exists(dirb)) |
| 980 | self.assertFalse(os.path.exists(dira)) |
| 981 | self.assertFalse(os.path.exists(support.TESTFN)) |
| 982 | |
| 983 | def test_remove_partial(self): |
| 984 | dira = os.path.join(support.TESTFN, 'dira') |
| 985 | os.mkdir(dira) |
| 986 | dirb = os.path.join(dira, 'dirb') |
| 987 | os.mkdir(dirb) |
| 988 | with open(os.path.join(dira, 'file.txt'), 'w') as f: |
| 989 | f.write('text') |
| 990 | os.removedirs(dirb) |
| 991 | self.assertFalse(os.path.exists(dirb)) |
| 992 | self.assertTrue(os.path.exists(dira)) |
| 993 | self.assertTrue(os.path.exists(support.TESTFN)) |
| 994 | |
| 995 | def test_remove_nothing(self): |
| 996 | dira = os.path.join(support.TESTFN, 'dira') |
| 997 | os.mkdir(dira) |
| 998 | dirb = os.path.join(dira, 'dirb') |
| 999 | os.mkdir(dirb) |
| 1000 | with open(os.path.join(dirb, 'file.txt'), 'w') as f: |
| 1001 | f.write('text') |
| 1002 | with self.assertRaises(OSError): |
| 1003 | os.removedirs(dirb) |
| 1004 | self.assertTrue(os.path.exists(dirb)) |
| 1005 | self.assertTrue(os.path.exists(dira)) |
| 1006 | self.assertTrue(os.path.exists(support.TESTFN)) |
| 1007 | |
| 1008 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1009 | class DevNullTests(unittest.TestCase): |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 1010 | def test_devnull(self): |
Victor Stinner | a6d2c76 | 2011-06-30 18:20:11 +0200 | [diff] [blame] | 1011 | with open(os.devnull, 'wb') as f: |
| 1012 | f.write(b'hello') |
| 1013 | f.close() |
| 1014 | with open(os.devnull, 'rb') as f: |
| 1015 | self.assertEqual(f.read(), b'') |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 1016 | |
Andrew Svetlov | 405faed | 2012-12-25 12:18:09 +0200 | [diff] [blame] | 1017 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1018 | class URandomTests(unittest.TestCase): |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 1019 | def test_urandom_length(self): |
| 1020 | self.assertEqual(len(os.urandom(0)), 0) |
| 1021 | self.assertEqual(len(os.urandom(1)), 1) |
| 1022 | self.assertEqual(len(os.urandom(10)), 10) |
| 1023 | self.assertEqual(len(os.urandom(100)), 100) |
| 1024 | self.assertEqual(len(os.urandom(1000)), 1000) |
| 1025 | |
| 1026 | def test_urandom_value(self): |
| 1027 | data1 = os.urandom(16) |
| 1028 | data2 = os.urandom(16) |
| 1029 | self.assertNotEqual(data1, data2) |
| 1030 | |
| 1031 | def get_urandom_subprocess(self, count): |
| 1032 | code = '\n'.join(( |
| 1033 | 'import os, sys', |
| 1034 | 'data = os.urandom(%s)' % count, |
| 1035 | 'sys.stdout.buffer.write(data)', |
| 1036 | 'sys.stdout.buffer.flush()')) |
| 1037 | out = assert_python_ok('-c', code) |
| 1038 | stdout = out[1] |
| 1039 | self.assertEqual(len(stdout), 16) |
| 1040 | return stdout |
| 1041 | |
| 1042 | def test_urandom_subprocess(self): |
| 1043 | data1 = self.get_urandom_subprocess(16) |
| 1044 | data2 = self.get_urandom_subprocess(16) |
| 1045 | self.assertNotEqual(data1, data2) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 1046 | |
Antoine Pitrou | ec34ab5 | 2013-08-16 20:44:38 +0200 | [diff] [blame] | 1047 | @unittest.skipUnless(resource, "test requires the resource module") |
| 1048 | def test_urandom_failure(self): |
Antoine Pitrou | eba25ba | 2013-08-24 20:52:27 +0200 | [diff] [blame] | 1049 | # Check urandom() failing when it is not able to open /dev/random. |
| 1050 | # We spawn a new process to make the test more robust (if getrlimit() |
| 1051 | # failed to restore the file descriptor limit after this, the whole |
| 1052 | # test suite would crash; this actually happened on the OS X Tiger |
| 1053 | # buildbot). |
| 1054 | code = """if 1: |
| 1055 | import errno |
| 1056 | import os |
| 1057 | import resource |
| 1058 | |
| 1059 | soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE) |
| 1060 | resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit)) |
| 1061 | try: |
Antoine Pitrou | ec34ab5 | 2013-08-16 20:44:38 +0200 | [diff] [blame] | 1062 | os.urandom(16) |
Antoine Pitrou | eba25ba | 2013-08-24 20:52:27 +0200 | [diff] [blame] | 1063 | except OSError as e: |
| 1064 | assert e.errno == errno.EMFILE, e.errno |
| 1065 | else: |
| 1066 | raise AssertionError("OSError not raised") |
| 1067 | """ |
| 1068 | assert_python_ok('-c', code) |
Antoine Pitrou | ec34ab5 | 2013-08-16 20:44:38 +0200 | [diff] [blame] | 1069 | |
| 1070 | |
Victor Stinner | c2d095f | 2010-05-17 00:14:53 +0000 | [diff] [blame] | 1071 | @contextlib.contextmanager |
| 1072 | def _execvpe_mockup(defpath=None): |
| 1073 | """ |
| 1074 | Stubs out execv and execve functions when used as context manager. |
| 1075 | Records exec calls. The mock execv and execve functions always raise an |
| 1076 | exception as they would normally never return. |
| 1077 | """ |
| 1078 | # A list of tuples containing (function name, first arg, args) |
| 1079 | # of calls to execv or execve that have been made. |
| 1080 | calls = [] |
| 1081 | |
| 1082 | def mock_execv(name, *args): |
| 1083 | calls.append(('execv', name, args)) |
| 1084 | raise RuntimeError("execv called") |
| 1085 | |
| 1086 | def mock_execve(name, *args): |
| 1087 | calls.append(('execve', name, args)) |
| 1088 | raise OSError(errno.ENOTDIR, "execve called") |
| 1089 | |
| 1090 | try: |
| 1091 | orig_execv = os.execv |
| 1092 | orig_execve = os.execve |
| 1093 | orig_defpath = os.defpath |
| 1094 | os.execv = mock_execv |
| 1095 | os.execve = mock_execve |
| 1096 | if defpath is not None: |
| 1097 | os.defpath = defpath |
| 1098 | yield calls |
| 1099 | finally: |
| 1100 | os.execv = orig_execv |
| 1101 | os.execve = orig_execve |
| 1102 | os.defpath = orig_defpath |
| 1103 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1104 | class ExecTests(unittest.TestCase): |
Mark Dickinson | 7cf0389 | 2010-04-16 13:45:35 +0000 | [diff] [blame] | 1105 | @unittest.skipIf(USING_LINUXTHREADS, |
| 1106 | "avoid triggering a linuxthreads bug: see issue #4970") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1107 | def test_execvpe_with_bad_program(self): |
Mark Dickinson | 7cf0389 | 2010-04-16 13:45:35 +0000 | [diff] [blame] | 1108 | self.assertRaises(OSError, os.execvpe, 'no such app-', |
| 1109 | ['no such app-'], None) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1110 | |
Thomas Heller | 6790d60 | 2007-08-30 17:15:14 +0000 | [diff] [blame] | 1111 | def test_execvpe_with_bad_arglist(self): |
| 1112 | self.assertRaises(ValueError, os.execvpe, 'notepad', [], None) |
| 1113 | |
Gregory P. Smith | 4ae3777 | 2010-05-08 18:05:46 +0000 | [diff] [blame] | 1114 | @unittest.skipUnless(hasattr(os, '_execvpe'), |
| 1115 | "No internal os._execvpe function to test.") |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 1116 | def _test_internal_execvpe(self, test_type): |
| 1117 | program_path = os.sep + 'absolutepath' |
| 1118 | if test_type is bytes: |
| 1119 | program = b'executable' |
| 1120 | fullpath = os.path.join(os.fsencode(program_path), program) |
| 1121 | native_fullpath = fullpath |
| 1122 | arguments = [b'progname', 'arg1', 'arg2'] |
| 1123 | else: |
| 1124 | program = 'executable' |
| 1125 | arguments = ['progname', 'arg1', 'arg2'] |
| 1126 | fullpath = os.path.join(program_path, program) |
| 1127 | if os.name != "nt": |
| 1128 | native_fullpath = os.fsencode(fullpath) |
| 1129 | else: |
| 1130 | native_fullpath = fullpath |
Victor Stinner | c2d095f | 2010-05-17 00:14:53 +0000 | [diff] [blame] | 1131 | env = {'spam': 'beans'} |
| 1132 | |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 1133 | # test os._execvpe() with an absolute path |
Victor Stinner | c2d095f | 2010-05-17 00:14:53 +0000 | [diff] [blame] | 1134 | with _execvpe_mockup() as calls: |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 1135 | self.assertRaises(RuntimeError, |
| 1136 | os._execvpe, fullpath, arguments) |
Victor Stinner | c2d095f | 2010-05-17 00:14:53 +0000 | [diff] [blame] | 1137 | self.assertEqual(len(calls), 1) |
| 1138 | self.assertEqual(calls[0], ('execv', fullpath, (arguments,))) |
| 1139 | |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 1140 | # test os._execvpe() with a relative path: |
| 1141 | # os.get_exec_path() returns defpath |
Victor Stinner | c2d095f | 2010-05-17 00:14:53 +0000 | [diff] [blame] | 1142 | with _execvpe_mockup(defpath=program_path) as calls: |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 1143 | self.assertRaises(OSError, |
| 1144 | os._execvpe, program, arguments, env=env) |
Victor Stinner | c2d095f | 2010-05-17 00:14:53 +0000 | [diff] [blame] | 1145 | self.assertEqual(len(calls), 1) |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 1146 | self.assertSequenceEqual(calls[0], |
| 1147 | ('execve', native_fullpath, (arguments, env))) |
| 1148 | |
| 1149 | # test os._execvpe() with a relative path: |
| 1150 | # os.get_exec_path() reads the 'PATH' variable |
| 1151 | with _execvpe_mockup() as calls: |
| 1152 | env_path = env.copy() |
Victor Stinner | 38430e2 | 2010-08-19 17:10:18 +0000 | [diff] [blame] | 1153 | if test_type is bytes: |
| 1154 | env_path[b'PATH'] = program_path |
| 1155 | else: |
| 1156 | env_path['PATH'] = program_path |
Victor Stinner | b745a74 | 2010-05-18 17:17:23 +0000 | [diff] [blame] | 1157 | self.assertRaises(OSError, |
| 1158 | os._execvpe, program, arguments, env=env_path) |
| 1159 | self.assertEqual(len(calls), 1) |
| 1160 | self.assertSequenceEqual(calls[0], |
| 1161 | ('execve', native_fullpath, (arguments, env_path))) |
| 1162 | |
| 1163 | def test_internal_execvpe_str(self): |
| 1164 | self._test_internal_execvpe(str) |
| 1165 | if os.name != "nt": |
| 1166 | self._test_internal_execvpe(bytes) |
Victor Stinner | c2d095f | 2010-05-17 00:14:53 +0000 | [diff] [blame] | 1167 | |
Gregory P. Smith | 4ae3777 | 2010-05-08 18:05:46 +0000 | [diff] [blame] | 1168 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1169 | class Win32ErrorTests(unittest.TestCase): |
| 1170 | def test_rename(self): |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1171 | self.assertRaises(OSError, os.rename, support.TESTFN, support.TESTFN+".bak") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1172 | |
| 1173 | def test_remove(self): |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1174 | self.assertRaises(OSError, os.remove, support.TESTFN) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1175 | |
| 1176 | def test_chdir(self): |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1177 | self.assertRaises(OSError, os.chdir, support.TESTFN) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1178 | |
| 1179 | def test_mkdir(self): |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1180 | f = open(support.TESTFN, "w") |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 1181 | try: |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1182 | self.assertRaises(OSError, os.mkdir, support.TESTFN) |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 1183 | finally: |
| 1184 | f.close() |
Amaury Forgeot d'Arc | 2fc224f | 2009-02-19 23:23:47 +0000 | [diff] [blame] | 1185 | os.unlink(support.TESTFN) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1186 | |
| 1187 | def test_utime(self): |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1188 | self.assertRaises(OSError, os.utime, support.TESTFN, None) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1189 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1190 | def test_chmod(self): |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1191 | self.assertRaises(OSError, os.chmod, support.TESTFN, 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1192 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 1193 | class TestInvalidFD(unittest.TestCase): |
Benjamin Peterson | 05e782f | 2009-01-19 15:15:02 +0000 | [diff] [blame] | 1194 | singles = ["fchdir", "dup", "fdopen", "fdatasync", "fstat", |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 1195 | "fstatvfs", "fsync", "tcgetpgrp", "ttyname"] |
| 1196 | #singles.append("close") |
| 1197 | #We omit close because it doesn'r raise an exception on some platforms |
| 1198 | def get_single(f): |
| 1199 | def helper(self): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 1200 | if hasattr(os, f): |
| 1201 | self.check(getattr(os, f)) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 1202 | return helper |
| 1203 | for f in singles: |
| 1204 | locals()["test_"+f] = get_single(f) |
| 1205 | |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 1206 | def check(self, f, *args): |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 1207 | try: |
| 1208 | f(support.make_bad_fd(), *args) |
| 1209 | except OSError as e: |
| 1210 | self.assertEqual(e.errno, errno.EBADF) |
| 1211 | else: |
| 1212 | self.fail("%r didn't raise a OSError with a bad file descriptor" |
| 1213 | % f) |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 1214 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 1215 | def test_isatty(self): |
| 1216 | if hasattr(os, "isatty"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 1217 | self.assertEqual(os.isatty(support.make_bad_fd()), False) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 1218 | |
| 1219 | def test_closerange(self): |
| 1220 | if hasattr(os, "closerange"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 1221 | fd = support.make_bad_fd() |
R. David Murray | 630cc48 | 2009-07-22 15:20:27 +0000 | [diff] [blame] | 1222 | # Make sure none of the descriptors we are about to close are |
| 1223 | # currently valid (issue 6542). |
| 1224 | for i in range(10): |
| 1225 | try: os.fstat(fd+i) |
| 1226 | except OSError: |
| 1227 | pass |
| 1228 | else: |
| 1229 | break |
| 1230 | if i < 2: |
| 1231 | raise unittest.SkipTest( |
| 1232 | "Unable to acquire a range of invalid file descriptors") |
| 1233 | self.assertEqual(os.closerange(fd, fd + i-1), None) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 1234 | |
| 1235 | def test_dup2(self): |
| 1236 | if hasattr(os, "dup2"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 1237 | self.check(os.dup2, 20) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 1238 | |
| 1239 | def test_fchmod(self): |
| 1240 | if hasattr(os, "fchmod"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 1241 | self.check(os.fchmod, 0) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 1242 | |
| 1243 | def test_fchown(self): |
| 1244 | if hasattr(os, "fchown"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 1245 | self.check(os.fchown, -1, -1) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 1246 | |
| 1247 | def test_fpathconf(self): |
| 1248 | if hasattr(os, "fpathconf"): |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 1249 | self.check(os.pathconf, "PC_NAME_MAX") |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 1250 | self.check(os.fpathconf, "PC_NAME_MAX") |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 1251 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 1252 | def test_ftruncate(self): |
| 1253 | if hasattr(os, "ftruncate"): |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 1254 | self.check(os.truncate, 0) |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 1255 | self.check(os.ftruncate, 0) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 1256 | |
| 1257 | def test_lseek(self): |
| 1258 | if hasattr(os, "lseek"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 1259 | self.check(os.lseek, 0, 0) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 1260 | |
| 1261 | def test_read(self): |
| 1262 | if hasattr(os, "read"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 1263 | self.check(os.read, 1) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 1264 | |
| 1265 | def test_tcsetpgrpt(self): |
| 1266 | if hasattr(os, "tcsetpgrp"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 1267 | self.check(os.tcsetpgrp, 0) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 1268 | |
| 1269 | def test_write(self): |
| 1270 | if hasattr(os, "write"): |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 1271 | self.check(os.write, b" ") |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 1272 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1273 | |
| 1274 | class LinkTests(unittest.TestCase): |
| 1275 | def setUp(self): |
| 1276 | self.file1 = support.TESTFN |
| 1277 | self.file2 = os.path.join(support.TESTFN + "2") |
| 1278 | |
Brian Curtin | c0abc4e | 2010-11-30 23:46:54 +0000 | [diff] [blame] | 1279 | def tearDown(self): |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1280 | for file in (self.file1, self.file2): |
| 1281 | if os.path.exists(file): |
| 1282 | os.unlink(file) |
| 1283 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1284 | def _test_link(self, file1, file2): |
| 1285 | with open(file1, "w") as f1: |
| 1286 | f1.write("test") |
| 1287 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1288 | with warnings.catch_warnings(): |
| 1289 | warnings.simplefilter("ignore", DeprecationWarning) |
| 1290 | os.link(file1, file2) |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 1291 | with open(file1, "r") as f1, open(file2, "r") as f2: |
| 1292 | self.assertTrue(os.path.sameopenfile(f1.fileno(), f2.fileno())) |
| 1293 | |
| 1294 | def test_link(self): |
| 1295 | self._test_link(self.file1, self.file2) |
| 1296 | |
| 1297 | def test_link_bytes(self): |
| 1298 | self._test_link(bytes(self.file1, sys.getfilesystemencoding()), |
| 1299 | bytes(self.file2, sys.getfilesystemencoding())) |
| 1300 | |
Brian Curtin | f498b75 | 2010-11-30 15:54:04 +0000 | [diff] [blame] | 1301 | def test_unicode_name(self): |
Brian Curtin | 43f0c27 | 2010-11-30 15:40:04 +0000 | [diff] [blame] | 1302 | try: |
Brian Curtin | f498b75 | 2010-11-30 15:54:04 +0000 | [diff] [blame] | 1303 | os.fsencode("\xf1") |
Brian Curtin | 43f0c27 | 2010-11-30 15:40:04 +0000 | [diff] [blame] | 1304 | except UnicodeError: |
| 1305 | raise unittest.SkipTest("Unable to encode for this platform.") |
| 1306 | |
Brian Curtin | f498b75 | 2010-11-30 15:54:04 +0000 | [diff] [blame] | 1307 | self.file1 += "\xf1" |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 1308 | self.file2 = self.file1 + "2" |
| 1309 | self._test_link(self.file1, self.file2) |
| 1310 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1311 | if sys.platform != 'win32': |
| 1312 | class Win32ErrorTests(unittest.TestCase): |
| 1313 | pass |
| 1314 | |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1315 | class PosixUidGidTests(unittest.TestCase): |
| 1316 | if hasattr(os, 'setuid'): |
| 1317 | def test_setuid(self): |
| 1318 | if os.getuid() != 0: |
Andrew Svetlov | 8b33dd8 | 2012-12-24 19:58:48 +0200 | [diff] [blame] | 1319 | self.assertRaises(OSError, os.setuid, 0) |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1320 | self.assertRaises(OverflowError, os.setuid, 1<<32) |
| 1321 | |
| 1322 | if hasattr(os, 'setgid'): |
| 1323 | def test_setgid(self): |
Stefan Krah | ebee49a | 2013-01-17 15:31:00 +0100 | [diff] [blame] | 1324 | if os.getuid() != 0 and not HAVE_WHEEL_GROUP: |
Andrew Svetlov | 8b33dd8 | 2012-12-24 19:58:48 +0200 | [diff] [blame] | 1325 | self.assertRaises(OSError, os.setgid, 0) |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1326 | self.assertRaises(OverflowError, os.setgid, 1<<32) |
| 1327 | |
| 1328 | if hasattr(os, 'seteuid'): |
| 1329 | def test_seteuid(self): |
| 1330 | if os.getuid() != 0: |
Andrew Svetlov | 8b33dd8 | 2012-12-24 19:58:48 +0200 | [diff] [blame] | 1331 | self.assertRaises(OSError, os.seteuid, 0) |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1332 | self.assertRaises(OverflowError, os.seteuid, 1<<32) |
| 1333 | |
| 1334 | if hasattr(os, 'setegid'): |
| 1335 | def test_setegid(self): |
Stefan Krah | ebee49a | 2013-01-17 15:31:00 +0100 | [diff] [blame] | 1336 | if os.getuid() != 0 and not HAVE_WHEEL_GROUP: |
Andrew Svetlov | 8b33dd8 | 2012-12-24 19:58:48 +0200 | [diff] [blame] | 1337 | self.assertRaises(OSError, os.setegid, 0) |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1338 | self.assertRaises(OverflowError, os.setegid, 1<<32) |
| 1339 | |
| 1340 | if hasattr(os, 'setreuid'): |
| 1341 | def test_setreuid(self): |
| 1342 | if os.getuid() != 0: |
Andrew Svetlov | 8b33dd8 | 2012-12-24 19:58:48 +0200 | [diff] [blame] | 1343 | self.assertRaises(OSError, os.setreuid, 0, 0) |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1344 | self.assertRaises(OverflowError, os.setreuid, 1<<32, 0) |
| 1345 | self.assertRaises(OverflowError, os.setreuid, 0, 1<<32) |
Benjamin Peterson | ebe87ba | 2010-03-06 20:34:24 +0000 | [diff] [blame] | 1346 | |
| 1347 | def test_setreuid_neg1(self): |
| 1348 | # Needs to accept -1. We run this in a subprocess to avoid |
| 1349 | # altering the test runner's process state (issue8045). |
Benjamin Peterson | ebe87ba | 2010-03-06 20:34:24 +0000 | [diff] [blame] | 1350 | subprocess.check_call([ |
| 1351 | sys.executable, '-c', |
| 1352 | 'import os,sys;os.setreuid(-1,-1);sys.exit(0)']) |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1353 | |
| 1354 | if hasattr(os, 'setregid'): |
| 1355 | def test_setregid(self): |
Stefan Krah | ebee49a | 2013-01-17 15:31:00 +0100 | [diff] [blame] | 1356 | if os.getuid() != 0 and not HAVE_WHEEL_GROUP: |
Andrew Svetlov | 8b33dd8 | 2012-12-24 19:58:48 +0200 | [diff] [blame] | 1357 | self.assertRaises(OSError, os.setregid, 0, 0) |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1358 | self.assertRaises(OverflowError, os.setregid, 1<<32, 0) |
| 1359 | self.assertRaises(OverflowError, os.setregid, 0, 1<<32) |
Benjamin Peterson | ebe87ba | 2010-03-06 20:34:24 +0000 | [diff] [blame] | 1360 | |
| 1361 | def test_setregid_neg1(self): |
| 1362 | # Needs to accept -1. We run this in a subprocess to avoid |
| 1363 | # altering the test runner's process state (issue8045). |
Benjamin Peterson | ebe87ba | 2010-03-06 20:34:24 +0000 | [diff] [blame] | 1364 | subprocess.check_call([ |
| 1365 | sys.executable, '-c', |
| 1366 | 'import os,sys;os.setregid(-1,-1);sys.exit(0)']) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1367 | |
| 1368 | class Pep383Tests(unittest.TestCase): |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1369 | def setUp(self): |
Victor Stinner | d91df1a | 2010-08-18 10:56:19 +0000 | [diff] [blame] | 1370 | if support.TESTFN_UNENCODABLE: |
| 1371 | self.dir = support.TESTFN_UNENCODABLE |
Victor Stinner | 8b219b2 | 2012-11-06 23:23:43 +0100 | [diff] [blame] | 1372 | elif support.TESTFN_NONASCII: |
| 1373 | self.dir = support.TESTFN_NONASCII |
Victor Stinner | d91df1a | 2010-08-18 10:56:19 +0000 | [diff] [blame] | 1374 | else: |
| 1375 | self.dir = support.TESTFN |
| 1376 | self.bdir = os.fsencode(self.dir) |
| 1377 | |
| 1378 | bytesfn = [] |
| 1379 | def add_filename(fn): |
| 1380 | try: |
| 1381 | fn = os.fsencode(fn) |
| 1382 | except UnicodeEncodeError: |
| 1383 | return |
| 1384 | bytesfn.append(fn) |
| 1385 | add_filename(support.TESTFN_UNICODE) |
| 1386 | if support.TESTFN_UNENCODABLE: |
| 1387 | add_filename(support.TESTFN_UNENCODABLE) |
Victor Stinner | 8b219b2 | 2012-11-06 23:23:43 +0100 | [diff] [blame] | 1388 | if support.TESTFN_NONASCII: |
| 1389 | add_filename(support.TESTFN_NONASCII) |
Victor Stinner | d91df1a | 2010-08-18 10:56:19 +0000 | [diff] [blame] | 1390 | if not bytesfn: |
| 1391 | self.skipTest("couldn't create any non-ascii filename") |
| 1392 | |
| 1393 | self.unicodefn = set() |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1394 | os.mkdir(self.dir) |
Victor Stinner | d91df1a | 2010-08-18 10:56:19 +0000 | [diff] [blame] | 1395 | try: |
| 1396 | for fn in bytesfn: |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 1397 | support.create_empty_file(os.path.join(self.bdir, fn)) |
Victor Stinner | e8d5145 | 2010-08-19 01:05:19 +0000 | [diff] [blame] | 1398 | fn = os.fsdecode(fn) |
Victor Stinner | d91df1a | 2010-08-18 10:56:19 +0000 | [diff] [blame] | 1399 | if fn in self.unicodefn: |
| 1400 | raise ValueError("duplicate filename") |
| 1401 | self.unicodefn.add(fn) |
| 1402 | except: |
| 1403 | shutil.rmtree(self.dir) |
| 1404 | raise |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1405 | |
| 1406 | def tearDown(self): |
| 1407 | shutil.rmtree(self.dir) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1408 | |
| 1409 | def test_listdir(self): |
Victor Stinner | d91df1a | 2010-08-18 10:56:19 +0000 | [diff] [blame] | 1410 | expected = self.unicodefn |
| 1411 | found = set(os.listdir(self.dir)) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1412 | self.assertEqual(found, expected) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1413 | # test listdir without arguments |
| 1414 | current_directory = os.getcwd() |
| 1415 | try: |
| 1416 | os.chdir(os.sep) |
| 1417 | self.assertEqual(set(os.listdir()), set(os.listdir(os.sep))) |
| 1418 | finally: |
| 1419 | os.chdir(current_directory) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1420 | |
| 1421 | def test_open(self): |
| 1422 | for fn in self.unicodefn: |
Victor Stinner | a6d2c76 | 2011-06-30 18:20:11 +0200 | [diff] [blame] | 1423 | f = open(os.path.join(self.dir, fn), 'rb') |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1424 | f.close() |
| 1425 | |
Victor Stinner | e4110dc | 2013-01-01 23:05:55 +0100 | [diff] [blame] | 1426 | @unittest.skipUnless(hasattr(os, 'statvfs'), |
| 1427 | "need os.statvfs()") |
| 1428 | def test_statvfs(self): |
| 1429 | # issue #9645 |
| 1430 | for fn in self.unicodefn: |
| 1431 | # should not fail with file not found error |
| 1432 | fullname = os.path.join(self.dir, fn) |
| 1433 | os.statvfs(fullname) |
| 1434 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1435 | def test_stat(self): |
| 1436 | for fn in self.unicodefn: |
| 1437 | os.stat(os.path.join(self.dir, fn)) |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1438 | else: |
| 1439 | class PosixUidGidTests(unittest.TestCase): |
| 1440 | pass |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1441 | class Pep383Tests(unittest.TestCase): |
| 1442 | pass |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1443 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 1444 | @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests") |
| 1445 | class Win32KillTests(unittest.TestCase): |
Brian Curtin | c3acbc3 | 2010-05-28 16:08:40 +0000 | [diff] [blame] | 1446 | def _kill(self, sig): |
| 1447 | # Start sys.executable as a subprocess and communicate from the |
| 1448 | # subprocess to the parent that the interpreter is ready. When it |
| 1449 | # becomes ready, send *sig* via os.kill to the subprocess and check |
| 1450 | # that the return code is equal to *sig*. |
| 1451 | import ctypes |
| 1452 | from ctypes import wintypes |
| 1453 | import msvcrt |
| 1454 | |
| 1455 | # Since we can't access the contents of the process' stdout until the |
| 1456 | # process has exited, use PeekNamedPipe to see what's inside stdout |
| 1457 | # without waiting. This is done so we can tell that the interpreter |
| 1458 | # is started and running at a point where it could handle a signal. |
| 1459 | PeekNamedPipe = ctypes.windll.kernel32.PeekNamedPipe |
| 1460 | PeekNamedPipe.restype = wintypes.BOOL |
| 1461 | PeekNamedPipe.argtypes = (wintypes.HANDLE, # Pipe handle |
| 1462 | ctypes.POINTER(ctypes.c_char), # stdout buf |
| 1463 | wintypes.DWORD, # Buffer size |
| 1464 | ctypes.POINTER(wintypes.DWORD), # bytes read |
| 1465 | ctypes.POINTER(wintypes.DWORD), # bytes avail |
| 1466 | ctypes.POINTER(wintypes.DWORD)) # bytes left |
| 1467 | msg = "running" |
| 1468 | proc = subprocess.Popen([sys.executable, "-c", |
| 1469 | "import sys;" |
| 1470 | "sys.stdout.write('{}');" |
| 1471 | "sys.stdout.flush();" |
| 1472 | "input()".format(msg)], |
| 1473 | stdout=subprocess.PIPE, |
| 1474 | stderr=subprocess.PIPE, |
| 1475 | stdin=subprocess.PIPE) |
Brian Curtin | 43ec577 | 2010-11-05 15:17:11 +0000 | [diff] [blame] | 1476 | self.addCleanup(proc.stdout.close) |
| 1477 | self.addCleanup(proc.stderr.close) |
| 1478 | self.addCleanup(proc.stdin.close) |
Brian Curtin | c3acbc3 | 2010-05-28 16:08:40 +0000 | [diff] [blame] | 1479 | |
| 1480 | count, max = 0, 100 |
| 1481 | while count < max and proc.poll() is None: |
| 1482 | # Create a string buffer to store the result of stdout from the pipe |
| 1483 | buf = ctypes.create_string_buffer(len(msg)) |
| 1484 | # Obtain the text currently in proc.stdout |
| 1485 | # Bytes read/avail/left are left as NULL and unused |
| 1486 | rslt = PeekNamedPipe(msvcrt.get_osfhandle(proc.stdout.fileno()), |
| 1487 | buf, ctypes.sizeof(buf), None, None, None) |
| 1488 | self.assertNotEqual(rslt, 0, "PeekNamedPipe failed") |
| 1489 | if buf.value: |
| 1490 | self.assertEqual(msg, buf.value.decode()) |
| 1491 | break |
| 1492 | time.sleep(0.1) |
| 1493 | count += 1 |
| 1494 | else: |
| 1495 | self.fail("Did not receive communication from the subprocess") |
| 1496 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 1497 | os.kill(proc.pid, sig) |
| 1498 | self.assertEqual(proc.wait(), sig) |
| 1499 | |
| 1500 | def test_kill_sigterm(self): |
| 1501 | # SIGTERM doesn't mean anything special, but make sure it works |
Brian Curtin | c3acbc3 | 2010-05-28 16:08:40 +0000 | [diff] [blame] | 1502 | self._kill(signal.SIGTERM) |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 1503 | |
| 1504 | def test_kill_int(self): |
| 1505 | # 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] | 1506 | self._kill(100) |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 1507 | |
| 1508 | def _kill_with_event(self, event, name): |
Hirokazu Yamamoto | 54c950f | 2010-10-08 08:38:15 +0000 | [diff] [blame] | 1509 | tagname = "test_os_%s" % uuid.uuid1() |
| 1510 | m = mmap.mmap(-1, 1, tagname) |
| 1511 | m[0] = 0 |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 1512 | # Run a script which has console control handling enabled. |
| 1513 | proc = subprocess.Popen([sys.executable, |
| 1514 | os.path.join(os.path.dirname(__file__), |
Hirokazu Yamamoto | 54c950f | 2010-10-08 08:38:15 +0000 | [diff] [blame] | 1515 | "win_console_handler.py"), tagname], |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 1516 | creationflags=subprocess.CREATE_NEW_PROCESS_GROUP) |
| 1517 | # Let the interpreter startup before we send signals. See #3137. |
Hirokazu Yamamoto | 8e9fe9f | 2010-12-05 02:41:46 +0000 | [diff] [blame] | 1518 | count, max = 0, 100 |
Hirokazu Yamamoto | 54c950f | 2010-10-08 08:38:15 +0000 | [diff] [blame] | 1519 | while count < max and proc.poll() is None: |
Brian Curtin | f668df5 | 2010-10-15 14:21:06 +0000 | [diff] [blame] | 1520 | if m[0] == 1: |
Hirokazu Yamamoto | 54c950f | 2010-10-08 08:38:15 +0000 | [diff] [blame] | 1521 | break |
Hirokazu Yamamoto | 8e9fe9f | 2010-12-05 02:41:46 +0000 | [diff] [blame] | 1522 | time.sleep(0.1) |
Hirokazu Yamamoto | 54c950f | 2010-10-08 08:38:15 +0000 | [diff] [blame] | 1523 | count += 1 |
| 1524 | else: |
Hirokazu Yamamoto | 8e9fe9f | 2010-12-05 02:41:46 +0000 | [diff] [blame] | 1525 | # Forcefully kill the process if we weren't able to signal it. |
| 1526 | os.kill(proc.pid, signal.SIGINT) |
Hirokazu Yamamoto | 54c950f | 2010-10-08 08:38:15 +0000 | [diff] [blame] | 1527 | self.fail("Subprocess didn't finish initialization") |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 1528 | os.kill(proc.pid, event) |
| 1529 | # proc.send_signal(event) could also be done here. |
| 1530 | # Allow time for the signal to be passed and the process to exit. |
| 1531 | time.sleep(0.5) |
| 1532 | if not proc.poll(): |
| 1533 | # Forcefully kill the process if we weren't able to signal it. |
| 1534 | os.kill(proc.pid, signal.SIGINT) |
| 1535 | self.fail("subprocess did not stop on {}".format(name)) |
| 1536 | |
| 1537 | @unittest.skip("subprocesses aren't inheriting CTRL+C property") |
| 1538 | def test_CTRL_C_EVENT(self): |
| 1539 | from ctypes import wintypes |
| 1540 | import ctypes |
| 1541 | |
| 1542 | # Make a NULL value by creating a pointer with no argument. |
| 1543 | NULL = ctypes.POINTER(ctypes.c_int)() |
| 1544 | SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler |
| 1545 | SetConsoleCtrlHandler.argtypes = (ctypes.POINTER(ctypes.c_int), |
| 1546 | wintypes.BOOL) |
| 1547 | SetConsoleCtrlHandler.restype = wintypes.BOOL |
| 1548 | |
| 1549 | # Calling this with NULL and FALSE causes the calling process to |
| 1550 | # handle CTRL+C, rather than ignore it. This property is inherited |
| 1551 | # by subprocesses. |
| 1552 | SetConsoleCtrlHandler(NULL, 0) |
| 1553 | |
| 1554 | self._kill_with_event(signal.CTRL_C_EVENT, "CTRL_C_EVENT") |
| 1555 | |
| 1556 | def test_CTRL_BREAK_EVENT(self): |
| 1557 | self._kill_with_event(signal.CTRL_BREAK_EVENT, "CTRL_BREAK_EVENT") |
| 1558 | |
| 1559 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1560 | @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests") |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 1561 | @support.skip_unless_symlink |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1562 | class Win32SymlinkTests(unittest.TestCase): |
| 1563 | filelink = 'filelinktest' |
| 1564 | filelink_target = os.path.abspath(__file__) |
| 1565 | dirlink = 'dirlinktest' |
| 1566 | dirlink_target = os.path.dirname(filelink_target) |
| 1567 | missing_link = 'missing link' |
| 1568 | |
| 1569 | def setUp(self): |
| 1570 | assert os.path.exists(self.dirlink_target) |
| 1571 | assert os.path.exists(self.filelink_target) |
| 1572 | assert not os.path.exists(self.dirlink) |
| 1573 | assert not os.path.exists(self.filelink) |
| 1574 | assert not os.path.exists(self.missing_link) |
| 1575 | |
| 1576 | def tearDown(self): |
| 1577 | if os.path.exists(self.filelink): |
| 1578 | os.remove(self.filelink) |
| 1579 | if os.path.exists(self.dirlink): |
| 1580 | os.rmdir(self.dirlink) |
| 1581 | if os.path.lexists(self.missing_link): |
| 1582 | os.remove(self.missing_link) |
| 1583 | |
| 1584 | def test_directory_link(self): |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 1585 | os.symlink(self.dirlink_target, self.dirlink) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1586 | self.assertTrue(os.path.exists(self.dirlink)) |
| 1587 | self.assertTrue(os.path.isdir(self.dirlink)) |
| 1588 | self.assertTrue(os.path.islink(self.dirlink)) |
| 1589 | self.check_stat(self.dirlink, self.dirlink_target) |
| 1590 | |
| 1591 | def test_file_link(self): |
| 1592 | os.symlink(self.filelink_target, self.filelink) |
| 1593 | self.assertTrue(os.path.exists(self.filelink)) |
| 1594 | self.assertTrue(os.path.isfile(self.filelink)) |
| 1595 | self.assertTrue(os.path.islink(self.filelink)) |
| 1596 | self.check_stat(self.filelink, self.filelink_target) |
| 1597 | |
| 1598 | def _create_missing_dir_link(self): |
| 1599 | 'Create a "directory" link to a non-existent target' |
| 1600 | linkname = self.missing_link |
| 1601 | if os.path.lexists(linkname): |
| 1602 | os.remove(linkname) |
| 1603 | target = r'c:\\target does not exist.29r3c740' |
| 1604 | assert not os.path.exists(target) |
| 1605 | target_is_dir = True |
| 1606 | os.symlink(target, linkname, target_is_dir) |
| 1607 | |
| 1608 | def test_remove_directory_link_to_missing_target(self): |
| 1609 | self._create_missing_dir_link() |
| 1610 | # For compatibility with Unix, os.remove will check the |
| 1611 | # directory status and call RemoveDirectory if the symlink |
| 1612 | # was created with target_is_dir==True. |
| 1613 | os.remove(self.missing_link) |
| 1614 | |
| 1615 | @unittest.skip("currently fails; consider for improvement") |
| 1616 | def test_isdir_on_directory_link_to_missing_target(self): |
| 1617 | self._create_missing_dir_link() |
| 1618 | # consider having isdir return true for directory links |
| 1619 | self.assertTrue(os.path.isdir(self.missing_link)) |
| 1620 | |
| 1621 | @unittest.skip("currently fails; consider for improvement") |
| 1622 | def test_rmdir_on_directory_link_to_missing_target(self): |
| 1623 | self._create_missing_dir_link() |
| 1624 | # consider allowing rmdir to remove directory links |
| 1625 | os.rmdir(self.missing_link) |
| 1626 | |
| 1627 | def check_stat(self, link, target): |
| 1628 | self.assertEqual(os.stat(link), os.stat(target)) |
| 1629 | self.assertNotEqual(os.lstat(link), os.stat(link)) |
| 1630 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1631 | bytes_link = os.fsencode(link) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1632 | with warnings.catch_warnings(): |
| 1633 | warnings.simplefilter("ignore", DeprecationWarning) |
| 1634 | self.assertEqual(os.stat(bytes_link), os.stat(target)) |
| 1635 | self.assertNotEqual(os.lstat(bytes_link), os.stat(bytes_link)) |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1636 | |
| 1637 | def test_12084(self): |
| 1638 | level1 = os.path.abspath(support.TESTFN) |
| 1639 | level2 = os.path.join(level1, "level2") |
| 1640 | level3 = os.path.join(level2, "level3") |
| 1641 | try: |
| 1642 | os.mkdir(level1) |
| 1643 | os.mkdir(level2) |
| 1644 | os.mkdir(level3) |
| 1645 | |
| 1646 | file1 = os.path.abspath(os.path.join(level1, "file1")) |
| 1647 | |
| 1648 | with open(file1, "w") as f: |
| 1649 | f.write("file1") |
| 1650 | |
| 1651 | orig_dir = os.getcwd() |
| 1652 | try: |
| 1653 | os.chdir(level2) |
| 1654 | link = os.path.join(level2, "link") |
| 1655 | os.symlink(os.path.relpath(file1), "link") |
| 1656 | self.assertIn("link", os.listdir(os.getcwd())) |
| 1657 | |
| 1658 | # Check os.stat calls from the same dir as the link |
| 1659 | self.assertEqual(os.stat(file1), os.stat("link")) |
| 1660 | |
| 1661 | # Check os.stat calls from a dir below the link |
| 1662 | os.chdir(level1) |
| 1663 | self.assertEqual(os.stat(file1), |
| 1664 | os.stat(os.path.relpath(link))) |
| 1665 | |
| 1666 | # Check os.stat calls from a dir above the link |
| 1667 | os.chdir(level3) |
| 1668 | self.assertEqual(os.stat(file1), |
| 1669 | os.stat(os.path.relpath(link))) |
| 1670 | finally: |
| 1671 | os.chdir(orig_dir) |
| 1672 | except OSError as err: |
| 1673 | self.fail(err) |
| 1674 | finally: |
| 1675 | os.remove(file1) |
| 1676 | shutil.rmtree(level1) |
| 1677 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1678 | |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 1679 | @support.skip_unless_symlink |
| 1680 | class NonLocalSymlinkTests(unittest.TestCase): |
| 1681 | |
| 1682 | def setUp(self): |
| 1683 | """ |
| 1684 | Create this structure: |
| 1685 | |
| 1686 | base |
| 1687 | \___ some_dir |
| 1688 | """ |
| 1689 | os.makedirs('base/some_dir') |
| 1690 | |
| 1691 | def tearDown(self): |
| 1692 | shutil.rmtree('base') |
| 1693 | |
| 1694 | def test_directory_link_nonlocal(self): |
| 1695 | """ |
| 1696 | The symlink target should resolve relative to the link, not relative |
| 1697 | to the current directory. |
| 1698 | |
| 1699 | Then, link base/some_link -> base/some_dir and ensure that some_link |
| 1700 | is resolved as a directory. |
| 1701 | |
| 1702 | In issue13772, it was discovered that directory detection failed if |
| 1703 | the symlink target was not specified relative to the current |
| 1704 | directory, which was a defect in the implementation. |
| 1705 | """ |
| 1706 | src = os.path.join('base', 'some_link') |
| 1707 | os.symlink('some_dir', src) |
| 1708 | assert os.path.isdir(src) |
| 1709 | |
| 1710 | |
Victor Stinner | e8d5145 | 2010-08-19 01:05:19 +0000 | [diff] [blame] | 1711 | class FSEncodingTests(unittest.TestCase): |
| 1712 | def test_nop(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1713 | self.assertEqual(os.fsencode(b'abc\xff'), b'abc\xff') |
| 1714 | self.assertEqual(os.fsdecode('abc\u0141'), 'abc\u0141') |
Benjamin Peterson | 31191a9 | 2010-05-09 03:22:58 +0000 | [diff] [blame] | 1715 | |
Victor Stinner | e8d5145 | 2010-08-19 01:05:19 +0000 | [diff] [blame] | 1716 | def test_identity(self): |
| 1717 | # assert fsdecode(fsencode(x)) == x |
| 1718 | for fn in ('unicode\u0141', 'latin\xe9', 'ascii'): |
| 1719 | try: |
| 1720 | bytesfn = os.fsencode(fn) |
| 1721 | except UnicodeEncodeError: |
| 1722 | continue |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1723 | self.assertEqual(os.fsdecode(bytesfn), fn) |
Victor Stinner | e8d5145 | 2010-08-19 01:05:19 +0000 | [diff] [blame] | 1724 | |
Victor Stinner | bf9bcab | 2010-05-09 03:15:33 +0000 | [diff] [blame] | 1725 | |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 1726 | |
| 1727 | class DeviceEncodingTests(unittest.TestCase): |
| 1728 | |
| 1729 | def test_bad_fd(self): |
| 1730 | # Return None when an fd doesn't actually exist. |
| 1731 | self.assertIsNone(os.device_encoding(123456)) |
| 1732 | |
Philip Jenvey | e308b7c | 2012-02-29 16:16:15 -0800 | [diff] [blame] | 1733 | @unittest.skipUnless(os.isatty(0) and (sys.platform.startswith('win') or |
| 1734 | (hasattr(locale, 'nl_langinfo') and hasattr(locale, 'CODESET'))), |
Philip Jenvey | d7aff2d | 2012-02-29 16:21:25 -0800 | [diff] [blame] | 1735 | 'test requires a tty and either Windows or nl_langinfo(CODESET)') |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 1736 | def test_device_encoding(self): |
| 1737 | encoding = os.device_encoding(0) |
| 1738 | self.assertIsNotNone(encoding) |
| 1739 | self.assertTrue(codecs.lookup(encoding)) |
| 1740 | |
| 1741 | |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 1742 | class PidTests(unittest.TestCase): |
| 1743 | @unittest.skipUnless(hasattr(os, 'getppid'), "test needs os.getppid") |
| 1744 | def test_getppid(self): |
| 1745 | p = subprocess.Popen([sys.executable, '-c', |
| 1746 | 'import os; print(os.getppid())'], |
| 1747 | stdout=subprocess.PIPE) |
| 1748 | stdout, _ = p.communicate() |
| 1749 | # We are the parent of our subprocess |
| 1750 | self.assertEqual(int(stdout), os.getpid()) |
| 1751 | |
| 1752 | |
Brian Curtin | 0151b8e | 2010-09-24 13:43:43 +0000 | [diff] [blame] | 1753 | # The introduction of this TestCase caused at least two different errors on |
| 1754 | # *nix buildbots. Temporarily skip this to let the buildbots move along. |
| 1755 | @unittest.skip("Skip due to platform/environment differences on *NIX buildbots") |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 1756 | @unittest.skipUnless(hasattr(os, 'getlogin'), "test needs os.getlogin") |
| 1757 | class LoginTests(unittest.TestCase): |
| 1758 | def test_getlogin(self): |
| 1759 | user_name = os.getlogin() |
| 1760 | self.assertNotEqual(len(user_name), 0) |
| 1761 | |
| 1762 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 1763 | @unittest.skipUnless(hasattr(os, 'getpriority') and hasattr(os, 'setpriority'), |
| 1764 | "needs os.getpriority and os.setpriority") |
| 1765 | class ProgramPriorityTests(unittest.TestCase): |
| 1766 | """Tests for os.getpriority() and os.setpriority().""" |
| 1767 | |
| 1768 | def test_set_get_priority(self): |
Giampaolo Rodolà | cfbcec3 | 2011-02-28 19:27:16 +0000 | [diff] [blame] | 1769 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 1770 | base = os.getpriority(os.PRIO_PROCESS, os.getpid()) |
| 1771 | os.setpriority(os.PRIO_PROCESS, os.getpid(), base + 1) |
| 1772 | try: |
Giampaolo Rodolà | cfbcec3 | 2011-02-28 19:27:16 +0000 | [diff] [blame] | 1773 | new_prio = os.getpriority(os.PRIO_PROCESS, os.getpid()) |
| 1774 | if base >= 19 and new_prio <= 19: |
| 1775 | raise unittest.SkipTest( |
| 1776 | "unable to reliably test setpriority at current nice level of %s" % base) |
| 1777 | else: |
| 1778 | self.assertEqual(new_prio, base + 1) |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 1779 | finally: |
| 1780 | try: |
| 1781 | os.setpriority(os.PRIO_PROCESS, os.getpid(), base) |
| 1782 | except OSError as err: |
Antoine Pitrou | 692f038 | 2011-02-26 00:22:25 +0000 | [diff] [blame] | 1783 | if err.errno != errno.EACCES: |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 1784 | raise |
| 1785 | |
| 1786 | |
Giampaolo Rodola' | 566f8a6 | 2011-05-18 21:28:39 +0200 | [diff] [blame] | 1787 | if threading is not None: |
| 1788 | class SendfileTestServer(asyncore.dispatcher, threading.Thread): |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1789 | |
Giampaolo Rodola' | 566f8a6 | 2011-05-18 21:28:39 +0200 | [diff] [blame] | 1790 | class Handler(asynchat.async_chat): |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1791 | |
Giampaolo Rodola' | 566f8a6 | 2011-05-18 21:28:39 +0200 | [diff] [blame] | 1792 | def __init__(self, conn): |
| 1793 | asynchat.async_chat.__init__(self, conn) |
| 1794 | self.in_buffer = [] |
| 1795 | self.closed = False |
| 1796 | self.push(b"220 ready\r\n") |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1797 | |
Giampaolo Rodola' | 566f8a6 | 2011-05-18 21:28:39 +0200 | [diff] [blame] | 1798 | def handle_read(self): |
| 1799 | data = self.recv(4096) |
| 1800 | self.in_buffer.append(data) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1801 | |
Giampaolo Rodola' | 566f8a6 | 2011-05-18 21:28:39 +0200 | [diff] [blame] | 1802 | def get_data(self): |
| 1803 | return b''.join(self.in_buffer) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1804 | |
Giampaolo Rodola' | 566f8a6 | 2011-05-18 21:28:39 +0200 | [diff] [blame] | 1805 | def handle_close(self): |
| 1806 | self.close() |
| 1807 | self.closed = True |
| 1808 | |
| 1809 | def handle_error(self): |
| 1810 | raise |
| 1811 | |
| 1812 | def __init__(self, address): |
| 1813 | threading.Thread.__init__(self) |
| 1814 | asyncore.dispatcher.__init__(self) |
| 1815 | self.create_socket(socket.AF_INET, socket.SOCK_STREAM) |
| 1816 | self.bind(address) |
| 1817 | self.listen(5) |
| 1818 | self.host, self.port = self.socket.getsockname()[:2] |
| 1819 | self.handler_instance = None |
| 1820 | self._active = False |
| 1821 | self._active_lock = threading.Lock() |
| 1822 | |
| 1823 | # --- public API |
| 1824 | |
| 1825 | @property |
| 1826 | def running(self): |
| 1827 | return self._active |
| 1828 | |
| 1829 | def start(self): |
| 1830 | assert not self.running |
| 1831 | self.__flag = threading.Event() |
| 1832 | threading.Thread.start(self) |
| 1833 | self.__flag.wait() |
| 1834 | |
| 1835 | def stop(self): |
| 1836 | assert self.running |
| 1837 | self._active = False |
| 1838 | self.join() |
| 1839 | |
| 1840 | def wait(self): |
| 1841 | # wait for handler connection to be closed, then stop the server |
| 1842 | while not getattr(self.handler_instance, "closed", False): |
| 1843 | time.sleep(0.001) |
| 1844 | self.stop() |
| 1845 | |
| 1846 | # --- internals |
| 1847 | |
| 1848 | def run(self): |
| 1849 | self._active = True |
| 1850 | self.__flag.set() |
| 1851 | while self._active and asyncore.socket_map: |
| 1852 | self._active_lock.acquire() |
| 1853 | asyncore.loop(timeout=0.001, count=1) |
| 1854 | self._active_lock.release() |
| 1855 | asyncore.close_all() |
| 1856 | |
| 1857 | def handle_accept(self): |
| 1858 | conn, addr = self.accept() |
| 1859 | self.handler_instance = self.Handler(conn) |
| 1860 | |
| 1861 | def handle_connect(self): |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1862 | self.close() |
Giampaolo Rodola' | 566f8a6 | 2011-05-18 21:28:39 +0200 | [diff] [blame] | 1863 | handle_read = handle_connect |
| 1864 | |
| 1865 | def writable(self): |
| 1866 | return 0 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1867 | |
| 1868 | def handle_error(self): |
| 1869 | raise |
| 1870 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1871 | |
Giampaolo Rodolà | 4613464 | 2011-02-25 20:01:05 +0000 | [diff] [blame] | 1872 | @unittest.skipUnless(threading is not None, "test needs threading module") |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1873 | @unittest.skipUnless(hasattr(os, 'sendfile'), "test needs os.sendfile()") |
| 1874 | class TestSendfile(unittest.TestCase): |
| 1875 | |
Antoine Pitrou | 18dd0df | 2011-02-26 14:29:24 +0000 | [diff] [blame] | 1876 | DATA = b"12345abcde" * 16 * 1024 # 160 KB |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1877 | SUPPORT_HEADERS_TRAILERS = not sys.platform.startswith("linux") and \ |
Giampaolo Rodolà | 4bc6857 | 2011-02-25 21:46:01 +0000 | [diff] [blame] | 1878 | not sys.platform.startswith("solaris") and \ |
| 1879 | not sys.platform.startswith("sunos") |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1880 | |
| 1881 | @classmethod |
| 1882 | def setUpClass(cls): |
| 1883 | with open(support.TESTFN, "wb") as f: |
| 1884 | f.write(cls.DATA) |
| 1885 | |
| 1886 | @classmethod |
| 1887 | def tearDownClass(cls): |
| 1888 | support.unlink(support.TESTFN) |
| 1889 | |
| 1890 | def setUp(self): |
| 1891 | self.server = SendfileTestServer((support.HOST, 0)) |
| 1892 | self.server.start() |
| 1893 | self.client = socket.socket() |
| 1894 | self.client.connect((self.server.host, self.server.port)) |
| 1895 | self.client.settimeout(1) |
| 1896 | # synchronize by waiting for "220 ready" response |
| 1897 | self.client.recv(1024) |
| 1898 | self.sockno = self.client.fileno() |
| 1899 | self.file = open(support.TESTFN, 'rb') |
| 1900 | self.fileno = self.file.fileno() |
| 1901 | |
| 1902 | def tearDown(self): |
| 1903 | self.file.close() |
| 1904 | self.client.close() |
| 1905 | if self.server.running: |
| 1906 | self.server.stop() |
| 1907 | |
| 1908 | def sendfile_wrapper(self, sock, file, offset, nbytes, headers=[], trailers=[]): |
| 1909 | """A higher level wrapper representing how an application is |
| 1910 | supposed to use sendfile(). |
| 1911 | """ |
| 1912 | while 1: |
| 1913 | try: |
| 1914 | if self.SUPPORT_HEADERS_TRAILERS: |
| 1915 | return os.sendfile(sock, file, offset, nbytes, headers, |
| 1916 | trailers) |
| 1917 | else: |
| 1918 | return os.sendfile(sock, file, offset, nbytes) |
| 1919 | except OSError as err: |
| 1920 | if err.errno == errno.ECONNRESET: |
| 1921 | # disconnected |
| 1922 | raise |
| 1923 | elif err.errno in (errno.EAGAIN, errno.EBUSY): |
| 1924 | # we have to retry send data |
| 1925 | continue |
| 1926 | else: |
| 1927 | raise |
| 1928 | |
| 1929 | def test_send_whole_file(self): |
| 1930 | # normal send |
| 1931 | total_sent = 0 |
| 1932 | offset = 0 |
| 1933 | nbytes = 4096 |
Antoine Pitrou | 18dd0df | 2011-02-26 14:29:24 +0000 | [diff] [blame] | 1934 | while total_sent < len(self.DATA): |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1935 | sent = self.sendfile_wrapper(self.sockno, self.fileno, offset, nbytes) |
| 1936 | if sent == 0: |
| 1937 | break |
| 1938 | offset += sent |
| 1939 | total_sent += sent |
| 1940 | self.assertTrue(sent <= nbytes) |
| 1941 | self.assertEqual(offset, total_sent) |
| 1942 | |
| 1943 | self.assertEqual(total_sent, len(self.DATA)) |
Antoine Pitrou | 2de51ff | 2011-02-26 17:52:50 +0000 | [diff] [blame] | 1944 | self.client.shutdown(socket.SHUT_RDWR) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1945 | self.client.close() |
| 1946 | self.server.wait() |
| 1947 | data = self.server.handler_instance.get_data() |
Antoine Pitrou | 2de51ff | 2011-02-26 17:52:50 +0000 | [diff] [blame] | 1948 | self.assertEqual(len(data), len(self.DATA)) |
Antoine Pitrou | 18dd0df | 2011-02-26 14:29:24 +0000 | [diff] [blame] | 1949 | self.assertEqual(data, self.DATA) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1950 | |
| 1951 | def test_send_at_certain_offset(self): |
| 1952 | # start sending a file at a certain offset |
| 1953 | total_sent = 0 |
Antoine Pitrou | 18dd0df | 2011-02-26 14:29:24 +0000 | [diff] [blame] | 1954 | offset = len(self.DATA) // 2 |
| 1955 | must_send = len(self.DATA) - offset |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1956 | nbytes = 4096 |
Antoine Pitrou | 18dd0df | 2011-02-26 14:29:24 +0000 | [diff] [blame] | 1957 | while total_sent < must_send: |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1958 | sent = self.sendfile_wrapper(self.sockno, self.fileno, offset, nbytes) |
| 1959 | if sent == 0: |
| 1960 | break |
| 1961 | offset += sent |
| 1962 | total_sent += sent |
| 1963 | self.assertTrue(sent <= nbytes) |
| 1964 | |
Antoine Pitrou | 2de51ff | 2011-02-26 17:52:50 +0000 | [diff] [blame] | 1965 | self.client.shutdown(socket.SHUT_RDWR) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1966 | self.client.close() |
| 1967 | self.server.wait() |
| 1968 | data = self.server.handler_instance.get_data() |
Antoine Pitrou | 18dd0df | 2011-02-26 14:29:24 +0000 | [diff] [blame] | 1969 | expected = self.DATA[len(self.DATA) // 2:] |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1970 | self.assertEqual(total_sent, len(expected)) |
Antoine Pitrou | 2de51ff | 2011-02-26 17:52:50 +0000 | [diff] [blame] | 1971 | self.assertEqual(len(data), len(expected)) |
Antoine Pitrou | 18dd0df | 2011-02-26 14:29:24 +0000 | [diff] [blame] | 1972 | self.assertEqual(data, expected) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1973 | |
| 1974 | def test_offset_overflow(self): |
| 1975 | # specify an offset > file size |
| 1976 | offset = len(self.DATA) + 4096 |
Antoine Pitrou | 18dd0df | 2011-02-26 14:29:24 +0000 | [diff] [blame] | 1977 | try: |
| 1978 | sent = os.sendfile(self.sockno, self.fileno, offset, 4096) |
| 1979 | except OSError as e: |
| 1980 | # Solaris can raise EINVAL if offset >= file length, ignore. |
| 1981 | if e.errno != errno.EINVAL: |
| 1982 | raise |
| 1983 | else: |
| 1984 | self.assertEqual(sent, 0) |
Antoine Pitrou | 2de51ff | 2011-02-26 17:52:50 +0000 | [diff] [blame] | 1985 | self.client.shutdown(socket.SHUT_RDWR) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1986 | self.client.close() |
| 1987 | self.server.wait() |
| 1988 | data = self.server.handler_instance.get_data() |
| 1989 | self.assertEqual(data, b'') |
| 1990 | |
| 1991 | def test_invalid_offset(self): |
| 1992 | with self.assertRaises(OSError) as cm: |
| 1993 | os.sendfile(self.sockno, self.fileno, -1, 4096) |
| 1994 | self.assertEqual(cm.exception.errno, errno.EINVAL) |
| 1995 | |
| 1996 | # --- headers / trailers tests |
| 1997 | |
| 1998 | if SUPPORT_HEADERS_TRAILERS: |
| 1999 | |
| 2000 | def test_headers(self): |
| 2001 | total_sent = 0 |
| 2002 | sent = os.sendfile(self.sockno, self.fileno, 0, 4096, |
| 2003 | headers=[b"x" * 512]) |
| 2004 | total_sent += sent |
| 2005 | offset = 4096 |
| 2006 | nbytes = 4096 |
| 2007 | while 1: |
| 2008 | sent = self.sendfile_wrapper(self.sockno, self.fileno, |
| 2009 | offset, nbytes) |
| 2010 | if sent == 0: |
| 2011 | break |
| 2012 | total_sent += sent |
| 2013 | offset += sent |
| 2014 | |
| 2015 | expected_data = b"x" * 512 + self.DATA |
| 2016 | self.assertEqual(total_sent, len(expected_data)) |
| 2017 | self.client.close() |
| 2018 | self.server.wait() |
| 2019 | data = self.server.handler_instance.get_data() |
| 2020 | self.assertEqual(hash(data), hash(expected_data)) |
| 2021 | |
| 2022 | def test_trailers(self): |
| 2023 | TESTFN2 = support.TESTFN + "2" |
Victor Stinner | 5e4d639 | 2013-08-15 11:57:02 +0200 | [diff] [blame] | 2024 | file_data = b"abcdef" |
Brett Cannon | b637680 | 2011-03-15 17:38:22 -0400 | [diff] [blame] | 2025 | with open(TESTFN2, 'wb') as f: |
Victor Stinner | 5e4d639 | 2013-08-15 11:57:02 +0200 | [diff] [blame] | 2026 | f.write(file_data) |
Brett Cannon | b637680 | 2011-03-15 17:38:22 -0400 | [diff] [blame] | 2027 | with open(TESTFN2, 'rb')as f: |
| 2028 | self.addCleanup(os.remove, TESTFN2) |
Victor Stinner | 5e4d639 | 2013-08-15 11:57:02 +0200 | [diff] [blame] | 2029 | os.sendfile(self.sockno, f.fileno(), 0, len(file_data), |
| 2030 | trailers=[b"1234"]) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2031 | self.client.close() |
| 2032 | self.server.wait() |
| 2033 | data = self.server.handler_instance.get_data() |
Victor Stinner | 5e4d639 | 2013-08-15 11:57:02 +0200 | [diff] [blame] | 2034 | self.assertEqual(data, b"abcdef1234") |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2035 | |
| 2036 | if hasattr(os, "SF_NODISKIO"): |
| 2037 | def test_flags(self): |
| 2038 | try: |
| 2039 | os.sendfile(self.sockno, self.fileno, 0, 4096, |
| 2040 | flags=os.SF_NODISKIO) |
| 2041 | except OSError as err: |
| 2042 | if err.errno not in (errno.EBUSY, errno.EAGAIN): |
| 2043 | raise |
| 2044 | |
| 2045 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2046 | def supports_extended_attributes(): |
| 2047 | if not hasattr(os, "setxattr"): |
| 2048 | return False |
| 2049 | try: |
| 2050 | with open(support.TESTFN, "wb") as fp: |
| 2051 | try: |
| 2052 | os.setxattr(fp.fileno(), b"user.test", b"") |
| 2053 | except OSError: |
| 2054 | return False |
| 2055 | finally: |
| 2056 | support.unlink(support.TESTFN) |
| 2057 | # Kernels < 2.6.39 don't respect setxattr flags. |
| 2058 | kernel_version = platform.release() |
| 2059 | m = re.match("2.6.(\d{1,2})", kernel_version) |
| 2060 | return m is None or int(m.group(1)) >= 39 |
| 2061 | |
| 2062 | |
| 2063 | @unittest.skipUnless(supports_extended_attributes(), |
| 2064 | "no non-broken extended attribute support") |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2065 | class ExtendedAttributeTests(unittest.TestCase): |
| 2066 | |
| 2067 | def tearDown(self): |
| 2068 | support.unlink(support.TESTFN) |
| 2069 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2070 | def _check_xattrs_str(self, s, getxattr, setxattr, removexattr, listxattr, **kwargs): |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2071 | fn = support.TESTFN |
| 2072 | open(fn, "wb").close() |
| 2073 | with self.assertRaises(OSError) as cm: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2074 | getxattr(fn, s("user.test"), **kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2075 | self.assertEqual(cm.exception.errno, errno.ENODATA) |
Victor Stinner | f12e506 | 2011-10-16 22:12:03 +0200 | [diff] [blame] | 2076 | init_xattr = listxattr(fn) |
| 2077 | self.assertIsInstance(init_xattr, list) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2078 | setxattr(fn, s("user.test"), b"", **kwargs) |
Victor Stinner | f12e506 | 2011-10-16 22:12:03 +0200 | [diff] [blame] | 2079 | xattr = set(init_xattr) |
| 2080 | xattr.add("user.test") |
| 2081 | self.assertEqual(set(listxattr(fn)), xattr) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2082 | self.assertEqual(getxattr(fn, b"user.test", **kwargs), b"") |
| 2083 | setxattr(fn, s("user.test"), b"hello", os.XATTR_REPLACE, **kwargs) |
| 2084 | self.assertEqual(getxattr(fn, b"user.test", **kwargs), b"hello") |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2085 | with self.assertRaises(OSError) as cm: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2086 | setxattr(fn, s("user.test"), b"bye", os.XATTR_CREATE, **kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2087 | self.assertEqual(cm.exception.errno, errno.EEXIST) |
| 2088 | with self.assertRaises(OSError) as cm: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2089 | setxattr(fn, s("user.test2"), b"bye", os.XATTR_REPLACE, **kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2090 | self.assertEqual(cm.exception.errno, errno.ENODATA) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2091 | setxattr(fn, s("user.test2"), b"foo", os.XATTR_CREATE, **kwargs) |
Victor Stinner | f12e506 | 2011-10-16 22:12:03 +0200 | [diff] [blame] | 2092 | xattr.add("user.test2") |
| 2093 | self.assertEqual(set(listxattr(fn)), xattr) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2094 | removexattr(fn, s("user.test"), **kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2095 | with self.assertRaises(OSError) as cm: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2096 | getxattr(fn, s("user.test"), **kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2097 | self.assertEqual(cm.exception.errno, errno.ENODATA) |
Victor Stinner | f12e506 | 2011-10-16 22:12:03 +0200 | [diff] [blame] | 2098 | xattr.remove("user.test") |
| 2099 | self.assertEqual(set(listxattr(fn)), xattr) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2100 | self.assertEqual(getxattr(fn, s("user.test2"), **kwargs), b"foo") |
| 2101 | setxattr(fn, s("user.test"), b"a"*1024, **kwargs) |
| 2102 | self.assertEqual(getxattr(fn, s("user.test"), **kwargs), b"a"*1024) |
| 2103 | removexattr(fn, s("user.test"), **kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2104 | many = sorted("user.test{}".format(i) for i in range(100)) |
| 2105 | for thing in many: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2106 | setxattr(fn, thing, b"x", **kwargs) |
Victor Stinner | f12e506 | 2011-10-16 22:12:03 +0200 | [diff] [blame] | 2107 | self.assertEqual(set(listxattr(fn)), set(init_xattr) | set(many)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2108 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2109 | def _check_xattrs(self, *args, **kwargs): |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2110 | def make_bytes(s): |
| 2111 | return bytes(s, "ascii") |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2112 | self._check_xattrs_str(str, *args, **kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2113 | support.unlink(support.TESTFN) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2114 | self._check_xattrs_str(make_bytes, *args, **kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2115 | |
| 2116 | def test_simple(self): |
| 2117 | self._check_xattrs(os.getxattr, os.setxattr, os.removexattr, |
| 2118 | os.listxattr) |
| 2119 | |
| 2120 | def test_lpath(self): |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2121 | self._check_xattrs(os.getxattr, os.setxattr, os.removexattr, |
| 2122 | os.listxattr, follow_symlinks=False) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2123 | |
| 2124 | def test_fds(self): |
| 2125 | def getxattr(path, *args): |
| 2126 | with open(path, "rb") as fp: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2127 | return os.getxattr(fp.fileno(), *args) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2128 | def setxattr(path, *args): |
| 2129 | with open(path, "wb") as fp: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2130 | os.setxattr(fp.fileno(), *args) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2131 | def removexattr(path, *args): |
| 2132 | with open(path, "wb") as fp: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2133 | os.removexattr(fp.fileno(), *args) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2134 | def listxattr(path, *args): |
| 2135 | with open(path, "rb") as fp: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2136 | return os.listxattr(fp.fileno(), *args) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2137 | self._check_xattrs(getxattr, setxattr, removexattr, listxattr) |
| 2138 | |
| 2139 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2140 | @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests") |
| 2141 | class Win32DeprecatedBytesAPI(unittest.TestCase): |
| 2142 | def test_deprecated(self): |
| 2143 | import nt |
| 2144 | filename = os.fsencode(support.TESTFN) |
| 2145 | with warnings.catch_warnings(): |
| 2146 | warnings.simplefilter("error", DeprecationWarning) |
| 2147 | for func, *args in ( |
| 2148 | (nt._getfullpathname, filename), |
| 2149 | (nt._isdir, filename), |
| 2150 | (os.access, filename, os.R_OK), |
| 2151 | (os.chdir, filename), |
| 2152 | (os.chmod, filename, 0o777), |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 2153 | (os.getcwdb,), |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2154 | (os.link, filename, filename), |
| 2155 | (os.listdir, filename), |
| 2156 | (os.lstat, filename), |
| 2157 | (os.mkdir, filename), |
| 2158 | (os.open, filename, os.O_RDONLY), |
| 2159 | (os.rename, filename, filename), |
| 2160 | (os.rmdir, filename), |
| 2161 | (os.startfile, filename), |
| 2162 | (os.stat, filename), |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2163 | (os.unlink, filename), |
| 2164 | (os.utime, filename), |
| 2165 | ): |
| 2166 | self.assertRaises(DeprecationWarning, func, *args) |
| 2167 | |
Victor Stinner | 2821644 | 2011-11-16 00:34:44 +0100 | [diff] [blame] | 2168 | @support.skip_unless_symlink |
| 2169 | def test_symlink(self): |
| 2170 | filename = os.fsencode(support.TESTFN) |
| 2171 | with warnings.catch_warnings(): |
| 2172 | warnings.simplefilter("error", DeprecationWarning) |
| 2173 | self.assertRaises(DeprecationWarning, |
| 2174 | os.symlink, filename, filename) |
| 2175 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2176 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 2177 | @unittest.skipUnless(hasattr(os, 'get_terminal_size'), "requires os.get_terminal_size") |
| 2178 | class TermsizeTests(unittest.TestCase): |
| 2179 | def test_does_not_crash(self): |
| 2180 | """Check if get_terminal_size() returns a meaningful value. |
| 2181 | |
| 2182 | There's no easy portable way to actually check the size of the |
| 2183 | terminal, so let's check if it returns something sensible instead. |
| 2184 | """ |
| 2185 | try: |
| 2186 | size = os.get_terminal_size() |
| 2187 | except OSError as e: |
Antoine Pitrou | 81a1fa5 | 2012-02-09 00:11:00 +0100 | [diff] [blame] | 2188 | if sys.platform == "win32" or e.errno in (errno.EINVAL, errno.ENOTTY): |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 2189 | # Under win32 a generic OSError can be thrown if the |
| 2190 | # handle cannot be retrieved |
| 2191 | self.skipTest("failed to query terminal size") |
| 2192 | raise |
| 2193 | |
Antoine Pitrou | cfade36 | 2012-02-08 23:48:59 +0100 | [diff] [blame] | 2194 | self.assertGreaterEqual(size.columns, 0) |
| 2195 | self.assertGreaterEqual(size.lines, 0) |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 2196 | |
| 2197 | def test_stty_match(self): |
| 2198 | """Check if stty returns the same results |
| 2199 | |
| 2200 | stty actually tests stdin, so get_terminal_size is invoked on |
| 2201 | stdin explicitly. If stty succeeded, then get_terminal_size() |
| 2202 | should work too. |
| 2203 | """ |
| 2204 | try: |
| 2205 | size = subprocess.check_output(['stty', 'size']).decode().split() |
| 2206 | except (FileNotFoundError, subprocess.CalledProcessError): |
| 2207 | self.skipTest("stty invocation failed") |
| 2208 | expected = (int(size[1]), int(size[0])) # reversed order |
| 2209 | |
Antoine Pitrou | 81a1fa5 | 2012-02-09 00:11:00 +0100 | [diff] [blame] | 2210 | try: |
| 2211 | actual = os.get_terminal_size(sys.__stdin__.fileno()) |
| 2212 | except OSError as e: |
| 2213 | if sys.platform == "win32" or e.errno in (errno.EINVAL, errno.ENOTTY): |
| 2214 | # Under win32 a generic OSError can be thrown if the |
| 2215 | # handle cannot be retrieved |
| 2216 | self.skipTest("failed to query terminal size") |
| 2217 | raise |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 2218 | self.assertEqual(expected, actual) |
| 2219 | |
| 2220 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2221 | class OSErrorTests(unittest.TestCase): |
| 2222 | def setUp(self): |
| 2223 | class Str(str): |
| 2224 | pass |
| 2225 | |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 2226 | self.bytes_filenames = [] |
| 2227 | self.unicode_filenames = [] |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2228 | if support.TESTFN_UNENCODABLE is not None: |
| 2229 | decoded = support.TESTFN_UNENCODABLE |
| 2230 | else: |
| 2231 | decoded = support.TESTFN |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 2232 | self.unicode_filenames.append(decoded) |
| 2233 | self.unicode_filenames.append(Str(decoded)) |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2234 | if support.TESTFN_UNDECODABLE is not None: |
| 2235 | encoded = support.TESTFN_UNDECODABLE |
| 2236 | else: |
| 2237 | encoded = os.fsencode(support.TESTFN) |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 2238 | self.bytes_filenames.append(encoded) |
| 2239 | self.bytes_filenames.append(memoryview(encoded)) |
| 2240 | |
| 2241 | self.filenames = self.bytes_filenames + self.unicode_filenames |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2242 | |
| 2243 | def test_oserror_filename(self): |
| 2244 | funcs = [ |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 2245 | (self.filenames, os.chdir,), |
| 2246 | (self.filenames, os.chmod, 0o777), |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 2247 | (self.filenames, os.lstat,), |
| 2248 | (self.filenames, os.open, os.O_RDONLY), |
| 2249 | (self.filenames, os.rmdir,), |
| 2250 | (self.filenames, os.stat,), |
| 2251 | (self.filenames, os.unlink,), |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2252 | ] |
| 2253 | if sys.platform == "win32": |
| 2254 | funcs.extend(( |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 2255 | (self.bytes_filenames, os.rename, b"dst"), |
| 2256 | (self.bytes_filenames, os.replace, b"dst"), |
| 2257 | (self.unicode_filenames, os.rename, "dst"), |
| 2258 | (self.unicode_filenames, os.replace, "dst"), |
Victor Stinner | 64e039a | 2012-11-07 00:10:14 +0100 | [diff] [blame] | 2259 | # Issue #16414: Don't test undecodable names with listdir() |
| 2260 | # because of a Windows bug. |
| 2261 | # |
| 2262 | # With the ANSI code page 932, os.listdir(b'\xe7') return an |
| 2263 | # empty list (instead of failing), whereas os.listdir(b'\xff') |
| 2264 | # raises a FileNotFoundError. It looks like a Windows bug: |
| 2265 | # b'\xe7' directory does not exist, FindFirstFileA(b'\xe7') |
| 2266 | # fails with ERROR_FILE_NOT_FOUND (2), instead of |
| 2267 | # ERROR_PATH_NOT_FOUND (3). |
| 2268 | (self.unicode_filenames, os.listdir,), |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2269 | )) |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 2270 | else: |
| 2271 | funcs.extend(( |
Victor Stinner | 64e039a | 2012-11-07 00:10:14 +0100 | [diff] [blame] | 2272 | (self.filenames, os.listdir,), |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 2273 | (self.filenames, os.rename, "dst"), |
| 2274 | (self.filenames, os.replace, "dst"), |
| 2275 | )) |
| 2276 | if hasattr(os, "chown"): |
| 2277 | funcs.append((self.filenames, os.chown, 0, 0)) |
| 2278 | if hasattr(os, "lchown"): |
| 2279 | funcs.append((self.filenames, os.lchown, 0, 0)) |
| 2280 | if hasattr(os, "truncate"): |
| 2281 | funcs.append((self.filenames, os.truncate, 0)) |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2282 | if hasattr(os, "chflags"): |
Victor Stinner | ee36c24 | 2012-11-13 09:31:51 +0100 | [diff] [blame] | 2283 | funcs.append((self.filenames, os.chflags, 0)) |
| 2284 | if hasattr(os, "lchflags"): |
| 2285 | funcs.append((self.filenames, os.lchflags, 0)) |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2286 | if hasattr(os, "chroot"): |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 2287 | funcs.append((self.filenames, os.chroot,)) |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2288 | if hasattr(os, "link"): |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 2289 | if sys.platform == "win32": |
| 2290 | funcs.append((self.bytes_filenames, os.link, b"dst")) |
| 2291 | funcs.append((self.unicode_filenames, os.link, "dst")) |
| 2292 | else: |
| 2293 | funcs.append((self.filenames, os.link, "dst")) |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2294 | if hasattr(os, "listxattr"): |
| 2295 | funcs.extend(( |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 2296 | (self.filenames, os.listxattr,), |
| 2297 | (self.filenames, os.getxattr, "user.test"), |
| 2298 | (self.filenames, os.setxattr, "user.test", b'user'), |
| 2299 | (self.filenames, os.removexattr, "user.test"), |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2300 | )) |
| 2301 | if hasattr(os, "lchmod"): |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 2302 | funcs.append((self.filenames, os.lchmod, 0o777)) |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2303 | if hasattr(os, "readlink"): |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 2304 | if sys.platform == "win32": |
| 2305 | funcs.append((self.unicode_filenames, os.readlink,)) |
| 2306 | else: |
| 2307 | funcs.append((self.filenames, os.readlink,)) |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2308 | |
Victor Stinner | afe1706 | 2012-10-31 22:47:43 +0100 | [diff] [blame] | 2309 | for filenames, func, *func_args in funcs: |
| 2310 | for name in filenames: |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2311 | try: |
| 2312 | func(name, *func_args) |
Victor Stinner | bd54f0e | 2012-10-31 01:12:55 +0100 | [diff] [blame] | 2313 | except OSError as err: |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2314 | self.assertIs(err.filename, name) |
| 2315 | else: |
| 2316 | self.fail("No exception thrown by {}".format(func)) |
| 2317 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 2318 | class CPUCountTests(unittest.TestCase): |
| 2319 | def test_cpu_count(self): |
| 2320 | cpus = os.cpu_count() |
| 2321 | if cpus is not None: |
| 2322 | self.assertIsInstance(cpus, int) |
| 2323 | self.assertGreater(cpus, 0) |
| 2324 | else: |
| 2325 | self.skipTest("Could not determine the number of CPUs") |
| 2326 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2327 | |
| 2328 | class FDInheritanceTests(unittest.TestCase): |
Victor Stinner | 7ba6b0f | 2013-09-08 11:47:54 +0200 | [diff] [blame] | 2329 | def test_get_set_inheritable(self): |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2330 | fd = os.open(__file__, os.O_RDONLY) |
| 2331 | self.addCleanup(os.close, fd) |
Victor Stinner | 7ba6b0f | 2013-09-08 11:47:54 +0200 | [diff] [blame] | 2332 | self.assertEqual(os.get_inheritable(fd), False) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2333 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2334 | os.set_inheritable(fd, True) |
| 2335 | self.assertEqual(os.get_inheritable(fd), True) |
| 2336 | |
Victor Stinner | 4f7a36f | 2013-09-08 14:14:38 +0200 | [diff] [blame] | 2337 | @unittest.skipIf(fcntl is None, "need fcntl") |
| 2338 | def test_get_inheritable_cloexec(self): |
| 2339 | fd = os.open(__file__, os.O_RDONLY) |
| 2340 | self.addCleanup(os.close, fd) |
| 2341 | self.assertEqual(os.get_inheritable(fd), False) |
Victor Stinner | 7ba6b0f | 2013-09-08 11:47:54 +0200 | [diff] [blame] | 2342 | |
Victor Stinner | 4f7a36f | 2013-09-08 14:14:38 +0200 | [diff] [blame] | 2343 | # clear FD_CLOEXEC flag |
| 2344 | flags = fcntl.fcntl(fd, fcntl.F_GETFD) |
| 2345 | flags &= ~fcntl.FD_CLOEXEC |
| 2346 | fcntl.fcntl(fd, fcntl.F_SETFD, flags) |
Victor Stinner | 7ba6b0f | 2013-09-08 11:47:54 +0200 | [diff] [blame] | 2347 | |
Victor Stinner | 4f7a36f | 2013-09-08 14:14:38 +0200 | [diff] [blame] | 2348 | self.assertEqual(os.get_inheritable(fd), True) |
Victor Stinner | 7ba6b0f | 2013-09-08 11:47:54 +0200 | [diff] [blame] | 2349 | |
Victor Stinner | 4f7a36f | 2013-09-08 14:14:38 +0200 | [diff] [blame] | 2350 | @unittest.skipIf(fcntl is None, "need fcntl") |
| 2351 | def test_set_inheritable_cloexec(self): |
| 2352 | fd = os.open(__file__, os.O_RDONLY) |
| 2353 | self.addCleanup(os.close, fd) |
| 2354 | self.assertEqual(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC, |
| 2355 | fcntl.FD_CLOEXEC) |
Victor Stinner | 7ba6b0f | 2013-09-08 11:47:54 +0200 | [diff] [blame] | 2356 | |
Victor Stinner | 4f7a36f | 2013-09-08 14:14:38 +0200 | [diff] [blame] | 2357 | os.set_inheritable(fd, True) |
| 2358 | self.assertEqual(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC, |
| 2359 | 0) |
Victor Stinner | 7ba6b0f | 2013-09-08 11:47:54 +0200 | [diff] [blame] | 2360 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2361 | def test_open(self): |
| 2362 | fd = os.open(__file__, os.O_RDONLY) |
| 2363 | self.addCleanup(os.close, fd) |
| 2364 | self.assertEqual(os.get_inheritable(fd), False) |
| 2365 | |
| 2366 | @unittest.skipUnless(hasattr(os, 'pipe'), "need os.pipe()") |
| 2367 | def test_pipe(self): |
| 2368 | rfd, wfd = os.pipe() |
| 2369 | self.addCleanup(os.close, rfd) |
| 2370 | self.addCleanup(os.close, wfd) |
| 2371 | self.assertEqual(os.get_inheritable(rfd), False) |
| 2372 | self.assertEqual(os.get_inheritable(wfd), False) |
| 2373 | |
| 2374 | def test_dup(self): |
| 2375 | fd1 = os.open(__file__, os.O_RDONLY) |
| 2376 | self.addCleanup(os.close, fd1) |
| 2377 | |
| 2378 | fd2 = os.dup(fd1) |
| 2379 | self.addCleanup(os.close, fd2) |
| 2380 | self.assertEqual(os.get_inheritable(fd2), False) |
| 2381 | |
| 2382 | @unittest.skipUnless(hasattr(os, 'dup2'), "need os.dup2()") |
| 2383 | def test_dup2(self): |
| 2384 | fd = os.open(__file__, os.O_RDONLY) |
| 2385 | self.addCleanup(os.close, fd) |
| 2386 | |
| 2387 | # inheritable by default |
| 2388 | fd2 = os.open(__file__, os.O_RDONLY) |
| 2389 | try: |
| 2390 | os.dup2(fd, fd2) |
| 2391 | self.assertEqual(os.get_inheritable(fd2), True) |
| 2392 | finally: |
| 2393 | os.close(fd2) |
| 2394 | |
| 2395 | # force non-inheritable |
| 2396 | fd3 = os.open(__file__, os.O_RDONLY) |
| 2397 | try: |
| 2398 | os.dup2(fd, fd3, inheritable=False) |
| 2399 | self.assertEqual(os.get_inheritable(fd3), False) |
| 2400 | finally: |
| 2401 | os.close(fd3) |
| 2402 | |
| 2403 | @unittest.skipUnless(hasattr(os, 'openpty'), "need os.openpty()") |
| 2404 | def test_openpty(self): |
| 2405 | master_fd, slave_fd = os.openpty() |
| 2406 | self.addCleanup(os.close, master_fd) |
| 2407 | self.addCleanup(os.close, slave_fd) |
| 2408 | self.assertEqual(os.get_inheritable(master_fd), False) |
| 2409 | self.assertEqual(os.get_inheritable(slave_fd), False) |
| 2410 | |
| 2411 | |
Antoine Pitrou | f26ad71 | 2011-07-15 23:00:56 +0200 | [diff] [blame] | 2412 | @support.reap_threads |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 2413 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2414 | support.run_unittest( |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2415 | FileTests, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 2416 | StatAttributeTests, |
| 2417 | EnvironTests, |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 2418 | WalkTests, |
Charles-François Natali | 7372b06 | 2012-02-05 15:15:38 +0100 | [diff] [blame] | 2419 | FwalkTests, |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 2420 | MakedirTests, |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 2421 | DevNullTests, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2422 | URandomTests, |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2423 | ExecTests, |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 2424 | Win32ErrorTests, |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 2425 | TestInvalidFD, |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2426 | PosixUidGidTests, |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 2427 | Pep383Tests, |
Victor Stinner | bf9bcab | 2010-05-09 03:15:33 +0000 | [diff] [blame] | 2428 | Win32KillTests, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2429 | Win32SymlinkTests, |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 2430 | NonLocalSymlinkTests, |
Victor Stinner | e8d5145 | 2010-08-19 01:05:19 +0000 | [diff] [blame] | 2431 | FSEncodingTests, |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 2432 | DeviceEncodingTests, |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 2433 | PidTests, |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 2434 | LoginTests, |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 2435 | LinkTests, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2436 | TestSendfile, |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 2437 | ProgramPriorityTests, |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 2438 | ExtendedAttributeTests, |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 2439 | Win32DeprecatedBytesAPI, |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 2440 | TermsizeTests, |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2441 | OSErrorTests, |
Andrew Svetlov | 405faed | 2012-12-25 12:18:09 +0200 | [diff] [blame] | 2442 | RemoveDirsTests, |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 2443 | CPUCountTests, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2444 | FDInheritanceTests, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 2445 | ) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 2446 | |
| 2447 | if __name__ == "__main__": |
| 2448 | test_main() |