blob: 1d673f65202d6321264b1fada3b45a7a1fb25798 [file] [log] [blame]
Fred Drake38c2ef02001-07-17 20:52:51 +00001# 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örwaldf0dfc7a2003-10-20 14:01:56 +00003# portable than they had been thought to be.
Fred Drake38c2ef02001-07-17 20:52:51 +00004
5import os
Benjamin Peterson1de05e92009-01-31 01:42:55 +00006import errno
Fred Drake38c2ef02001-07-17 20:52:51 +00007import unittest
Jeremy Hyltona7fc21b2001-08-20 20:10:01 +00008import warnings
Martin v. Löwis8e0d4942006-05-04 10:08:42 +00009import sys
Brian Curtine5aa8862010-04-02 23:26:06 +000010import signal
11import subprocess
12import time
Barry Warsaw1e13eb02012-02-20 20:42:21 -050013
Walter Dörwald21d3a322003-05-01 17:45:56 +000014from test import test_support
Hirokazu Yamamoto1f504f12010-10-08 09:41:13 +000015import mmap
16import uuid
Fred Drake38c2ef02001-07-17 20:52:51 +000017
Barry Warsaw60f01882001-08-22 19:24:42 +000018warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, __name__)
19warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, __name__)
20
Martin v. Löwisee1e06d2006-07-02 18:44:00 +000021# Tests creating TESTFN
22class FileTests(unittest.TestCase):
23 def setUp(self):
24 if os.path.exists(test_support.TESTFN):
25 os.unlink(test_support.TESTFN)
26 tearDown = setUp
27
28 def test_access(self):
29 f = os.open(test_support.TESTFN, os.O_CREAT|os.O_RDWR)
30 os.close(f)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000031 self.assertTrue(os.access(test_support.TESTFN, os.W_OK))
Tim Peters16a39322006-07-03 08:23:19 +000032
Georg Brandl309501a2008-01-19 20:22:13 +000033 def test_closerange(self):
Antoine Pitroubebb18b2008-08-17 14:43:41 +000034 first = os.open(test_support.TESTFN, os.O_CREAT|os.O_RDWR)
35 # We must allocate two consecutive file descriptors, otherwise
36 # it will mess up other file descriptors (perhaps even the three
37 # standard ones).
38 second = os.dup(first)
39 try:
40 retries = 0
41 while second != first + 1:
42 os.close(first)
43 retries += 1
44 if retries > 10:
45 # XXX test skipped
Benjamin Peterson757b3c92009-05-16 18:44:34 +000046 self.skipTest("couldn't allocate two consecutive fds")
Antoine Pitroubebb18b2008-08-17 14:43:41 +000047 first, second = second, os.dup(second)
48 finally:
49 os.close(second)
Georg Brandl309501a2008-01-19 20:22:13 +000050 # close a fd that is open, and one that isn't
Antoine Pitroubebb18b2008-08-17 14:43:41 +000051 os.closerange(first, first + 2)
52 self.assertRaises(OSError, os.write, first, "a")
Georg Brandl309501a2008-01-19 20:22:13 +000053
Benjamin Peterson10947a62010-06-30 17:11:08 +000054 @test_support.cpython_only
Hirokazu Yamamoto74ce88f2008-09-08 23:03:47 +000055 def test_rename(self):
56 path = unicode(test_support.TESTFN)
57 old = sys.getrefcount(path)
58 self.assertRaises(TypeError, os.rename, path, 0)
59 new = sys.getrefcount(path)
60 self.assertEqual(old, new)
61
Martin v. Löwisee1e06d2006-07-02 18:44:00 +000062
Fred Drake38c2ef02001-07-17 20:52:51 +000063class TemporaryFileTests(unittest.TestCase):
64 def setUp(self):
65 self.files = []
Walter Dörwald21d3a322003-05-01 17:45:56 +000066 os.mkdir(test_support.TESTFN)
Fred Drake38c2ef02001-07-17 20:52:51 +000067
68 def tearDown(self):
69 for name in self.files:
70 os.unlink(name)
Walter Dörwald21d3a322003-05-01 17:45:56 +000071 os.rmdir(test_support.TESTFN)
Fred Drake38c2ef02001-07-17 20:52:51 +000072
73 def check_tempfile(self, name):
74 # make sure it doesn't already exist:
Benjamin Peterson5c8da862009-06-30 22:57:08 +000075 self.assertFalse(os.path.exists(name),
Fred Drake38c2ef02001-07-17 20:52:51 +000076 "file already exists for temporary file")
77 # make sure we can create the file
78 open(name, "w")
79 self.files.append(name)
80
81 def test_tempnam(self):
82 if not hasattr(os, "tempnam"):
83 return
Antoine Pitroub0614612011-01-02 20:04:52 +000084 with warnings.catch_warnings():
85 warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
86 r"test_os$")
87 warnings.filterwarnings("ignore", "tempnam", DeprecationWarning)
88 self.check_tempfile(os.tempnam())
Fred Drake38c2ef02001-07-17 20:52:51 +000089
Antoine Pitroub0614612011-01-02 20:04:52 +000090 name = os.tempnam(test_support.TESTFN)
91 self.check_tempfile(name)
Fred Drake38c2ef02001-07-17 20:52:51 +000092
Antoine Pitroub0614612011-01-02 20:04:52 +000093 name = os.tempnam(test_support.TESTFN, "pfx")
94 self.assertTrue(os.path.basename(name)[:3] == "pfx")
95 self.check_tempfile(name)
Fred Drake38c2ef02001-07-17 20:52:51 +000096
97 def test_tmpfile(self):
98 if not hasattr(os, "tmpfile"):
99 return
Martin v. Löwisd2bbe522008-03-06 06:55:22 +0000100 # As with test_tmpnam() below, the Windows implementation of tmpfile()
101 # attempts to create a file in the root directory of the current drive.
102 # On Vista and Server 2008, this test will always fail for normal users
103 # as writing to the root directory requires elevated privileges. With
104 # XP and below, the semantics of tmpfile() are the same, but the user
105 # running the test is more likely to have administrative privileges on
106 # their account already. If that's the case, then os.tmpfile() should
107 # work. In order to make this test as useful as possible, rather than
108 # trying to detect Windows versions or whether or not the user has the
109 # right permissions, just try and create a file in the root directory
110 # and see if it raises a 'Permission denied' OSError. If it does, then
111 # test that a subsequent call to os.tmpfile() raises the same error. If
112 # it doesn't, assume we're on XP or below and the user running the test
113 # has administrative privileges, and proceed with the test as normal.
Antoine Pitroub0614612011-01-02 20:04:52 +0000114 with warnings.catch_warnings():
115 warnings.filterwarnings("ignore", "tmpfile", DeprecationWarning)
Martin v. Löwisd2bbe522008-03-06 06:55:22 +0000116
Antoine Pitroub0614612011-01-02 20:04:52 +0000117 if sys.platform == 'win32':
118 name = '\\python_test_os_test_tmpfile.txt'
119 if os.path.exists(name):
120 os.remove(name)
121 try:
122 fp = open(name, 'w')
123 except IOError, first:
124 # open() failed, assert tmpfile() fails in the same way.
125 # Although open() raises an IOError and os.tmpfile() raises an
126 # OSError(), 'args' will be (13, 'Permission denied') in both
127 # cases.
128 try:
129 fp = os.tmpfile()
130 except OSError, second:
131 self.assertEqual(first.args, second.args)
132 else:
133 self.fail("expected os.tmpfile() to raise OSError")
134 return
135 else:
136 # open() worked, therefore, tmpfile() should work. Close our
137 # dummy file and proceed with the test as normal.
138 fp.close()
139 os.remove(name)
140
141 fp = os.tmpfile()
142 fp.write("foobar")
143 fp.seek(0,0)
144 s = fp.read()
145 fp.close()
146 self.assertTrue(s == "foobar")
Fred Drake38c2ef02001-07-17 20:52:51 +0000147
148 def test_tmpnam(self):
149 if not hasattr(os, "tmpnam"):
150 return
Antoine Pitroub0614612011-01-02 20:04:52 +0000151 with warnings.catch_warnings():
152 warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning,
153 r"test_os$")
154 warnings.filterwarnings("ignore", "tmpnam", DeprecationWarning)
155
156 name = os.tmpnam()
157 if sys.platform in ("win32",):
158 # The Windows tmpnam() seems useless. From the MS docs:
159 #
160 # The character string that tmpnam creates consists of
161 # the path prefix, defined by the entry P_tmpdir in the
162 # file STDIO.H, followed by a sequence consisting of the
163 # digit characters '0' through '9'; the numerical value
164 # of this string is in the range 1 - 65,535. Changing the
165 # definitions of L_tmpnam or P_tmpdir in STDIO.H does not
166 # change the operation of tmpnam.
167 #
168 # The really bizarre part is that, at least under MSVC6,
169 # P_tmpdir is "\\". That is, the path returned refers to
170 # the root of the current drive. That's a terrible place to
171 # put temp files, and, depending on privileges, the user
172 # may not even be able to open a file in the root directory.
173 self.assertFalse(os.path.exists(name),
174 "file already exists for temporary file")
175 else:
176 self.check_tempfile(name)
Tim Peters87cc0c32001-07-21 01:41:30 +0000177
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000178# Test attributes on return values from os.*stat* family.
179class StatAttributeTests(unittest.TestCase):
180 def setUp(self):
Walter Dörwald21d3a322003-05-01 17:45:56 +0000181 os.mkdir(test_support.TESTFN)
182 self.fname = os.path.join(test_support.TESTFN, "f1")
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000183 f = open(self.fname, 'wb')
184 f.write("ABC")
185 f.close()
Tim Peterse0c446b2001-10-18 21:57:37 +0000186
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000187 def tearDown(self):
188 os.unlink(self.fname)
Walter Dörwald21d3a322003-05-01 17:45:56 +0000189 os.rmdir(test_support.TESTFN)
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000190
191 def test_stat_attributes(self):
192 if not hasattr(os, "stat"):
193 return
194
195 import stat
196 result = os.stat(self.fname)
197
198 # Make sure direct access works
Ezio Melotti2623a372010-11-21 13:34:58 +0000199 self.assertEqual(result[stat.ST_SIZE], 3)
200 self.assertEqual(result.st_size, 3)
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000201
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000202 # Make sure all the attributes are there
203 members = dir(result)
204 for name in dir(stat):
205 if name[:3] == 'ST_':
206 attr = name.lower()
Martin v. Löwis4d394df2005-01-23 09:19:22 +0000207 if name.endswith("TIME"):
208 def trunc(x): return int(x)
209 else:
210 def trunc(x): return x
Ezio Melotti2623a372010-11-21 13:34:58 +0000211 self.assertEqual(trunc(getattr(result, attr)),
212 result[getattr(stat, name)])
Ezio Melottiaa980582010-01-23 23:04:36 +0000213 self.assertIn(attr, members)
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000214
215 try:
216 result[200]
217 self.fail("No exception thrown")
218 except IndexError:
219 pass
220
221 # Make sure that assignment fails
222 try:
223 result.st_mode = 1
224 self.fail("No exception thrown")
Benjamin Petersonc262a692010-06-30 18:41:08 +0000225 except (AttributeError, TypeError):
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000226 pass
227
228 try:
229 result.st_rdev = 1
230 self.fail("No exception thrown")
Guido van Rossum1fff8782001-10-18 21:19:31 +0000231 except (AttributeError, TypeError):
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000232 pass
233
234 try:
235 result.parrot = 1
236 self.fail("No exception thrown")
237 except AttributeError:
238 pass
239
240 # Use the stat_result constructor with a too-short tuple.
241 try:
242 result2 = os.stat_result((10,))
243 self.fail("No exception thrown")
244 except TypeError:
245 pass
246
Ezio Melotti24b07bc2011-03-15 18:55:01 +0200247 # Use the constructor with a too-long tuple.
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000248 try:
249 result2 = os.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
250 except TypeError:
251 pass
252
Tim Peterse0c446b2001-10-18 21:57:37 +0000253
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000254 def test_statvfs_attributes(self):
255 if not hasattr(os, "statvfs"):
256 return
257
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000258 try:
259 result = os.statvfs(self.fname)
260 except OSError, e:
261 # On AtheOS, glibc always returns ENOSYS
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000262 if e.errno == errno.ENOSYS:
263 return
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000264
265 # Make sure direct access works
Ezio Melotti2623a372010-11-21 13:34:58 +0000266 self.assertEqual(result.f_bfree, result[3])
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000267
Brett Cannon90f2cb42008-05-16 00:37:42 +0000268 # 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 Melotti2623a372010-11-21 13:34:58 +0000272 self.assertEqual(getattr(result, 'f_' + member), result[value])
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000273
274 # Make sure that assignment really fails
275 try:
276 result.f_bfree = 1
277 self.fail("No exception thrown")
278 except TypeError:
279 pass
280
281 try:
282 result.parrot = 1
283 self.fail("No exception thrown")
284 except AttributeError:
285 pass
286
287 # Use the constructor with a too-short tuple.
288 try:
289 result2 = os.statvfs_result((10,))
290 self.fail("No exception thrown")
291 except TypeError:
292 pass
293
Ezio Melotti24b07bc2011-03-15 18:55:01 +0200294 # Use the constructor with a too-long tuple.
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000295 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 Drake38c2ef02001-07-17 20:52:51 +0000299
Martin v. Löwis18aaa562006-10-15 08:43:33 +0000300 def test_utime_dir(self):
301 delta = 1000000
302 st = os.stat(test_support.TESTFN)
Martin v. Löwisa97e06d2006-10-15 11:02:07 +0000303 # 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öwis18aaa562006-10-15 08:43:33 +0000306 st2 = os.stat(test_support.TESTFN)
Ezio Melotti2623a372010-11-21 13:34:58 +0000307 self.assertEqual(st2.st_mtime, int(st.st_mtime-delta))
Martin v. Löwis18aaa562006-10-15 08:43:33 +0000308
Martin v. Löwisf43893a2006-10-09 20:44:25 +0000309 # Restrict test to Win32, since there is no guarantee other
310 # systems support centiseconds
311 if sys.platform == 'win32':
Martin v. Löwis7dcb83c2007-08-30 19:04:09 +0000312 def get_file_system(path):
Hirokazu Yamamotoccfdcd02008-08-20 04:13:28 +0000313 root = os.path.splitdrive(os.path.abspath(path))[0] + '\\'
Martin v. Löwis7dcb83c2007-08-30 19:04:09 +0000314 import ctypes
Hirokazu Yamamotocd3b74d2008-08-20 16:15:28 +0000315 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öwis7dcb83c2007-08-30 19:04:09 +0000318 return buf.value
319
320 if get_file_system(test_support.TESTFN) == "NTFS":
321 def test_1565150(self):
322 t1 = 1159195039.25
323 os.utime(self.fname, (t1, t1))
Ezio Melotti2623a372010-11-21 13:34:58 +0000324 self.assertEqual(os.stat(self.fname).st_mtime, t1)
Martin v. Löwisf43893a2006-10-09 20:44:25 +0000325
Amaury Forgeot d'Arcac514c82011-01-03 00:50:57 +0000326 def test_large_time(self):
327 t1 = 5000000000 # some day in 2128
328 os.utime(self.fname, (t1, t1))
329 self.assertEqual(os.stat(self.fname).st_mtime, t1)
330
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000331 def test_1686475(self):
332 # Verify that an open file can be stat'ed
333 try:
334 os.stat(r"c:\pagefile.sys")
335 except WindowsError, e:
Antoine Pitrou954ea642008-08-17 20:15:07 +0000336 if e.errno == 2: # file does not exist; cannot run test
Martin v. Löwis3bf573f2007-04-04 18:30:36 +0000337 return
338 self.fail("Could not stat pagefile.sys")
339
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000340from test import mapping_tests
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000341
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000342class EnvironTests(mapping_tests.BasicTestMappingProtocol):
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000343 """check that os.environ object conform to mapping protocol"""
Walter Dörwald118f9312004-06-02 18:42:25 +0000344 type2test = None
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000345 def _reference(self):
346 return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"}
347 def _empty_mapping(self):
348 os.environ.clear()
349 return os.environ
350 def setUp(self):
351 self.__save = dict(os.environ)
352 os.environ.clear()
353 def tearDown(self):
354 os.environ.clear()
355 os.environ.update(self.__save)
356
Martin v. Löwis1d11de62005-01-29 13:29:23 +0000357 # Bug 1110478
Martin v. Löwis5510f652005-02-17 21:23:20 +0000358 def test_update2(self):
Martin v. Löwis1d11de62005-01-29 13:29:23 +0000359 if os.path.exists("/bin/sh"):
360 os.environ.update(HELLO="World")
Brian Curtinfcbf5d02010-10-30 21:29:52 +0000361 with os.popen("/bin/sh -c 'echo $HELLO'") as popen:
362 value = popen.read().strip()
Ezio Melotti2623a372010-11-21 13:34:58 +0000363 self.assertEqual(value, "World")
Martin v. Löwis1d11de62005-01-29 13:29:23 +0000364
Charles-François Natali27bc4d02011-11-27 13:05:14 +0100365 # On FreeBSD < 7 and OS X < 10.6, unsetenv() doesn't return a value (issue
366 # #13415).
367 @unittest.skipIf(sys.platform.startswith(('freebsd', 'darwin')),
368 "due to known OS bug: see issue #13415")
Victor Stinner53853c32011-11-22 22:20:13 +0100369 def test_unset_error(self):
370 if sys.platform == "win32":
371 # an environment variable is limited to 32,767 characters
372 key = 'x' * 50000
Victor Stinner091b6ef2011-11-22 22:30:19 +0100373 self.assertRaises(ValueError, os.environ.__delitem__, key)
Victor Stinner53853c32011-11-22 22:20:13 +0100374 else:
375 # "=" is not allowed in a variable name
376 key = 'key='
Victor Stinner091b6ef2011-11-22 22:30:19 +0100377 self.assertRaises(OSError, os.environ.__delitem__, key)
Victor Stinner53853c32011-11-22 22:20:13 +0100378
Tim Petersc4e09402003-04-25 07:11:48 +0000379class WalkTests(unittest.TestCase):
380 """Tests for os.walk()."""
381
382 def test_traversal(self):
383 import os
384 from os.path import join
385
386 # Build:
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000387 # TESTFN/
Georg Brandlcae9f3d2007-03-21 09:10:29 +0000388 # TEST1/ a file kid and two directory kids
Tim Petersc4e09402003-04-25 07:11:48 +0000389 # tmp1
390 # SUB1/ a file kid and a directory kid
Georg Brandlcae9f3d2007-03-21 09:10:29 +0000391 # tmp2
392 # SUB11/ no kids
393 # SUB2/ a file kid and a dirsymlink kid
394 # tmp3
395 # link/ a symlink to TESTFN.2
396 # TEST2/
397 # tmp4 a lone file
398 walk_path = join(test_support.TESTFN, "TEST1")
399 sub1_path = join(walk_path, "SUB1")
Tim Petersc4e09402003-04-25 07:11:48 +0000400 sub11_path = join(sub1_path, "SUB11")
Georg Brandlcae9f3d2007-03-21 09:10:29 +0000401 sub2_path = join(walk_path, "SUB2")
402 tmp1_path = join(walk_path, "tmp1")
Tim Petersc4e09402003-04-25 07:11:48 +0000403 tmp2_path = join(sub1_path, "tmp2")
404 tmp3_path = join(sub2_path, "tmp3")
Georg Brandlcae9f3d2007-03-21 09:10:29 +0000405 link_path = join(sub2_path, "link")
406 t2_path = join(test_support.TESTFN, "TEST2")
407 tmp4_path = join(test_support.TESTFN, "TEST2", "tmp4")
Tim Petersc4e09402003-04-25 07:11:48 +0000408
409 # Create stuff.
410 os.makedirs(sub11_path)
411 os.makedirs(sub2_path)
Georg Brandlcae9f3d2007-03-21 09:10:29 +0000412 os.makedirs(t2_path)
413 for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path:
Tim Petersc4e09402003-04-25 07:11:48 +0000414 f = file(path, "w")
415 f.write("I'm " + path + " and proud of it. Blame test_os.\n")
416 f.close()
Georg Brandlcae9f3d2007-03-21 09:10:29 +0000417 if hasattr(os, "symlink"):
418 os.symlink(os.path.abspath(t2_path), link_path)
Žiga Seilnacht18ffe422007-04-04 18:38:47 +0000419 sub2_tree = (sub2_path, ["link"], ["tmp3"])
Georg Brandlcae9f3d2007-03-21 09:10:29 +0000420 else:
Žiga Seilnacht18ffe422007-04-04 18:38:47 +0000421 sub2_tree = (sub2_path, [], ["tmp3"])
Tim Petersc4e09402003-04-25 07:11:48 +0000422
423 # Walk top-down.
Georg Brandlcae9f3d2007-03-21 09:10:29 +0000424 all = list(os.walk(walk_path))
Tim Petersc4e09402003-04-25 07:11:48 +0000425 self.assertEqual(len(all), 4)
426 # We can't know which order SUB1 and SUB2 will appear in.
427 # Not flipped: TESTFN, SUB1, SUB11, SUB2
428 # flipped: TESTFN, SUB2, SUB1, SUB11
429 flipped = all[0][1][0] != "SUB1"
430 all[0][1].sort()
Georg Brandlcae9f3d2007-03-21 09:10:29 +0000431 self.assertEqual(all[0], (walk_path, ["SUB1", "SUB2"], ["tmp1"]))
Tim Petersc4e09402003-04-25 07:11:48 +0000432 self.assertEqual(all[1 + flipped], (sub1_path, ["SUB11"], ["tmp2"]))
433 self.assertEqual(all[2 + flipped], (sub11_path, [], []))
Žiga Seilnacht18ffe422007-04-04 18:38:47 +0000434 self.assertEqual(all[3 - 2 * flipped], sub2_tree)
Tim Petersc4e09402003-04-25 07:11:48 +0000435
436 # Prune the search.
437 all = []
Georg Brandlcae9f3d2007-03-21 09:10:29 +0000438 for root, dirs, files in os.walk(walk_path):
Tim Petersc4e09402003-04-25 07:11:48 +0000439 all.append((root, dirs, files))
440 # Don't descend into SUB1.
441 if 'SUB1' in dirs:
442 # Note that this also mutates the dirs we appended to all!
443 dirs.remove('SUB1')
444 self.assertEqual(len(all), 2)
Georg Brandlcae9f3d2007-03-21 09:10:29 +0000445 self.assertEqual(all[0], (walk_path, ["SUB2"], ["tmp1"]))
Žiga Seilnacht18ffe422007-04-04 18:38:47 +0000446 self.assertEqual(all[1], sub2_tree)
Tim Petersc4e09402003-04-25 07:11:48 +0000447
448 # Walk bottom-up.
Georg Brandlcae9f3d2007-03-21 09:10:29 +0000449 all = list(os.walk(walk_path, topdown=False))
Tim Petersc4e09402003-04-25 07:11:48 +0000450 self.assertEqual(len(all), 4)
451 # We can't know which order SUB1 and SUB2 will appear in.
452 # Not flipped: SUB11, SUB1, SUB2, TESTFN
453 # flipped: SUB2, SUB11, SUB1, TESTFN
454 flipped = all[3][1][0] != "SUB1"
455 all[3][1].sort()
Georg Brandlcae9f3d2007-03-21 09:10:29 +0000456 self.assertEqual(all[3], (walk_path, ["SUB1", "SUB2"], ["tmp1"]))
Tim Petersc4e09402003-04-25 07:11:48 +0000457 self.assertEqual(all[flipped], (sub11_path, [], []))
458 self.assertEqual(all[flipped + 1], (sub1_path, ["SUB11"], ["tmp2"]))
Žiga Seilnacht18ffe422007-04-04 18:38:47 +0000459 self.assertEqual(all[2 - 2 * flipped], sub2_tree)
Georg Brandlcae9f3d2007-03-21 09:10:29 +0000460
Žiga Seilnacht18ffe422007-04-04 18:38:47 +0000461 if hasattr(os, "symlink"):
462 # Walk, following symlinks.
463 for root, dirs, files in os.walk(walk_path, followlinks=True):
464 if root == link_path:
465 self.assertEqual(dirs, [])
466 self.assertEqual(files, ["tmp4"])
467 break
468 else:
469 self.fail("Didn't follow symlink with followlinks=True")
Tim Petersc4e09402003-04-25 07:11:48 +0000470
Žiga Seilnacht18ffe422007-04-04 18:38:47 +0000471 def tearDown(self):
Tim Petersc4e09402003-04-25 07:11:48 +0000472 # Tear everything down. This is a decent use for bottom-up on
473 # Windows, which doesn't have a recursive delete command. The
474 # (not so) subtlety is that rmdir will fail unless the dir's
475 # kids are removed first, so bottom up is essential.
Walter Dörwald21d3a322003-05-01 17:45:56 +0000476 for root, dirs, files in os.walk(test_support.TESTFN, topdown=False):
Tim Petersc4e09402003-04-25 07:11:48 +0000477 for name in files:
Žiga Seilnacht18ffe422007-04-04 18:38:47 +0000478 os.remove(os.path.join(root, name))
Tim Petersc4e09402003-04-25 07:11:48 +0000479 for name in dirs:
Žiga Seilnacht18ffe422007-04-04 18:38:47 +0000480 dirname = os.path.join(root, name)
Georg Brandlcae9f3d2007-03-21 09:10:29 +0000481 if not os.path.islink(dirname):
482 os.rmdir(dirname)
483 else:
484 os.remove(dirname)
Walter Dörwald21d3a322003-05-01 17:45:56 +0000485 os.rmdir(test_support.TESTFN)
Tim Petersc4e09402003-04-25 07:11:48 +0000486
Andrew M. Kuchlingb386f6a2003-12-23 16:36:11 +0000487class MakedirTests (unittest.TestCase):
488 def setUp(self):
489 os.mkdir(test_support.TESTFN)
490
491 def test_makedir(self):
492 base = test_support.TESTFN
493 path = os.path.join(base, 'dir1', 'dir2', 'dir3')
494 os.makedirs(path) # Should work
495 path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4')
496 os.makedirs(path)
497
498 # Try paths with a '.' in them
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000499 self.assertRaises(OSError, os.makedirs, os.curdir)
Andrew M. Kuchlingb386f6a2003-12-23 16:36:11 +0000500 path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4', 'dir5', os.curdir)
501 os.makedirs(path)
502 path = os.path.join(base, 'dir1', os.curdir, 'dir2', 'dir3', 'dir4',
503 'dir5', 'dir6')
504 os.makedirs(path)
Andrew M. Kuchlingb386f6a2003-12-23 16:36:11 +0000505
Tim Peters58eb11c2004-01-18 20:29:55 +0000506
507
508
Andrew M. Kuchlingb386f6a2003-12-23 16:36:11 +0000509 def tearDown(self):
510 path = os.path.join(test_support.TESTFN, 'dir1', 'dir2', 'dir3',
511 'dir4', 'dir5', 'dir6')
512 # If the tests failed, the bottom-most directory ('../dir6')
513 # may not have been created, so we look for the outermost directory
514 # that exists.
515 while not os.path.exists(path) and path != test_support.TESTFN:
516 path = os.path.dirname(path)
517
518 os.removedirs(path)
519
Martin v. Löwisbdec50f2004-06-08 08:29:33 +0000520class DevNullTests (unittest.TestCase):
521 def test_devnull(self):
522 f = file(os.devnull, 'w')
523 f.write('hello')
524 f.close()
525 f = file(os.devnull, 'r')
Tim Peters4182cfd2004-06-08 20:34:34 +0000526 self.assertEqual(f.read(), '')
Martin v. Löwisbdec50f2004-06-08 08:29:33 +0000527 f.close()
Andrew M. Kuchlingb386f6a2003-12-23 16:36:11 +0000528
Martin v. Löwisdc3883f2004-08-29 15:46:35 +0000529class URandomTests (unittest.TestCase):
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500530
531 def test_urandom_length(self):
532 self.assertEqual(len(os.urandom(0)), 0)
533 self.assertEqual(len(os.urandom(1)), 1)
534 self.assertEqual(len(os.urandom(10)), 10)
535 self.assertEqual(len(os.urandom(100)), 100)
536 self.assertEqual(len(os.urandom(1000)), 1000)
537
538 def test_urandom_value(self):
539 data1 = os.urandom(16)
540 data2 = os.urandom(16)
541 self.assertNotEqual(data1, data2)
542
543 def get_urandom_subprocess(self, count):
Antoine Pitrou341016e2012-02-22 22:16:25 +0100544 # We need to use repr() and eval() to avoid line ending conversions
545 # under Windows.
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500546 code = '\n'.join((
547 'import os, sys',
548 'data = os.urandom(%s)' % count,
Antoine Pitrou341016e2012-02-22 22:16:25 +0100549 'sys.stdout.write(repr(data))',
Antoine Pitrou0607f732012-02-21 22:02:04 +0100550 'sys.stdout.flush()',
551 'print >> sys.stderr, (len(data), data)'))
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500552 cmd_line = [sys.executable, '-c', code]
553 p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
Antoine Pitrou0607f732012-02-21 22:02:04 +0100554 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500555 out, err = p.communicate()
Antoine Pitrou0607f732012-02-21 22:02:04 +0100556 self.assertEqual(p.wait(), 0, (p.wait(), err))
Antoine Pitrou341016e2012-02-22 22:16:25 +0100557 out = eval(out)
558 self.assertEqual(len(out), count, err)
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500559 return out
560
561 def test_urandom_subprocess(self):
562 data1 = self.get_urandom_subprocess(16)
563 data2 = self.get_urandom_subprocess(16)
564 self.assertNotEqual(data1, data2)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +0000565
Matthias Klosee9fbf2b2010-03-19 14:45:06 +0000566 def test_execvpe_with_bad_arglist(self):
567 self.assertRaises(ValueError, os.execvpe, 'notepad', [], None)
568
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000569class Win32ErrorTests(unittest.TestCase):
570 def test_rename(self):
571 self.assertRaises(WindowsError, os.rename, test_support.TESTFN, test_support.TESTFN+".bak")
572
573 def test_remove(self):
574 self.assertRaises(WindowsError, os.remove, test_support.TESTFN)
575
576 def test_chdir(self):
577 self.assertRaises(WindowsError, os.chdir, test_support.TESTFN)
578
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000579 def test_mkdir(self):
Kristján Valur Jónssone20f54f2009-02-06 10:17:34 +0000580 f = open(test_support.TESTFN, "w")
581 try:
582 self.assertRaises(WindowsError, os.mkdir, test_support.TESTFN)
583 finally:
584 f.close()
585 os.unlink(test_support.TESTFN)
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000586
587 def test_utime(self):
588 self.assertRaises(WindowsError, os.utime, test_support.TESTFN, None)
589
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000590 def test_chmod(self):
Kristján Valur Jónssone20f54f2009-02-06 10:17:34 +0000591 self.assertRaises(WindowsError, os.chmod, test_support.TESTFN, 0)
Martin v. Löwisd4e3bb32006-05-06 16:32:54 +0000592
Kristján Valur Jónsson1c62b652009-01-12 18:09:27 +0000593class TestInvalidFD(unittest.TestCase):
Kristján Valur Jónsson71ba2152009-01-15 22:40:03 +0000594 singles = ["fchdir", "fdopen", "dup", "fdatasync", "fstat",
Kristján Valur Jónsson1c62b652009-01-12 18:09:27 +0000595 "fstatvfs", "fsync", "tcgetpgrp", "ttyname"]
Kristján Valur Jónsson71ba2152009-01-15 22:40:03 +0000596 #singles.append("close")
597 #We omit close because it doesn'r raise an exception on some platforms
Kristján Valur Jónsson1c62b652009-01-12 18:09:27 +0000598 def get_single(f):
599 def helper(self):
Benjamin Peterson5539c782009-01-19 17:37:42 +0000600 if hasattr(os, f):
601 self.check(getattr(os, f))
Kristján Valur Jónsson1c62b652009-01-12 18:09:27 +0000602 return helper
603 for f in singles:
604 locals()["test_"+f] = get_single(f)
605
Benjamin Peterson5539c782009-01-19 17:37:42 +0000606 def check(self, f, *args):
Benjamin Peterson1de05e92009-01-31 01:42:55 +0000607 try:
608 f(test_support.make_bad_fd(), *args)
609 except OSError as e:
610 self.assertEqual(e.errno, errno.EBADF)
611 else:
612 self.fail("%r didn't raise a OSError with a bad file descriptor"
613 % f)
Benjamin Peterson5539c782009-01-19 17:37:42 +0000614
Kristján Valur Jónsson1c62b652009-01-12 18:09:27 +0000615 def test_isatty(self):
Kristján Valur Jónsson4f69b7e2009-01-15 22:46:26 +0000616 if hasattr(os, "isatty"):
Benjamin Peterson5539c782009-01-19 17:37:42 +0000617 self.assertEqual(os.isatty(test_support.make_bad_fd()), False)
Kristján Valur Jónsson1c62b652009-01-12 18:09:27 +0000618
619 def test_closerange(self):
Kristján Valur Jónsson4f69b7e2009-01-15 22:46:26 +0000620 if hasattr(os, "closerange"):
Benjamin Peterson5539c782009-01-19 17:37:42 +0000621 fd = test_support.make_bad_fd()
R. David Murray46ca2f22009-07-22 17:22:58 +0000622 # Make sure none of the descriptors we are about to close are
623 # currently valid (issue 6542).
624 for i in range(10):
625 try: os.fstat(fd+i)
626 except OSError:
627 pass
628 else:
629 break
630 if i < 2:
631 raise unittest.SkipTest(
632 "Unable to acquire a range of invalid file descriptors")
633 self.assertEqual(os.closerange(fd, fd + i-1), None)
Kristján Valur Jónsson1c62b652009-01-12 18:09:27 +0000634
635 def test_dup2(self):
Kristján Valur Jónsson4f69b7e2009-01-15 22:46:26 +0000636 if hasattr(os, "dup2"):
Benjamin Peterson5539c782009-01-19 17:37:42 +0000637 self.check(os.dup2, 20)
Kristján Valur Jónsson1c62b652009-01-12 18:09:27 +0000638
639 def test_fchmod(self):
640 if hasattr(os, "fchmod"):
Benjamin Peterson5539c782009-01-19 17:37:42 +0000641 self.check(os.fchmod, 0)
Kristján Valur Jónsson1c62b652009-01-12 18:09:27 +0000642
643 def test_fchown(self):
644 if hasattr(os, "fchown"):
Benjamin Peterson5539c782009-01-19 17:37:42 +0000645 self.check(os.fchown, -1, -1)
Kristján Valur Jónsson1c62b652009-01-12 18:09:27 +0000646
647 def test_fpathconf(self):
648 if hasattr(os, "fpathconf"):
Benjamin Peterson5539c782009-01-19 17:37:42 +0000649 self.check(os.fpathconf, "PC_NAME_MAX")
Kristján Valur Jónsson1c62b652009-01-12 18:09:27 +0000650
651 def test_ftruncate(self):
652 if hasattr(os, "ftruncate"):
Benjamin Peterson5539c782009-01-19 17:37:42 +0000653 self.check(os.ftruncate, 0)
Kristján Valur Jónsson1c62b652009-01-12 18:09:27 +0000654
655 def test_lseek(self):
Kristján Valur Jónsson4f69b7e2009-01-15 22:46:26 +0000656 if hasattr(os, "lseek"):
Benjamin Peterson5539c782009-01-19 17:37:42 +0000657 self.check(os.lseek, 0, 0)
Kristján Valur Jónsson1c62b652009-01-12 18:09:27 +0000658
659 def test_read(self):
Kristján Valur Jónsson4f69b7e2009-01-15 22:46:26 +0000660 if hasattr(os, "read"):
Benjamin Peterson5539c782009-01-19 17:37:42 +0000661 self.check(os.read, 1)
Kristján Valur Jónsson1c62b652009-01-12 18:09:27 +0000662
663 def test_tcsetpgrpt(self):
664 if hasattr(os, "tcsetpgrp"):
Benjamin Peterson5539c782009-01-19 17:37:42 +0000665 self.check(os.tcsetpgrp, 0)
Kristján Valur Jónsson1c62b652009-01-12 18:09:27 +0000666
667 def test_write(self):
Kristján Valur Jónsson4f69b7e2009-01-15 22:46:26 +0000668 if hasattr(os, "write"):
Benjamin Peterson5539c782009-01-19 17:37:42 +0000669 self.check(os.write, " ")
Kristján Valur Jónsson1c62b652009-01-12 18:09:27 +0000670
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000671if sys.platform != 'win32':
672 class Win32ErrorTests(unittest.TestCase):
673 pass
674
Gregory P. Smith6d307932009-04-05 23:43:58 +0000675 class PosixUidGidTests(unittest.TestCase):
676 if hasattr(os, 'setuid'):
677 def test_setuid(self):
678 if os.getuid() != 0:
679 self.assertRaises(os.error, os.setuid, 0)
680 self.assertRaises(OverflowError, os.setuid, 1<<32)
681
682 if hasattr(os, 'setgid'):
683 def test_setgid(self):
684 if os.getuid() != 0:
685 self.assertRaises(os.error, os.setgid, 0)
686 self.assertRaises(OverflowError, os.setgid, 1<<32)
687
688 if hasattr(os, 'seteuid'):
689 def test_seteuid(self):
690 if os.getuid() != 0:
691 self.assertRaises(os.error, os.seteuid, 0)
692 self.assertRaises(OverflowError, os.seteuid, 1<<32)
693
694 if hasattr(os, 'setegid'):
695 def test_setegid(self):
696 if os.getuid() != 0:
697 self.assertRaises(os.error, os.setegid, 0)
698 self.assertRaises(OverflowError, os.setegid, 1<<32)
699
700 if hasattr(os, 'setreuid'):
701 def test_setreuid(self):
702 if os.getuid() != 0:
703 self.assertRaises(os.error, os.setreuid, 0, 0)
704 self.assertRaises(OverflowError, os.setreuid, 1<<32, 0)
705 self.assertRaises(OverflowError, os.setreuid, 0, 1<<32)
Gregory P. Smith467298c2010-03-06 07:35:19 +0000706
707 def test_setreuid_neg1(self):
708 # Needs to accept -1. We run this in a subprocess to avoid
709 # altering the test runner's process state (issue8045).
Gregory P. Smith467298c2010-03-06 07:35:19 +0000710 subprocess.check_call([
711 sys.executable, '-c',
712 'import os,sys;os.setreuid(-1,-1);sys.exit(0)'])
Gregory P. Smith6d307932009-04-05 23:43:58 +0000713
714 if hasattr(os, 'setregid'):
715 def test_setregid(self):
716 if os.getuid() != 0:
717 self.assertRaises(os.error, os.setregid, 0, 0)
718 self.assertRaises(OverflowError, os.setregid, 1<<32, 0)
719 self.assertRaises(OverflowError, os.setregid, 0, 1<<32)
Gregory P. Smith467298c2010-03-06 07:35:19 +0000720
721 def test_setregid_neg1(self):
722 # Needs to accept -1. We run this in a subprocess to avoid
723 # altering the test runner's process state (issue8045).
Gregory P. Smith467298c2010-03-06 07:35:19 +0000724 subprocess.check_call([
725 sys.executable, '-c',
726 'import os,sys;os.setregid(-1,-1);sys.exit(0)'])
Gregory P. Smith6d307932009-04-05 23:43:58 +0000727else:
728 class PosixUidGidTests(unittest.TestCase):
729 pass
730
Brian Curtine5aa8862010-04-02 23:26:06 +0000731@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
732class Win32KillTests(unittest.TestCase):
Brian Curtinb3dde132010-04-15 00:40:40 +0000733 def _kill(self, sig):
734 # Start sys.executable as a subprocess and communicate from the
735 # subprocess to the parent that the interpreter is ready. When it
736 # becomes ready, send *sig* via os.kill to the subprocess and check
737 # that the return code is equal to *sig*.
738 import ctypes
739 from ctypes import wintypes
740 import msvcrt
741
742 # Since we can't access the contents of the process' stdout until the
743 # process has exited, use PeekNamedPipe to see what's inside stdout
744 # without waiting. This is done so we can tell that the interpreter
745 # is started and running at a point where it could handle a signal.
746 PeekNamedPipe = ctypes.windll.kernel32.PeekNamedPipe
747 PeekNamedPipe.restype = wintypes.BOOL
748 PeekNamedPipe.argtypes = (wintypes.HANDLE, # Pipe handle
749 ctypes.POINTER(ctypes.c_char), # stdout buf
750 wintypes.DWORD, # Buffer size
751 ctypes.POINTER(wintypes.DWORD), # bytes read
752 ctypes.POINTER(wintypes.DWORD), # bytes avail
753 ctypes.POINTER(wintypes.DWORD)) # bytes left
754 msg = "running"
755 proc = subprocess.Popen([sys.executable, "-c",
756 "import sys;"
757 "sys.stdout.write('{}');"
758 "sys.stdout.flush();"
759 "input()".format(msg)],
760 stdout=subprocess.PIPE,
761 stderr=subprocess.PIPE,
762 stdin=subprocess.PIPE)
Brian Curtinf4f0c8b2010-11-05 15:31:20 +0000763 self.addCleanup(proc.stdout.close)
764 self.addCleanup(proc.stderr.close)
765 self.addCleanup(proc.stdin.close)
Brian Curtinb3dde132010-04-15 00:40:40 +0000766
Brian Curtin83cba052010-05-28 15:49:21 +0000767 count, max = 0, 100
768 while count < max and proc.poll() is None:
769 # Create a string buffer to store the result of stdout from the pipe
770 buf = ctypes.create_string_buffer(len(msg))
771 # Obtain the text currently in proc.stdout
772 # Bytes read/avail/left are left as NULL and unused
773 rslt = PeekNamedPipe(msvcrt.get_osfhandle(proc.stdout.fileno()),
774 buf, ctypes.sizeof(buf), None, None, None)
775 self.assertNotEqual(rslt, 0, "PeekNamedPipe failed")
776 if buf.value:
777 self.assertEqual(msg, buf.value)
778 break
779 time.sleep(0.1)
780 count += 1
781 else:
782 self.fail("Did not receive communication from the subprocess")
Brian Curtinb3dde132010-04-15 00:40:40 +0000783
Brian Curtine5aa8862010-04-02 23:26:06 +0000784 os.kill(proc.pid, sig)
785 self.assertEqual(proc.wait(), sig)
786
787 def test_kill_sigterm(self):
788 # SIGTERM doesn't mean anything special, but make sure it works
Brian Curtinb3dde132010-04-15 00:40:40 +0000789 self._kill(signal.SIGTERM)
Brian Curtine5aa8862010-04-02 23:26:06 +0000790
791 def test_kill_int(self):
792 # os.kill on Windows can take an int which gets set as the exit code
Brian Curtinb3dde132010-04-15 00:40:40 +0000793 self._kill(100)
Brian Curtine5aa8862010-04-02 23:26:06 +0000794
795 def _kill_with_event(self, event, name):
Hirokazu Yamamoto1f504f12010-10-08 09:41:13 +0000796 tagname = "test_os_%s" % uuid.uuid1()
797 m = mmap.mmap(-1, 1, tagname)
798 m[0] = '0'
Brian Curtine5aa8862010-04-02 23:26:06 +0000799 # Run a script which has console control handling enabled.
800 proc = subprocess.Popen([sys.executable,
801 os.path.join(os.path.dirname(__file__),
Hirokazu Yamamoto1f504f12010-10-08 09:41:13 +0000802 "win_console_handler.py"), tagname],
Brian Curtine5aa8862010-04-02 23:26:06 +0000803 creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
804 # Let the interpreter startup before we send signals. See #3137.
Hirokazu Yamamoto1f504f12010-10-08 09:41:13 +0000805 count, max = 0, 20
806 while count < max and proc.poll() is None:
Brian Curtindbf8e832010-11-05 15:28:19 +0000807 if m[0] == '1':
Hirokazu Yamamoto1f504f12010-10-08 09:41:13 +0000808 break
809 time.sleep(0.5)
810 count += 1
811 else:
812 self.fail("Subprocess didn't finish initialization")
Brian Curtine5aa8862010-04-02 23:26:06 +0000813 os.kill(proc.pid, event)
814 # proc.send_signal(event) could also be done here.
815 # Allow time for the signal to be passed and the process to exit.
Brian Curtinfce1d312010-04-05 19:04:23 +0000816 time.sleep(0.5)
Brian Curtine5aa8862010-04-02 23:26:06 +0000817 if not proc.poll():
818 # Forcefully kill the process if we weren't able to signal it.
819 os.kill(proc.pid, signal.SIGINT)
820 self.fail("subprocess did not stop on {}".format(name))
821
822 @unittest.skip("subprocesses aren't inheriting CTRL+C property")
823 def test_CTRL_C_EVENT(self):
824 from ctypes import wintypes
825 import ctypes
826
827 # Make a NULL value by creating a pointer with no argument.
828 NULL = ctypes.POINTER(ctypes.c_int)()
829 SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler
830 SetConsoleCtrlHandler.argtypes = (ctypes.POINTER(ctypes.c_int),
831 wintypes.BOOL)
832 SetConsoleCtrlHandler.restype = wintypes.BOOL
833
834 # Calling this with NULL and FALSE causes the calling process to
835 # handle CTRL+C, rather than ignore it. This property is inherited
836 # by subprocesses.
837 SetConsoleCtrlHandler(NULL, 0)
838
839 self._kill_with_event(signal.CTRL_C_EVENT, "CTRL_C_EVENT")
840
841 def test_CTRL_BREAK_EVENT(self):
842 self._kill_with_event(signal.CTRL_BREAK_EVENT, "CTRL_BREAK_EVENT")
843
844
Fred Drake2e2be372001-09-20 21:33:42 +0000845def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000846 test_support.run_unittest(
Martin v. Löwisee1e06d2006-07-02 18:44:00 +0000847 FileTests,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000848 TemporaryFileTests,
849 StatAttributeTests,
850 EnvironTests,
Andrew M. Kuchlingb386f6a2003-12-23 16:36:11 +0000851 WalkTests,
852 MakedirTests,
Martin v. Löwisbdec50f2004-06-08 08:29:33 +0000853 DevNullTests,
Martin v. Löwis8e0d4942006-05-04 10:08:42 +0000854 URandomTests,
Kristján Valur Jónsson1c62b652009-01-12 18:09:27 +0000855 Win32ErrorTests,
Gregory P. Smith6d307932009-04-05 23:43:58 +0000856 TestInvalidFD,
Brian Curtine5aa8862010-04-02 23:26:06 +0000857 PosixUidGidTests,
858 Win32KillTests
Walter Dörwald21d3a322003-05-01 17:45:56 +0000859 )
Fred Drake2e2be372001-09-20 21:33:42 +0000860
861if __name__ == "__main__":
862 test_main()