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 | 1de05e9 | 2009-01-31 01:42:55 +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 |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 9 | import sys |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 10 | import signal |
| 11 | import subprocess |
Benjamin Peterson | 27c269a | 2014-12-26 11:09:00 -0600 | [diff] [blame] | 12 | import sysconfig |
Victor Stinner | e0a0bd6 | 2015-02-24 14:30:43 +0100 | [diff] [blame] | 13 | import textwrap |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 14 | import time |
Antoine Pitrou | f48a67b | 2013-08-16 20:44:38 +0200 | [diff] [blame] | 15 | try: |
| 16 | import resource |
| 17 | except ImportError: |
| 18 | resource = None |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 19 | |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 20 | from test import test_support |
Antoine Pitrou | e758715 | 2013-08-24 20:52:27 +0200 | [diff] [blame] | 21 | from test.script_helper import assert_python_ok |
Hirokazu Yamamoto | 1f504f1 | 2010-10-08 09:41:13 +0000 | [diff] [blame] | 22 | import mmap |
| 23 | import uuid |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 24 | |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 25 | warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, __name__) |
| 26 | warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, __name__) |
| 27 | |
Martin v. Löwis | ee1e06d | 2006-07-02 18:44:00 +0000 | [diff] [blame] | 28 | # Tests creating TESTFN |
| 29 | class FileTests(unittest.TestCase): |
| 30 | def setUp(self): |
| 31 | if os.path.exists(test_support.TESTFN): |
| 32 | os.unlink(test_support.TESTFN) |
| 33 | tearDown = setUp |
| 34 | |
| 35 | def test_access(self): |
| 36 | f = os.open(test_support.TESTFN, os.O_CREAT|os.O_RDWR) |
| 37 | os.close(f) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 38 | self.assertTrue(os.access(test_support.TESTFN, os.W_OK)) |
Tim Peters | 16a3932 | 2006-07-03 08:23:19 +0000 | [diff] [blame] | 39 | |
Georg Brandl | 309501a | 2008-01-19 20:22:13 +0000 | [diff] [blame] | 40 | def test_closerange(self): |
Antoine Pitrou | bebb18b | 2008-08-17 14:43:41 +0000 | [diff] [blame] | 41 | first = os.open(test_support.TESTFN, os.O_CREAT|os.O_RDWR) |
| 42 | # We must allocate two consecutive file descriptors, otherwise |
| 43 | # it will mess up other file descriptors (perhaps even the three |
| 44 | # standard ones). |
| 45 | second = os.dup(first) |
| 46 | try: |
| 47 | retries = 0 |
| 48 | while second != first + 1: |
| 49 | os.close(first) |
| 50 | retries += 1 |
| 51 | if retries > 10: |
| 52 | # XXX test skipped |
Benjamin Peterson | 757b3c9 | 2009-05-16 18:44:34 +0000 | [diff] [blame] | 53 | self.skipTest("couldn't allocate two consecutive fds") |
Antoine Pitrou | bebb18b | 2008-08-17 14:43:41 +0000 | [diff] [blame] | 54 | first, second = second, os.dup(second) |
| 55 | finally: |
| 56 | os.close(second) |
Georg Brandl | 309501a | 2008-01-19 20:22:13 +0000 | [diff] [blame] | 57 | # close a fd that is open, and one that isn't |
Antoine Pitrou | bebb18b | 2008-08-17 14:43:41 +0000 | [diff] [blame] | 58 | os.closerange(first, first + 2) |
| 59 | self.assertRaises(OSError, os.write, first, "a") |
Georg Brandl | 309501a | 2008-01-19 20:22:13 +0000 | [diff] [blame] | 60 | |
Benjamin Peterson | 10947a6 | 2010-06-30 17:11:08 +0000 | [diff] [blame] | 61 | @test_support.cpython_only |
Hirokazu Yamamoto | 74ce88f | 2008-09-08 23:03:47 +0000 | [diff] [blame] | 62 | def test_rename(self): |
| 63 | path = unicode(test_support.TESTFN) |
| 64 | old = sys.getrefcount(path) |
| 65 | self.assertRaises(TypeError, os.rename, path, 0) |
| 66 | new = sys.getrefcount(path) |
| 67 | self.assertEqual(old, new) |
| 68 | |
Martin v. Löwis | ee1e06d | 2006-07-02 18:44:00 +0000 | [diff] [blame] | 69 | |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 70 | class TemporaryFileTests(unittest.TestCase): |
| 71 | def setUp(self): |
| 72 | self.files = [] |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 73 | os.mkdir(test_support.TESTFN) |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 74 | |
| 75 | def tearDown(self): |
| 76 | for name in self.files: |
| 77 | os.unlink(name) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 78 | os.rmdir(test_support.TESTFN) |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 79 | |
| 80 | def check_tempfile(self, name): |
| 81 | # make sure it doesn't already exist: |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 82 | self.assertFalse(os.path.exists(name), |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 83 | "file already exists for temporary file") |
| 84 | # make sure we can create the file |
| 85 | open(name, "w") |
| 86 | self.files.append(name) |
| 87 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 88 | @unittest.skipUnless(hasattr(os, 'tempnam'), 'test needs os.tempnam()') |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 89 | def test_tempnam(self): |
Antoine Pitrou | b061461 | 2011-01-02 20:04:52 +0000 | [diff] [blame] | 90 | with warnings.catch_warnings(): |
| 91 | warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, |
| 92 | r"test_os$") |
| 93 | warnings.filterwarnings("ignore", "tempnam", DeprecationWarning) |
| 94 | self.check_tempfile(os.tempnam()) |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 95 | |
Antoine Pitrou | b061461 | 2011-01-02 20:04:52 +0000 | [diff] [blame] | 96 | name = os.tempnam(test_support.TESTFN) |
| 97 | self.check_tempfile(name) |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 98 | |
Antoine Pitrou | b061461 | 2011-01-02 20:04:52 +0000 | [diff] [blame] | 99 | name = os.tempnam(test_support.TESTFN, "pfx") |
| 100 | self.assertTrue(os.path.basename(name)[:3] == "pfx") |
| 101 | self.check_tempfile(name) |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 102 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 103 | @unittest.skipUnless(hasattr(os, 'tmpfile'), 'test needs os.tmpfile()') |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 104 | def test_tmpfile(self): |
Martin v. Löwis | d2bbe52 | 2008-03-06 06:55:22 +0000 | [diff] [blame] | 105 | # As with test_tmpnam() below, the Windows implementation of tmpfile() |
| 106 | # attempts to create a file in the root directory of the current drive. |
| 107 | # On Vista and Server 2008, this test will always fail for normal users |
| 108 | # as writing to the root directory requires elevated privileges. With |
| 109 | # XP and below, the semantics of tmpfile() are the same, but the user |
| 110 | # running the test is more likely to have administrative privileges on |
| 111 | # their account already. If that's the case, then os.tmpfile() should |
| 112 | # work. In order to make this test as useful as possible, rather than |
| 113 | # trying to detect Windows versions or whether or not the user has the |
| 114 | # right permissions, just try and create a file in the root directory |
| 115 | # and see if it raises a 'Permission denied' OSError. If it does, then |
| 116 | # test that a subsequent call to os.tmpfile() raises the same error. If |
| 117 | # it doesn't, assume we're on XP or below and the user running the test |
| 118 | # has administrative privileges, and proceed with the test as normal. |
Antoine Pitrou | b061461 | 2011-01-02 20:04:52 +0000 | [diff] [blame] | 119 | with warnings.catch_warnings(): |
| 120 | warnings.filterwarnings("ignore", "tmpfile", DeprecationWarning) |
Martin v. Löwis | d2bbe52 | 2008-03-06 06:55:22 +0000 | [diff] [blame] | 121 | |
Antoine Pitrou | b061461 | 2011-01-02 20:04:52 +0000 | [diff] [blame] | 122 | if sys.platform == 'win32': |
| 123 | name = '\\python_test_os_test_tmpfile.txt' |
| 124 | if os.path.exists(name): |
| 125 | os.remove(name) |
| 126 | try: |
| 127 | fp = open(name, 'w') |
| 128 | except IOError, first: |
| 129 | # open() failed, assert tmpfile() fails in the same way. |
| 130 | # Although open() raises an IOError and os.tmpfile() raises an |
| 131 | # OSError(), 'args' will be (13, 'Permission denied') in both |
| 132 | # cases. |
| 133 | try: |
| 134 | fp = os.tmpfile() |
| 135 | except OSError, second: |
| 136 | self.assertEqual(first.args, second.args) |
| 137 | else: |
| 138 | self.fail("expected os.tmpfile() to raise OSError") |
Zachary Ware | b06231a | 2013-12-10 16:06:46 -0600 | [diff] [blame] | 139 | return |
Antoine Pitrou | b061461 | 2011-01-02 20:04:52 +0000 | [diff] [blame] | 140 | else: |
| 141 | # open() worked, therefore, tmpfile() should work. Close our |
| 142 | # dummy file and proceed with the test as normal. |
| 143 | fp.close() |
| 144 | os.remove(name) |
| 145 | |
| 146 | fp = os.tmpfile() |
| 147 | fp.write("foobar") |
| 148 | fp.seek(0,0) |
| 149 | s = fp.read() |
| 150 | fp.close() |
| 151 | self.assertTrue(s == "foobar") |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 152 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 153 | @unittest.skipUnless(hasattr(os, 'tmpnam'), 'test needs os.tmpnam()') |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 154 | def test_tmpnam(self): |
Antoine Pitrou | b061461 | 2011-01-02 20:04:52 +0000 | [diff] [blame] | 155 | with warnings.catch_warnings(): |
| 156 | warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, |
| 157 | r"test_os$") |
| 158 | warnings.filterwarnings("ignore", "tmpnam", DeprecationWarning) |
| 159 | |
| 160 | name = os.tmpnam() |
| 161 | if sys.platform in ("win32",): |
| 162 | # The Windows tmpnam() seems useless. From the MS docs: |
| 163 | # |
| 164 | # The character string that tmpnam creates consists of |
| 165 | # the path prefix, defined by the entry P_tmpdir in the |
| 166 | # file STDIO.H, followed by a sequence consisting of the |
| 167 | # digit characters '0' through '9'; the numerical value |
| 168 | # of this string is in the range 1 - 65,535. Changing the |
| 169 | # definitions of L_tmpnam or P_tmpdir in STDIO.H does not |
| 170 | # change the operation of tmpnam. |
| 171 | # |
| 172 | # The really bizarre part is that, at least under MSVC6, |
| 173 | # P_tmpdir is "\\". That is, the path returned refers to |
| 174 | # the root of the current drive. That's a terrible place to |
| 175 | # put temp files, and, depending on privileges, the user |
| 176 | # may not even be able to open a file in the root directory. |
| 177 | self.assertFalse(os.path.exists(name), |
| 178 | "file already exists for temporary file") |
| 179 | else: |
| 180 | self.check_tempfile(name) |
Tim Peters | 87cc0c3 | 2001-07-21 01:41:30 +0000 | [diff] [blame] | 181 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 182 | # Test attributes on return values from os.*stat* family. |
| 183 | class StatAttributeTests(unittest.TestCase): |
| 184 | def setUp(self): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 185 | os.mkdir(test_support.TESTFN) |
| 186 | self.fname = os.path.join(test_support.TESTFN, "f1") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 187 | f = open(self.fname, 'wb') |
| 188 | f.write("ABC") |
| 189 | f.close() |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 190 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 191 | def tearDown(self): |
| 192 | os.unlink(self.fname) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 193 | os.rmdir(test_support.TESTFN) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 194 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 195 | @unittest.skipUnless(hasattr(os, 'stat'), 'test needs os.stat()') |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 196 | def test_stat_attributes(self): |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 197 | import stat |
| 198 | result = os.stat(self.fname) |
| 199 | |
| 200 | # Make sure direct access works |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 201 | self.assertEqual(result[stat.ST_SIZE], 3) |
| 202 | self.assertEqual(result.st_size, 3) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 204 | # Make sure all the attributes are there |
| 205 | members = dir(result) |
| 206 | for name in dir(stat): |
| 207 | if name[:3] == 'ST_': |
| 208 | attr = name.lower() |
Martin v. Löwis | 4d394df | 2005-01-23 09:19:22 +0000 | [diff] [blame] | 209 | if name.endswith("TIME"): |
| 210 | def trunc(x): return int(x) |
| 211 | else: |
| 212 | def trunc(x): return x |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 213 | self.assertEqual(trunc(getattr(result, attr)), |
| 214 | result[getattr(stat, name)]) |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 215 | self.assertIn(attr, members) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 216 | |
| 217 | try: |
| 218 | result[200] |
Andrew Svetlov | 4bb142b | 2012-12-18 21:27:37 +0200 | [diff] [blame] | 219 | self.fail("No exception raised") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 220 | except IndexError: |
| 221 | pass |
| 222 | |
| 223 | # Make sure that assignment fails |
| 224 | try: |
| 225 | result.st_mode = 1 |
Andrew Svetlov | 4bb142b | 2012-12-18 21:27:37 +0200 | [diff] [blame] | 226 | self.fail("No exception raised") |
Benjamin Peterson | c262a69 | 2010-06-30 18:41:08 +0000 | [diff] [blame] | 227 | except (AttributeError, TypeError): |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 228 | pass |
| 229 | |
| 230 | try: |
| 231 | result.st_rdev = 1 |
Andrew Svetlov | 4bb142b | 2012-12-18 21:27:37 +0200 | [diff] [blame] | 232 | self.fail("No exception raised") |
Guido van Rossum | 1fff878 | 2001-10-18 21:19:31 +0000 | [diff] [blame] | 233 | except (AttributeError, TypeError): |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 234 | pass |
| 235 | |
| 236 | try: |
| 237 | result.parrot = 1 |
Andrew Svetlov | 4bb142b | 2012-12-18 21:27:37 +0200 | [diff] [blame] | 238 | self.fail("No exception raised") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 239 | except AttributeError: |
| 240 | pass |
| 241 | |
| 242 | # Use the stat_result constructor with a too-short tuple. |
| 243 | try: |
| 244 | result2 = os.stat_result((10,)) |
Andrew Svetlov | 4bb142b | 2012-12-18 21:27:37 +0200 | [diff] [blame] | 245 | self.fail("No exception raised") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 246 | except TypeError: |
| 247 | pass |
| 248 | |
Ezio Melotti | 24b07bc | 2011-03-15 18:55:01 +0200 | [diff] [blame] | 249 | # Use the constructor with a too-long tuple. |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 250 | try: |
| 251 | result2 = os.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)) |
| 252 | except TypeError: |
| 253 | pass |
| 254 | |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 255 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 256 | @unittest.skipUnless(hasattr(os, 'statvfs'), 'test needs os.statvfs()') |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 257 | def test_statvfs_attributes(self): |
Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 258 | try: |
| 259 | result = os.statvfs(self.fname) |
| 260 | except OSError, e: |
| 261 | # On AtheOS, glibc always returns ENOSYS |
Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 262 | if e.errno == errno.ENOSYS: |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 263 | self.skipTest('glibc always returns ENOSYS on AtheOS') |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 264 | |
| 265 | # Make sure direct access works |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 266 | self.assertEqual(result.f_bfree, result[3]) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 267 | |
Brett Cannon | 90f2cb4 | 2008-05-16 00:37:42 +0000 | [diff] [blame] | 268 | # Make sure all the attributes are there. |
| 269 | members = ('bsize', 'frsize', 'blocks', 'bfree', 'bavail', 'files', |
| 270 | 'ffree', 'favail', 'flag', 'namemax') |
| 271 | for value, member in enumerate(members): |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 272 | self.assertEqual(getattr(result, 'f_' + member), result[value]) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 273 | |
| 274 | # Make sure that assignment really fails |
| 275 | try: |
| 276 | result.f_bfree = 1 |
Andrew Svetlov | 4bb142b | 2012-12-18 21:27:37 +0200 | [diff] [blame] | 277 | self.fail("No exception raised") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 278 | except TypeError: |
| 279 | pass |
| 280 | |
| 281 | try: |
| 282 | result.parrot = 1 |
Andrew Svetlov | 4bb142b | 2012-12-18 21:27:37 +0200 | [diff] [blame] | 283 | self.fail("No exception raised") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 284 | except AttributeError: |
| 285 | pass |
| 286 | |
| 287 | # Use the constructor with a too-short tuple. |
| 288 | try: |
| 289 | result2 = os.statvfs_result((10,)) |
Andrew Svetlov | 4bb142b | 2012-12-18 21:27:37 +0200 | [diff] [blame] | 290 | self.fail("No exception raised") |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 291 | except TypeError: |
| 292 | pass |
| 293 | |
Ezio Melotti | 24b07bc | 2011-03-15 18:55:01 +0200 | [diff] [blame] | 294 | # Use the constructor with a too-long tuple. |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 295 | try: |
| 296 | result2 = os.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14)) |
| 297 | except TypeError: |
| 298 | pass |
Fred Drake | 38c2ef0 | 2001-07-17 20:52:51 +0000 | [diff] [blame] | 299 | |
Martin v. Löwis | 18aaa56 | 2006-10-15 08:43:33 +0000 | [diff] [blame] | 300 | def test_utime_dir(self): |
| 301 | delta = 1000000 |
| 302 | st = os.stat(test_support.TESTFN) |
Martin v. Löwis | a97e06d | 2006-10-15 11:02:07 +0000 | [diff] [blame] | 303 | # round to int, because some systems may support sub-second |
| 304 | # time stamps in stat, but not in utime. |
| 305 | os.utime(test_support.TESTFN, (st.st_atime, int(st.st_mtime-delta))) |
Martin v. Löwis | 18aaa56 | 2006-10-15 08:43:33 +0000 | [diff] [blame] | 306 | st2 = os.stat(test_support.TESTFN) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 307 | self.assertEqual(st2.st_mtime, int(st.st_mtime-delta)) |
Martin v. Löwis | 18aaa56 | 2006-10-15 08:43:33 +0000 | [diff] [blame] | 308 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 309 | # Restrict tests to Win32, since there is no guarantee other |
Martin v. Löwis | f43893a | 2006-10-09 20:44:25 +0000 | [diff] [blame] | 310 | # systems support centiseconds |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 311 | def get_file_system(path): |
| 312 | if sys.platform == 'win32': |
Hirokazu Yamamoto | ccfdcd0 | 2008-08-20 04:13:28 +0000 | [diff] [blame] | 313 | root = os.path.splitdrive(os.path.abspath(path))[0] + '\\' |
Martin v. Löwis | 7dcb83c | 2007-08-30 19:04:09 +0000 | [diff] [blame] | 314 | import ctypes |
Hirokazu Yamamoto | cd3b74d | 2008-08-20 16:15:28 +0000 | [diff] [blame] | 315 | kernel32 = ctypes.windll.kernel32 |
| 316 | buf = ctypes.create_string_buffer("", 100) |
| 317 | if kernel32.GetVolumeInformationA(root, None, 0, None, None, None, buf, len(buf)): |
Martin v. Löwis | 7dcb83c | 2007-08-30 19:04:09 +0000 | [diff] [blame] | 318 | return buf.value |
| 319 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 320 | @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests") |
Serhiy Storchaka | d412b49 | 2013-11-03 23:25:42 +0200 | [diff] [blame] | 321 | @unittest.skipUnless(get_file_system(test_support.TESTFN) == "NTFS", |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 322 | "requires NTFS") |
| 323 | def test_1565150(self): |
| 324 | t1 = 1159195039.25 |
| 325 | os.utime(self.fname, (t1, t1)) |
| 326 | self.assertEqual(os.stat(self.fname).st_mtime, t1) |
Martin v. Löwis | f43893a | 2006-10-09 20:44:25 +0000 | [diff] [blame] | 327 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 328 | @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests") |
Serhiy Storchaka | d412b49 | 2013-11-03 23:25:42 +0200 | [diff] [blame] | 329 | @unittest.skipUnless(get_file_system(test_support.TESTFN) == "NTFS", |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 330 | "requires NTFS") |
| 331 | def test_large_time(self): |
| 332 | t1 = 5000000000 # some day in 2128 |
| 333 | os.utime(self.fname, (t1, t1)) |
| 334 | self.assertEqual(os.stat(self.fname).st_mtime, t1) |
Amaury Forgeot d'Arc | ac514c8 | 2011-01-03 00:50:57 +0000 | [diff] [blame] | 335 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 336 | @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests") |
| 337 | def test_1686475(self): |
| 338 | # Verify that an open file can be stat'ed |
| 339 | try: |
| 340 | os.stat(r"c:\pagefile.sys") |
| 341 | except WindowsError, e: |
| 342 | if e.errno == 2: # file does not exist; cannot run test |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 343 | self.skipTest(r'c:\pagefile.sys does not exist') |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 344 | self.fail("Could not stat pagefile.sys") |
Martin v. Löwis | 3bf573f | 2007-04-04 18:30:36 +0000 | [diff] [blame] | 345 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 346 | from test import mapping_tests |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 347 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 348 | class EnvironTests(mapping_tests.BasicTestMappingProtocol): |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 349 | """check that os.environ object conform to mapping protocol""" |
Walter Dörwald | 118f931 | 2004-06-02 18:42:25 +0000 | [diff] [blame] | 350 | type2test = None |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 351 | def _reference(self): |
| 352 | return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"} |
| 353 | def _empty_mapping(self): |
| 354 | os.environ.clear() |
| 355 | return os.environ |
| 356 | def setUp(self): |
| 357 | self.__save = dict(os.environ) |
| 358 | os.environ.clear() |
| 359 | def tearDown(self): |
| 360 | os.environ.clear() |
| 361 | os.environ.update(self.__save) |
| 362 | |
Martin v. Löwis | 1d11de6 | 2005-01-29 13:29:23 +0000 | [diff] [blame] | 363 | # Bug 1110478 |
Martin v. Löwis | 5510f65 | 2005-02-17 21:23:20 +0000 | [diff] [blame] | 364 | def test_update2(self): |
Martin v. Löwis | 1d11de6 | 2005-01-29 13:29:23 +0000 | [diff] [blame] | 365 | if os.path.exists("/bin/sh"): |
| 366 | os.environ.update(HELLO="World") |
Brian Curtin | fcbf5d0 | 2010-10-30 21:29:52 +0000 | [diff] [blame] | 367 | with os.popen("/bin/sh -c 'echo $HELLO'") as popen: |
| 368 | value = popen.read().strip() |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 369 | self.assertEqual(value, "World") |
Martin v. Löwis | 1d11de6 | 2005-01-29 13:29:23 +0000 | [diff] [blame] | 370 | |
Charles-François Natali | 27bc4d0 | 2011-11-27 13:05:14 +0100 | [diff] [blame] | 371 | # On FreeBSD < 7 and OS X < 10.6, unsetenv() doesn't return a value (issue |
| 372 | # #13415). |
| 373 | @unittest.skipIf(sys.platform.startswith(('freebsd', 'darwin')), |
| 374 | "due to known OS bug: see issue #13415") |
Victor Stinner | 53853c3 | 2011-11-22 22:20:13 +0100 | [diff] [blame] | 375 | def test_unset_error(self): |
| 376 | if sys.platform == "win32": |
| 377 | # an environment variable is limited to 32,767 characters |
| 378 | key = 'x' * 50000 |
Victor Stinner | 091b6ef | 2011-11-22 22:30:19 +0100 | [diff] [blame] | 379 | self.assertRaises(ValueError, os.environ.__delitem__, key) |
Victor Stinner | 53853c3 | 2011-11-22 22:20:13 +0100 | [diff] [blame] | 380 | else: |
| 381 | # "=" is not allowed in a variable name |
| 382 | key = 'key=' |
Victor Stinner | 091b6ef | 2011-11-22 22:30:19 +0100 | [diff] [blame] | 383 | self.assertRaises(OSError, os.environ.__delitem__, key) |
Victor Stinner | 53853c3 | 2011-11-22 22:20:13 +0100 | [diff] [blame] | 384 | |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 385 | class WalkTests(unittest.TestCase): |
| 386 | """Tests for os.walk().""" |
| 387 | |
| 388 | def test_traversal(self): |
| 389 | import os |
| 390 | from os.path import join |
| 391 | |
| 392 | # Build: |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 393 | # TESTFN/ |
Georg Brandl | cae9f3d | 2007-03-21 09:10:29 +0000 | [diff] [blame] | 394 | # TEST1/ a file kid and two directory kids |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 395 | # tmp1 |
| 396 | # SUB1/ a file kid and a directory kid |
Georg Brandl | cae9f3d | 2007-03-21 09:10:29 +0000 | [diff] [blame] | 397 | # tmp2 |
| 398 | # SUB11/ no kids |
| 399 | # SUB2/ a file kid and a dirsymlink kid |
| 400 | # tmp3 |
| 401 | # link/ a symlink to TESTFN.2 |
| 402 | # TEST2/ |
| 403 | # tmp4 a lone file |
| 404 | walk_path = join(test_support.TESTFN, "TEST1") |
| 405 | sub1_path = join(walk_path, "SUB1") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 406 | sub11_path = join(sub1_path, "SUB11") |
Georg Brandl | cae9f3d | 2007-03-21 09:10:29 +0000 | [diff] [blame] | 407 | sub2_path = join(walk_path, "SUB2") |
| 408 | tmp1_path = join(walk_path, "tmp1") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 409 | tmp2_path = join(sub1_path, "tmp2") |
| 410 | tmp3_path = join(sub2_path, "tmp3") |
Georg Brandl | cae9f3d | 2007-03-21 09:10:29 +0000 | [diff] [blame] | 411 | link_path = join(sub2_path, "link") |
| 412 | t2_path = join(test_support.TESTFN, "TEST2") |
| 413 | tmp4_path = join(test_support.TESTFN, "TEST2", "tmp4") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 414 | |
| 415 | # Create stuff. |
| 416 | os.makedirs(sub11_path) |
| 417 | os.makedirs(sub2_path) |
Georg Brandl | cae9f3d | 2007-03-21 09:10:29 +0000 | [diff] [blame] | 418 | os.makedirs(t2_path) |
| 419 | for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path: |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 420 | f = file(path, "w") |
| 421 | f.write("I'm " + path + " and proud of it. Blame test_os.\n") |
| 422 | f.close() |
Georg Brandl | cae9f3d | 2007-03-21 09:10:29 +0000 | [diff] [blame] | 423 | if hasattr(os, "symlink"): |
| 424 | os.symlink(os.path.abspath(t2_path), link_path) |
Žiga Seilnacht | 18ffe42 | 2007-04-04 18:38:47 +0000 | [diff] [blame] | 425 | sub2_tree = (sub2_path, ["link"], ["tmp3"]) |
Georg Brandl | cae9f3d | 2007-03-21 09:10:29 +0000 | [diff] [blame] | 426 | else: |
Žiga Seilnacht | 18ffe42 | 2007-04-04 18:38:47 +0000 | [diff] [blame] | 427 | sub2_tree = (sub2_path, [], ["tmp3"]) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 428 | |
| 429 | # Walk top-down. |
Georg Brandl | cae9f3d | 2007-03-21 09:10:29 +0000 | [diff] [blame] | 430 | all = list(os.walk(walk_path)) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 431 | self.assertEqual(len(all), 4) |
| 432 | # We can't know which order SUB1 and SUB2 will appear in. |
| 433 | # Not flipped: TESTFN, SUB1, SUB11, SUB2 |
| 434 | # flipped: TESTFN, SUB2, SUB1, SUB11 |
| 435 | flipped = all[0][1][0] != "SUB1" |
| 436 | all[0][1].sort() |
Georg Brandl | cae9f3d | 2007-03-21 09:10:29 +0000 | [diff] [blame] | 437 | self.assertEqual(all[0], (walk_path, ["SUB1", "SUB2"], ["tmp1"])) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 438 | self.assertEqual(all[1 + flipped], (sub1_path, ["SUB11"], ["tmp2"])) |
| 439 | self.assertEqual(all[2 + flipped], (sub11_path, [], [])) |
Žiga Seilnacht | 18ffe42 | 2007-04-04 18:38:47 +0000 | [diff] [blame] | 440 | self.assertEqual(all[3 - 2 * flipped], sub2_tree) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 441 | |
| 442 | # Prune the search. |
| 443 | all = [] |
Georg Brandl | cae9f3d | 2007-03-21 09:10:29 +0000 | [diff] [blame] | 444 | for root, dirs, files in os.walk(walk_path): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 445 | all.append((root, dirs, files)) |
| 446 | # Don't descend into SUB1. |
| 447 | if 'SUB1' in dirs: |
| 448 | # Note that this also mutates the dirs we appended to all! |
| 449 | dirs.remove('SUB1') |
| 450 | self.assertEqual(len(all), 2) |
Georg Brandl | cae9f3d | 2007-03-21 09:10:29 +0000 | [diff] [blame] | 451 | self.assertEqual(all[0], (walk_path, ["SUB2"], ["tmp1"])) |
Žiga Seilnacht | 18ffe42 | 2007-04-04 18:38:47 +0000 | [diff] [blame] | 452 | self.assertEqual(all[1], sub2_tree) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 453 | |
| 454 | # Walk bottom-up. |
Georg Brandl | cae9f3d | 2007-03-21 09:10:29 +0000 | [diff] [blame] | 455 | all = list(os.walk(walk_path, topdown=False)) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 456 | self.assertEqual(len(all), 4) |
| 457 | # We can't know which order SUB1 and SUB2 will appear in. |
| 458 | # Not flipped: SUB11, SUB1, SUB2, TESTFN |
| 459 | # flipped: SUB2, SUB11, SUB1, TESTFN |
| 460 | flipped = all[3][1][0] != "SUB1" |
| 461 | all[3][1].sort() |
Georg Brandl | cae9f3d | 2007-03-21 09:10:29 +0000 | [diff] [blame] | 462 | self.assertEqual(all[3], (walk_path, ["SUB1", "SUB2"], ["tmp1"])) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 463 | self.assertEqual(all[flipped], (sub11_path, [], [])) |
| 464 | self.assertEqual(all[flipped + 1], (sub1_path, ["SUB11"], ["tmp2"])) |
Žiga Seilnacht | 18ffe42 | 2007-04-04 18:38:47 +0000 | [diff] [blame] | 465 | self.assertEqual(all[2 - 2 * flipped], sub2_tree) |
Georg Brandl | cae9f3d | 2007-03-21 09:10:29 +0000 | [diff] [blame] | 466 | |
Žiga Seilnacht | 18ffe42 | 2007-04-04 18:38:47 +0000 | [diff] [blame] | 467 | if hasattr(os, "symlink"): |
| 468 | # Walk, following symlinks. |
| 469 | for root, dirs, files in os.walk(walk_path, followlinks=True): |
| 470 | if root == link_path: |
| 471 | self.assertEqual(dirs, []) |
| 472 | self.assertEqual(files, ["tmp4"]) |
| 473 | break |
| 474 | else: |
| 475 | self.fail("Didn't follow symlink with followlinks=True") |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 476 | |
Žiga Seilnacht | 18ffe42 | 2007-04-04 18:38:47 +0000 | [diff] [blame] | 477 | def tearDown(self): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 478 | # Tear everything down. This is a decent use for bottom-up on |
| 479 | # Windows, which doesn't have a recursive delete command. The |
| 480 | # (not so) subtlety is that rmdir will fail unless the dir's |
| 481 | # kids are removed first, so bottom up is essential. |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 482 | for root, dirs, files in os.walk(test_support.TESTFN, topdown=False): |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 483 | for name in files: |
Žiga Seilnacht | 18ffe42 | 2007-04-04 18:38:47 +0000 | [diff] [blame] | 484 | os.remove(os.path.join(root, name)) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 485 | for name in dirs: |
Žiga Seilnacht | 18ffe42 | 2007-04-04 18:38:47 +0000 | [diff] [blame] | 486 | dirname = os.path.join(root, name) |
Georg Brandl | cae9f3d | 2007-03-21 09:10:29 +0000 | [diff] [blame] | 487 | if not os.path.islink(dirname): |
| 488 | os.rmdir(dirname) |
| 489 | else: |
| 490 | os.remove(dirname) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 491 | os.rmdir(test_support.TESTFN) |
Tim Peters | c4e0940 | 2003-04-25 07:11:48 +0000 | [diff] [blame] | 492 | |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 493 | class MakedirTests (unittest.TestCase): |
| 494 | def setUp(self): |
| 495 | os.mkdir(test_support.TESTFN) |
| 496 | |
| 497 | def test_makedir(self): |
| 498 | base = test_support.TESTFN |
| 499 | path = os.path.join(base, 'dir1', 'dir2', 'dir3') |
| 500 | os.makedirs(path) # Should work |
| 501 | path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4') |
| 502 | os.makedirs(path) |
| 503 | |
| 504 | # Try paths with a '.' in them |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 505 | self.assertRaises(OSError, os.makedirs, os.curdir) |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 506 | path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4', 'dir5', os.curdir) |
| 507 | os.makedirs(path) |
| 508 | path = os.path.join(base, 'dir1', os.curdir, 'dir2', 'dir3', 'dir4', |
| 509 | 'dir5', 'dir6') |
| 510 | os.makedirs(path) |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 511 | |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 512 | |
| 513 | |
| 514 | |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 515 | def tearDown(self): |
| 516 | path = os.path.join(test_support.TESTFN, 'dir1', 'dir2', 'dir3', |
| 517 | 'dir4', 'dir5', 'dir6') |
| 518 | # If the tests failed, the bottom-most directory ('../dir6') |
| 519 | # may not have been created, so we look for the outermost directory |
| 520 | # that exists. |
| 521 | while not os.path.exists(path) and path != test_support.TESTFN: |
| 522 | path = os.path.dirname(path) |
| 523 | |
| 524 | os.removedirs(path) |
| 525 | |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 526 | class DevNullTests (unittest.TestCase): |
| 527 | def test_devnull(self): |
| 528 | f = file(os.devnull, 'w') |
| 529 | f.write('hello') |
| 530 | f.close() |
| 531 | f = file(os.devnull, 'r') |
Tim Peters | 4182cfd | 2004-06-08 20:34:34 +0000 | [diff] [blame] | 532 | self.assertEqual(f.read(), '') |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 533 | f.close() |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 534 | |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 535 | class URandomTests (unittest.TestCase): |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 536 | |
| 537 | def test_urandom_length(self): |
| 538 | self.assertEqual(len(os.urandom(0)), 0) |
| 539 | self.assertEqual(len(os.urandom(1)), 1) |
| 540 | self.assertEqual(len(os.urandom(10)), 10) |
| 541 | self.assertEqual(len(os.urandom(100)), 100) |
| 542 | self.assertEqual(len(os.urandom(1000)), 1000) |
| 543 | |
| 544 | def test_urandom_value(self): |
| 545 | data1 = os.urandom(16) |
| 546 | data2 = os.urandom(16) |
| 547 | self.assertNotEqual(data1, data2) |
| 548 | |
| 549 | def get_urandom_subprocess(self, count): |
Antoine Pitrou | 341016e | 2012-02-22 22:16:25 +0100 | [diff] [blame] | 550 | # We need to use repr() and eval() to avoid line ending conversions |
| 551 | # under Windows. |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 552 | code = '\n'.join(( |
| 553 | 'import os, sys', |
| 554 | 'data = os.urandom(%s)' % count, |
Antoine Pitrou | 341016e | 2012-02-22 22:16:25 +0100 | [diff] [blame] | 555 | 'sys.stdout.write(repr(data))', |
Antoine Pitrou | 0607f73 | 2012-02-21 22:02:04 +0100 | [diff] [blame] | 556 | 'sys.stdout.flush()', |
| 557 | 'print >> sys.stderr, (len(data), data)')) |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 558 | cmd_line = [sys.executable, '-c', code] |
| 559 | p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE, |
Antoine Pitrou | 0607f73 | 2012-02-21 22:02:04 +0100 | [diff] [blame] | 560 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 561 | out, err = p.communicate() |
Antoine Pitrou | 0607f73 | 2012-02-21 22:02:04 +0100 | [diff] [blame] | 562 | self.assertEqual(p.wait(), 0, (p.wait(), err)) |
Antoine Pitrou | 341016e | 2012-02-22 22:16:25 +0100 | [diff] [blame] | 563 | out = eval(out) |
| 564 | self.assertEqual(len(out), count, err) |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 565 | return out |
| 566 | |
| 567 | def test_urandom_subprocess(self): |
| 568 | data1 = self.get_urandom_subprocess(16) |
| 569 | data2 = self.get_urandom_subprocess(16) |
| 570 | self.assertNotEqual(data1, data2) |
Martin v. Löwis | dc3883f | 2004-08-29 15:46:35 +0000 | [diff] [blame] | 571 | |
Victor Stinner | ebcbbfb | 2015-02-24 15:12:57 +0100 | [diff] [blame] | 572 | # os.urandom() doesn't use a file descriptor on Windows |
| 573 | @unittest.skipIf(sys.platform == "win32", "POSIX specific tests") |
Ned Deily | 9e52735 | 2015-03-17 15:18:07 -0700 | [diff] [blame] | 574 | # FD_CLOEXEC is first supported on OS X 10.5 |
| 575 | @test_support.requires_mac_ver(10, 5) |
Victor Stinner | e0a0bd6 | 2015-02-24 14:30:43 +0100 | [diff] [blame] | 576 | def test_urandom_fd_non_inheritable(self): |
| 577 | # Issue #23458: os.urandom() keeps a file descriptor open, but it |
| 578 | # must be non inheritable |
| 579 | fd_status = test_support.findfile("fd_status.py", subdir="subprocessdata") |
| 580 | |
| 581 | # Need a two subprocesses because the Python test suite opens other |
| 582 | # inheritable file descriptors, whereas the test is specific to |
| 583 | # os.urandom() file descriptor |
| 584 | code = textwrap.dedent(""" |
| 585 | import os |
| 586 | import subprocess |
| 587 | import sys |
| 588 | |
| 589 | # Ensure that the /dev/urandom file descriptor is open |
| 590 | os.urandom(1) |
| 591 | |
| 592 | exitcode = subprocess.call([sys.executable, %r], |
| 593 | close_fds=False) |
| 594 | sys.exit(exitcode) |
| 595 | """ % fd_status) |
| 596 | |
| 597 | proc = subprocess.Popen([sys.executable, "-c", code], |
| 598 | stdout=subprocess.PIPE, close_fds=True) |
| 599 | output, error = proc.communicate() |
| 600 | open_fds = set(map(int, output.rstrip().split(','))) |
| 601 | self.assertEqual(open_fds - set(range(3)), set()) |
| 602 | |
Benjamin Peterson | 27c269a | 2014-12-26 11:09:00 -0600 | [diff] [blame] | 603 | |
| 604 | HAVE_GETENTROPY = (sysconfig.get_config_var('HAVE_GETENTROPY') == 1) |
| 605 | |
| 606 | @unittest.skipIf(HAVE_GETENTROPY, |
| 607 | "getentropy() does not use a file descriptor") |
| 608 | class URandomFDTests(unittest.TestCase): |
Antoine Pitrou | f48a67b | 2013-08-16 20:44:38 +0200 | [diff] [blame] | 609 | @unittest.skipUnless(resource, "test requires the resource module") |
| 610 | def test_urandom_failure(self): |
Antoine Pitrou | e758715 | 2013-08-24 20:52:27 +0200 | [diff] [blame] | 611 | # Check urandom() failing when it is not able to open /dev/random. |
| 612 | # We spawn a new process to make the test more robust (if getrlimit() |
| 613 | # failed to restore the file descriptor limit after this, the whole |
| 614 | # test suite would crash; this actually happened on the OS X Tiger |
| 615 | # buildbot). |
| 616 | code = """if 1: |
| 617 | import errno |
| 618 | import os |
| 619 | import resource |
| 620 | |
| 621 | soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE) |
| 622 | resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit)) |
| 623 | try: |
Antoine Pitrou | f48a67b | 2013-08-16 20:44:38 +0200 | [diff] [blame] | 624 | os.urandom(16) |
Antoine Pitrou | e758715 | 2013-08-24 20:52:27 +0200 | [diff] [blame] | 625 | except OSError as e: |
| 626 | assert e.errno == errno.EMFILE, e.errno |
| 627 | else: |
| 628 | raise AssertionError("OSError not raised") |
| 629 | """ |
| 630 | assert_python_ok('-c', code) |
Antoine Pitrou | f48a67b | 2013-08-16 20:44:38 +0200 | [diff] [blame] | 631 | |
Antoine Pitrou | 326ec04 | 2013-08-16 20:56:12 +0200 | [diff] [blame] | 632 | |
| 633 | class ExecvpeTests(unittest.TestCase): |
| 634 | |
Matthias Klose | e9fbf2b | 2010-03-19 14:45:06 +0000 | [diff] [blame] | 635 | def test_execvpe_with_bad_arglist(self): |
| 636 | self.assertRaises(ValueError, os.execvpe, 'notepad', [], None) |
| 637 | |
Antoine Pitrou | 326ec04 | 2013-08-16 20:56:12 +0200 | [diff] [blame] | 638 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 639 | @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests") |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 640 | class Win32ErrorTests(unittest.TestCase): |
| 641 | def test_rename(self): |
| 642 | self.assertRaises(WindowsError, os.rename, test_support.TESTFN, test_support.TESTFN+".bak") |
| 643 | |
| 644 | def test_remove(self): |
| 645 | self.assertRaises(WindowsError, os.remove, test_support.TESTFN) |
| 646 | |
| 647 | def test_chdir(self): |
| 648 | self.assertRaises(WindowsError, os.chdir, test_support.TESTFN) |
| 649 | |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 650 | def test_mkdir(self): |
Kristján Valur Jónsson | e20f54f | 2009-02-06 10:17:34 +0000 | [diff] [blame] | 651 | f = open(test_support.TESTFN, "w") |
| 652 | try: |
| 653 | self.assertRaises(WindowsError, os.mkdir, test_support.TESTFN) |
| 654 | finally: |
| 655 | f.close() |
| 656 | os.unlink(test_support.TESTFN) |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 657 | |
| 658 | def test_utime(self): |
| 659 | self.assertRaises(WindowsError, os.utime, test_support.TESTFN, None) |
| 660 | |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 661 | def test_chmod(self): |
Kristján Valur Jónsson | e20f54f | 2009-02-06 10:17:34 +0000 | [diff] [blame] | 662 | self.assertRaises(WindowsError, os.chmod, test_support.TESTFN, 0) |
Martin v. Löwis | d4e3bb3 | 2006-05-06 16:32:54 +0000 | [diff] [blame] | 663 | |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 664 | class TestInvalidFD(unittest.TestCase): |
Kristján Valur Jónsson | 71ba215 | 2009-01-15 22:40:03 +0000 | [diff] [blame] | 665 | singles = ["fchdir", "fdopen", "dup", "fdatasync", "fstat", |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 666 | "fstatvfs", "fsync", "tcgetpgrp", "ttyname"] |
Kristján Valur Jónsson | 71ba215 | 2009-01-15 22:40:03 +0000 | [diff] [blame] | 667 | #singles.append("close") |
| 668 | #We omit close because it doesn'r raise an exception on some platforms |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 669 | def get_single(f): |
| 670 | def helper(self): |
Benjamin Peterson | 5539c78 | 2009-01-19 17:37:42 +0000 | [diff] [blame] | 671 | if hasattr(os, f): |
| 672 | self.check(getattr(os, f)) |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 673 | return helper |
| 674 | for f in singles: |
| 675 | locals()["test_"+f] = get_single(f) |
| 676 | |
Benjamin Peterson | 5539c78 | 2009-01-19 17:37:42 +0000 | [diff] [blame] | 677 | def check(self, f, *args): |
Benjamin Peterson | 1de05e9 | 2009-01-31 01:42:55 +0000 | [diff] [blame] | 678 | try: |
| 679 | f(test_support.make_bad_fd(), *args) |
| 680 | except OSError as e: |
| 681 | self.assertEqual(e.errno, errno.EBADF) |
| 682 | else: |
| 683 | self.fail("%r didn't raise a OSError with a bad file descriptor" |
| 684 | % f) |
Benjamin Peterson | 5539c78 | 2009-01-19 17:37:42 +0000 | [diff] [blame] | 685 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 686 | @unittest.skipUnless(hasattr(os, 'isatty'), 'test needs os.isatty()') |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 687 | def test_isatty(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 688 | self.assertEqual(os.isatty(test_support.make_bad_fd()), False) |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 689 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 690 | @unittest.skipUnless(hasattr(os, 'closerange'), 'test needs os.closerange()') |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 691 | def test_closerange(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 692 | fd = test_support.make_bad_fd() |
| 693 | # Make sure none of the descriptors we are about to close are |
| 694 | # currently valid (issue 6542). |
| 695 | for i in range(10): |
| 696 | try: os.fstat(fd+i) |
| 697 | except OSError: |
| 698 | pass |
| 699 | else: |
| 700 | break |
| 701 | if i < 2: |
| 702 | raise unittest.SkipTest( |
| 703 | "Unable to acquire a range of invalid file descriptors") |
| 704 | self.assertEqual(os.closerange(fd, fd + i-1), None) |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 705 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 706 | @unittest.skipUnless(hasattr(os, 'dup2'), 'test needs os.dup2()') |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 707 | def test_dup2(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 708 | self.check(os.dup2, 20) |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 709 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 710 | @unittest.skipUnless(hasattr(os, 'fchmod'), 'test needs os.fchmod()') |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 711 | def test_fchmod(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 712 | self.check(os.fchmod, 0) |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 713 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 714 | @unittest.skipUnless(hasattr(os, 'fchown'), 'test needs os.fchown()') |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 715 | def test_fchown(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 716 | self.check(os.fchown, -1, -1) |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 717 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 718 | @unittest.skipUnless(hasattr(os, 'fpathconf'), 'test needs os.fpathconf()') |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 719 | def test_fpathconf(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 720 | self.check(os.fpathconf, "PC_NAME_MAX") |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 721 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 722 | @unittest.skipUnless(hasattr(os, 'ftruncate'), 'test needs os.ftruncate()') |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 723 | def test_ftruncate(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 724 | self.check(os.ftruncate, 0) |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 725 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 726 | @unittest.skipUnless(hasattr(os, 'lseek'), 'test needs os.lseek()') |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 727 | def test_lseek(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 728 | self.check(os.lseek, 0, 0) |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 729 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 730 | @unittest.skipUnless(hasattr(os, 'read'), 'test needs os.read()') |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 731 | def test_read(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 732 | self.check(os.read, 1) |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 733 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 734 | @unittest.skipUnless(hasattr(os, 'tcsetpgrp'), 'test needs os.tcsetpgrp()') |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 735 | def test_tcsetpgrpt(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 736 | self.check(os.tcsetpgrp, 0) |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 737 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 738 | @unittest.skipUnless(hasattr(os, 'write'), 'test needs os.write()') |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 739 | def test_write(self): |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 740 | self.check(os.write, " ") |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 741 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 742 | @unittest.skipIf(sys.platform == "win32", "Posix specific tests") |
| 743 | class PosixUidGidTests(unittest.TestCase): |
| 744 | @unittest.skipUnless(hasattr(os, 'setuid'), 'test needs os.setuid()') |
| 745 | def test_setuid(self): |
| 746 | if os.getuid() != 0: |
| 747 | self.assertRaises(os.error, os.setuid, 0) |
| 748 | self.assertRaises(OverflowError, os.setuid, 1<<32) |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 749 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 750 | @unittest.skipUnless(hasattr(os, 'setgid'), 'test needs os.setgid()') |
| 751 | def test_setgid(self): |
| 752 | if os.getuid() != 0: |
| 753 | self.assertRaises(os.error, os.setgid, 0) |
| 754 | self.assertRaises(OverflowError, os.setgid, 1<<32) |
Gregory P. Smith | 6d30793 | 2009-04-05 23:43:58 +0000 | [diff] [blame] | 755 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 756 | @unittest.skipUnless(hasattr(os, 'seteuid'), 'test needs os.seteuid()') |
| 757 | def test_seteuid(self): |
| 758 | if os.getuid() != 0: |
| 759 | self.assertRaises(os.error, os.seteuid, 0) |
| 760 | self.assertRaises(OverflowError, os.seteuid, 1<<32) |
Gregory P. Smith | 6d30793 | 2009-04-05 23:43:58 +0000 | [diff] [blame] | 761 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 762 | @unittest.skipUnless(hasattr(os, 'setegid'), 'test needs os.setegid()') |
| 763 | def test_setegid(self): |
| 764 | if os.getuid() != 0: |
| 765 | self.assertRaises(os.error, os.setegid, 0) |
| 766 | self.assertRaises(OverflowError, os.setegid, 1<<32) |
Gregory P. Smith | 6d30793 | 2009-04-05 23:43:58 +0000 | [diff] [blame] | 767 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 768 | @unittest.skipUnless(hasattr(os, 'setreuid'), 'test needs os.setreuid()') |
| 769 | def test_setreuid(self): |
| 770 | if os.getuid() != 0: |
| 771 | self.assertRaises(os.error, os.setreuid, 0, 0) |
| 772 | self.assertRaises(OverflowError, os.setreuid, 1<<32, 0) |
| 773 | self.assertRaises(OverflowError, os.setreuid, 0, 1<<32) |
Gregory P. Smith | 6d30793 | 2009-04-05 23:43:58 +0000 | [diff] [blame] | 774 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 775 | @unittest.skipUnless(hasattr(os, 'setreuid'), 'test needs os.setreuid()') |
| 776 | def test_setreuid_neg1(self): |
| 777 | # Needs to accept -1. We run this in a subprocess to avoid |
| 778 | # altering the test runner's process state (issue8045). |
| 779 | subprocess.check_call([ |
| 780 | sys.executable, '-c', |
| 781 | 'import os,sys;os.setreuid(-1,-1);sys.exit(0)']) |
Gregory P. Smith | 467298c | 2010-03-06 07:35:19 +0000 | [diff] [blame] | 782 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 783 | @unittest.skipUnless(hasattr(os, 'setregid'), 'test needs os.setregid()') |
| 784 | def test_setregid(self): |
| 785 | if os.getuid() != 0: |
| 786 | self.assertRaises(os.error, os.setregid, 0, 0) |
| 787 | self.assertRaises(OverflowError, os.setregid, 1<<32, 0) |
| 788 | self.assertRaises(OverflowError, os.setregid, 0, 1<<32) |
Gregory P. Smith | 6d30793 | 2009-04-05 23:43:58 +0000 | [diff] [blame] | 789 | |
Serhiy Storchaka | 32e23e7 | 2013-11-03 23:15:46 +0200 | [diff] [blame] | 790 | @unittest.skipUnless(hasattr(os, 'setregid'), 'test needs os.setregid()') |
| 791 | def test_setregid_neg1(self): |
| 792 | # Needs to accept -1. We run this in a subprocess to avoid |
| 793 | # altering the test runner's process state (issue8045). |
| 794 | subprocess.check_call([ |
| 795 | sys.executable, '-c', |
| 796 | 'import os,sys;os.setregid(-1,-1);sys.exit(0)']) |
Gregory P. Smith | 467298c | 2010-03-06 07:35:19 +0000 | [diff] [blame] | 797 | |
Gregory P. Smith | 6d30793 | 2009-04-05 23:43:58 +0000 | [diff] [blame] | 798 | |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 799 | @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests") |
| 800 | class Win32KillTests(unittest.TestCase): |
Brian Curtin | b3dde13 | 2010-04-15 00:40:40 +0000 | [diff] [blame] | 801 | def _kill(self, sig): |
| 802 | # Start sys.executable as a subprocess and communicate from the |
| 803 | # subprocess to the parent that the interpreter is ready. When it |
| 804 | # becomes ready, send *sig* via os.kill to the subprocess and check |
| 805 | # that the return code is equal to *sig*. |
| 806 | import ctypes |
| 807 | from ctypes import wintypes |
| 808 | import msvcrt |
| 809 | |
| 810 | # Since we can't access the contents of the process' stdout until the |
| 811 | # process has exited, use PeekNamedPipe to see what's inside stdout |
| 812 | # without waiting. This is done so we can tell that the interpreter |
| 813 | # is started and running at a point where it could handle a signal. |
| 814 | PeekNamedPipe = ctypes.windll.kernel32.PeekNamedPipe |
| 815 | PeekNamedPipe.restype = wintypes.BOOL |
| 816 | PeekNamedPipe.argtypes = (wintypes.HANDLE, # Pipe handle |
| 817 | ctypes.POINTER(ctypes.c_char), # stdout buf |
| 818 | wintypes.DWORD, # Buffer size |
| 819 | ctypes.POINTER(wintypes.DWORD), # bytes read |
| 820 | ctypes.POINTER(wintypes.DWORD), # bytes avail |
| 821 | ctypes.POINTER(wintypes.DWORD)) # bytes left |
| 822 | msg = "running" |
| 823 | proc = subprocess.Popen([sys.executable, "-c", |
| 824 | "import sys;" |
| 825 | "sys.stdout.write('{}');" |
| 826 | "sys.stdout.flush();" |
| 827 | "input()".format(msg)], |
| 828 | stdout=subprocess.PIPE, |
| 829 | stderr=subprocess.PIPE, |
| 830 | stdin=subprocess.PIPE) |
Brian Curtin | f4f0c8b | 2010-11-05 15:31:20 +0000 | [diff] [blame] | 831 | self.addCleanup(proc.stdout.close) |
| 832 | self.addCleanup(proc.stderr.close) |
| 833 | self.addCleanup(proc.stdin.close) |
Brian Curtin | b3dde13 | 2010-04-15 00:40:40 +0000 | [diff] [blame] | 834 | |
Brian Curtin | 83cba05 | 2010-05-28 15:49:21 +0000 | [diff] [blame] | 835 | count, max = 0, 100 |
| 836 | while count < max and proc.poll() is None: |
| 837 | # Create a string buffer to store the result of stdout from the pipe |
| 838 | buf = ctypes.create_string_buffer(len(msg)) |
| 839 | # Obtain the text currently in proc.stdout |
| 840 | # Bytes read/avail/left are left as NULL and unused |
| 841 | rslt = PeekNamedPipe(msvcrt.get_osfhandle(proc.stdout.fileno()), |
| 842 | buf, ctypes.sizeof(buf), None, None, None) |
| 843 | self.assertNotEqual(rslt, 0, "PeekNamedPipe failed") |
| 844 | if buf.value: |
| 845 | self.assertEqual(msg, buf.value) |
| 846 | break |
| 847 | time.sleep(0.1) |
| 848 | count += 1 |
| 849 | else: |
| 850 | self.fail("Did not receive communication from the subprocess") |
Brian Curtin | b3dde13 | 2010-04-15 00:40:40 +0000 | [diff] [blame] | 851 | |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 852 | os.kill(proc.pid, sig) |
| 853 | self.assertEqual(proc.wait(), sig) |
| 854 | |
| 855 | def test_kill_sigterm(self): |
| 856 | # SIGTERM doesn't mean anything special, but make sure it works |
Brian Curtin | b3dde13 | 2010-04-15 00:40:40 +0000 | [diff] [blame] | 857 | self._kill(signal.SIGTERM) |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 858 | |
| 859 | def test_kill_int(self): |
| 860 | # os.kill on Windows can take an int which gets set as the exit code |
Brian Curtin | b3dde13 | 2010-04-15 00:40:40 +0000 | [diff] [blame] | 861 | self._kill(100) |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 862 | |
| 863 | def _kill_with_event(self, event, name): |
Hirokazu Yamamoto | 1f504f1 | 2010-10-08 09:41:13 +0000 | [diff] [blame] | 864 | tagname = "test_os_%s" % uuid.uuid1() |
| 865 | m = mmap.mmap(-1, 1, tagname) |
| 866 | m[0] = '0' |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 867 | # Run a script which has console control handling enabled. |
| 868 | proc = subprocess.Popen([sys.executable, |
| 869 | os.path.join(os.path.dirname(__file__), |
Hirokazu Yamamoto | 1f504f1 | 2010-10-08 09:41:13 +0000 | [diff] [blame] | 870 | "win_console_handler.py"), tagname], |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 871 | creationflags=subprocess.CREATE_NEW_PROCESS_GROUP) |
| 872 | # Let the interpreter startup before we send signals. See #3137. |
Hirokazu Yamamoto | 1f504f1 | 2010-10-08 09:41:13 +0000 | [diff] [blame] | 873 | count, max = 0, 20 |
| 874 | while count < max and proc.poll() is None: |
Brian Curtin | dbf8e83 | 2010-11-05 15:28:19 +0000 | [diff] [blame] | 875 | if m[0] == '1': |
Hirokazu Yamamoto | 1f504f1 | 2010-10-08 09:41:13 +0000 | [diff] [blame] | 876 | break |
| 877 | time.sleep(0.5) |
| 878 | count += 1 |
| 879 | else: |
| 880 | self.fail("Subprocess didn't finish initialization") |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 881 | os.kill(proc.pid, event) |
| 882 | # proc.send_signal(event) could also be done here. |
| 883 | # Allow time for the signal to be passed and the process to exit. |
Brian Curtin | fce1d31 | 2010-04-05 19:04:23 +0000 | [diff] [blame] | 884 | time.sleep(0.5) |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 885 | if not proc.poll(): |
| 886 | # Forcefully kill the process if we weren't able to signal it. |
| 887 | os.kill(proc.pid, signal.SIGINT) |
| 888 | self.fail("subprocess did not stop on {}".format(name)) |
| 889 | |
| 890 | @unittest.skip("subprocesses aren't inheriting CTRL+C property") |
| 891 | def test_CTRL_C_EVENT(self): |
| 892 | from ctypes import wintypes |
| 893 | import ctypes |
| 894 | |
| 895 | # Make a NULL value by creating a pointer with no argument. |
| 896 | NULL = ctypes.POINTER(ctypes.c_int)() |
| 897 | SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler |
| 898 | SetConsoleCtrlHandler.argtypes = (ctypes.POINTER(ctypes.c_int), |
| 899 | wintypes.BOOL) |
| 900 | SetConsoleCtrlHandler.restype = wintypes.BOOL |
| 901 | |
| 902 | # Calling this with NULL and FALSE causes the calling process to |
| 903 | # handle CTRL+C, rather than ignore it. This property is inherited |
| 904 | # by subprocesses. |
| 905 | SetConsoleCtrlHandler(NULL, 0) |
| 906 | |
| 907 | self._kill_with_event(signal.CTRL_C_EVENT, "CTRL_C_EVENT") |
| 908 | |
| 909 | def test_CTRL_BREAK_EVENT(self): |
| 910 | self._kill_with_event(signal.CTRL_BREAK_EVENT, "CTRL_BREAK_EVENT") |
| 911 | |
| 912 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 913 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 914 | test_support.run_unittest( |
Martin v. Löwis | ee1e06d | 2006-07-02 18:44:00 +0000 | [diff] [blame] | 915 | FileTests, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 916 | TemporaryFileTests, |
| 917 | StatAttributeTests, |
| 918 | EnvironTests, |
Andrew M. Kuchling | b386f6a | 2003-12-23 16:36:11 +0000 | [diff] [blame] | 919 | WalkTests, |
| 920 | MakedirTests, |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 921 | DevNullTests, |
Martin v. Löwis | 8e0d494 | 2006-05-04 10:08:42 +0000 | [diff] [blame] | 922 | URandomTests, |
Ned Deily | 596b751 | 2015-03-17 04:34:46 -0700 | [diff] [blame] | 923 | URandomFDTests, |
Antoine Pitrou | 326ec04 | 2013-08-16 20:56:12 +0200 | [diff] [blame] | 924 | ExecvpeTests, |
Kristján Valur Jónsson | 1c62b65 | 2009-01-12 18:09:27 +0000 | [diff] [blame] | 925 | Win32ErrorTests, |
Gregory P. Smith | 6d30793 | 2009-04-05 23:43:58 +0000 | [diff] [blame] | 926 | TestInvalidFD, |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 927 | PosixUidGidTests, |
| 928 | Win32KillTests |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 929 | ) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 930 | |
| 931 | if __name__ == "__main__": |
| 932 | test_main() |