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