blob: 11180b7278c9d70a443dbf5b9e56a6ed26dc8f95 [file] [log] [blame]
Neal Norwitze241ce82003-02-17 18:17:05 +00001"Test posix functions"
2
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support
Antoine Pitrou346cbd32017-05-27 17:50:54 +02004from test.support.script_helper import assert_python_ok
R. David Murrayeb3615d2009-04-22 02:24:39 +00005
6# Skip these tests if there is no posix module.
7posix = support.import_module('posix')
Neal Norwitze241ce82003-02-17 18:17:05 +00008
Antoine Pitroub7572f02009-12-02 20:46:48 +00009import errno
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +000010import sys
Neal Norwitze241ce82003-02-17 18:17:05 +000011import time
12import os
Charles-François Nataliab2d58e2012-04-17 19:48:35 +020013import platform
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000014import pwd
Benjamin Peterson052a02b2010-08-17 01:27:09 +000015import stat
Ned Deilyba2eab22011-07-26 13:53:55 -070016import tempfile
Neal Norwitze241ce82003-02-17 18:17:05 +000017import unittest
18import warnings
R. David Murraya21e4ca2009-03-31 23:16:50 +000019
Ned Deilyba2eab22011-07-26 13:53:55 -070020_DUMMY_SYMLINK = os.path.join(tempfile.gettempdir(),
21 support.TESTFN + '-dummy-symlink')
Neal Norwitze241ce82003-02-17 18:17:05 +000022
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -070023requires_32b = unittest.skipUnless(sys.maxsize < 2**32,
24 'test is only meaningful on 32-bit builds')
25
Benjamin Peterson0aef9092018-09-12 16:00:06 -070026def _supports_sched():
27 if not hasattr(posix, 'sched_getscheduler'):
28 return False
29 try:
30 posix.sched_getscheduler(0)
31 except OSError as e:
32 if e.errno == errno.ENOSYS:
33 return False
34 return True
35
36requires_sched = unittest.skipUnless(_supports_sched(), 'requires POSIX scheduler API')
37
Neal Norwitze241ce82003-02-17 18:17:05 +000038class PosixTester(unittest.TestCase):
39
40 def setUp(self):
41 # create empty file
Benjamin Petersonee8712c2008-05-20 21:35:26 +000042 fp = open(support.TESTFN, 'w+')
Neal Norwitze241ce82003-02-17 18:17:05 +000043 fp.close()
Ned Deily3eb67d52011-06-28 00:00:28 -070044 self.teardown_files = [ support.TESTFN ]
Brett Cannonc8d502e2010-03-20 21:53:28 +000045 self._warnings_manager = support.check_warnings()
46 self._warnings_manager.__enter__()
47 warnings.filterwarnings('ignore', '.* potential security risk .*',
48 RuntimeWarning)
Neal Norwitze241ce82003-02-17 18:17:05 +000049
50 def tearDown(self):
Ned Deily3eb67d52011-06-28 00:00:28 -070051 for teardown_file in self.teardown_files:
52 support.unlink(teardown_file)
Brett Cannonc8d502e2010-03-20 21:53:28 +000053 self._warnings_manager.__exit__(None, None, None)
Neal Norwitze241ce82003-02-17 18:17:05 +000054
55 def testNoArgFunctions(self):
56 # test posix functions which take no arguments and have
57 # no side-effects which we need to cleanup (e.g., fork, wait, abort)
Guido van Rossumf0af3e32008-10-02 18:55:37 +000058 NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdb", "uname",
Guido van Rossum687b9c02007-10-25 23:18:51 +000059 "times", "getloadavg",
Neal Norwitze241ce82003-02-17 18:17:05 +000060 "getegid", "geteuid", "getgid", "getgroups",
Ross Lagerwall7807c352011-03-17 20:20:30 +020061 "getpid", "getpgrp", "getppid", "getuid", "sync",
Neal Norwitze241ce82003-02-17 18:17:05 +000062 ]
Neal Norwitz71b13e82003-02-23 22:12:24 +000063
Neal Norwitze241ce82003-02-17 18:17:05 +000064 for name in NO_ARG_FUNCTIONS:
65 posix_func = getattr(posix, name, None)
66 if posix_func is not None:
67 posix_func()
Neal Norwitz2ff51a82003-02-17 22:40:31 +000068 self.assertRaises(TypeError, posix_func, 1)
Neal Norwitze241ce82003-02-17 18:17:05 +000069
Serhiy Storchaka43767632013-11-03 21:31:38 +020070 @unittest.skipUnless(hasattr(posix, 'getresuid'),
71 'test needs posix.getresuid()')
72 def test_getresuid(self):
73 user_ids = posix.getresuid()
74 self.assertEqual(len(user_ids), 3)
75 for val in user_ids:
76 self.assertGreaterEqual(val, 0)
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000077
Serhiy Storchaka43767632013-11-03 21:31:38 +020078 @unittest.skipUnless(hasattr(posix, 'getresgid'),
79 'test needs posix.getresgid()')
80 def test_getresgid(self):
81 group_ids = posix.getresgid()
82 self.assertEqual(len(group_ids), 3)
83 for val in group_ids:
84 self.assertGreaterEqual(val, 0)
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000085
Serhiy Storchaka43767632013-11-03 21:31:38 +020086 @unittest.skipUnless(hasattr(posix, 'setresuid'),
87 'test needs posix.setresuid()')
88 def test_setresuid(self):
89 current_user_ids = posix.getresuid()
90 self.assertIsNone(posix.setresuid(*current_user_ids))
91 # -1 means don't change that value.
92 self.assertIsNone(posix.setresuid(-1, -1, -1))
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000093
Serhiy Storchaka43767632013-11-03 21:31:38 +020094 @unittest.skipUnless(hasattr(posix, 'setresuid'),
95 'test needs posix.setresuid()')
96 def test_setresuid_exception(self):
97 # Don't do this test if someone is silly enough to run us as root.
98 current_user_ids = posix.getresuid()
99 if 0 not in current_user_ids:
100 new_user_ids = (current_user_ids[0]+1, -1, -1)
101 self.assertRaises(OSError, posix.setresuid, *new_user_ids)
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000102
Serhiy Storchaka43767632013-11-03 21:31:38 +0200103 @unittest.skipUnless(hasattr(posix, 'setresgid'),
104 'test needs posix.setresgid()')
105 def test_setresgid(self):
106 current_group_ids = posix.getresgid()
107 self.assertIsNone(posix.setresgid(*current_group_ids))
108 # -1 means don't change that value.
109 self.assertIsNone(posix.setresgid(-1, -1, -1))
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000110
Serhiy Storchaka43767632013-11-03 21:31:38 +0200111 @unittest.skipUnless(hasattr(posix, 'setresgid'),
112 'test needs posix.setresgid()')
113 def test_setresgid_exception(self):
114 # Don't do this test if someone is silly enough to run us as root.
115 current_group_ids = posix.getresgid()
116 if 0 not in current_group_ids:
117 new_group_ids = (current_group_ids[0]+1, -1, -1)
118 self.assertRaises(OSError, posix.setresgid, *new_group_ids)
Martin v. Löwis7aed61a2009-11-27 14:09:49 +0000119
Antoine Pitroub7572f02009-12-02 20:46:48 +0000120 @unittest.skipUnless(hasattr(posix, 'initgroups'),
121 "test needs os.initgroups()")
122 def test_initgroups(self):
123 # It takes a string and an integer; check that it raises a TypeError
124 # for other argument lists.
125 self.assertRaises(TypeError, posix.initgroups)
126 self.assertRaises(TypeError, posix.initgroups, None)
127 self.assertRaises(TypeError, posix.initgroups, 3, "foo")
128 self.assertRaises(TypeError, posix.initgroups, "foo", 3, object())
129
130 # If a non-privileged user invokes it, it should fail with OSError
131 # EPERM.
132 if os.getuid() != 0:
Charles-François Natalie8a255a2012-05-02 20:01:38 +0200133 try:
134 name = pwd.getpwuid(posix.getuid()).pw_name
135 except KeyError:
136 # the current UID may not have a pwd entry
137 raise unittest.SkipTest("need a pwd entry")
Antoine Pitroub7572f02009-12-02 20:46:48 +0000138 try:
139 posix.initgroups(name, 13)
140 except OSError as e:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000141 self.assertEqual(e.errno, errno.EPERM)
Antoine Pitroub7572f02009-12-02 20:46:48 +0000142 else:
143 self.fail("Expected OSError to be raised by initgroups")
144
Serhiy Storchaka43767632013-11-03 21:31:38 +0200145 @unittest.skipUnless(hasattr(posix, 'statvfs'),
146 'test needs posix.statvfs()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000147 def test_statvfs(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200148 self.assertTrue(posix.statvfs(os.curdir))
Neal Norwitze241ce82003-02-17 18:17:05 +0000149
Serhiy Storchaka43767632013-11-03 21:31:38 +0200150 @unittest.skipUnless(hasattr(posix, 'fstatvfs'),
151 'test needs posix.fstatvfs()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000152 def test_fstatvfs(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200153 fp = open(support.TESTFN)
154 try:
155 self.assertTrue(posix.fstatvfs(fp.fileno()))
156 self.assertTrue(posix.statvfs(fp.fileno()))
157 finally:
158 fp.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000159
Serhiy Storchaka43767632013-11-03 21:31:38 +0200160 @unittest.skipUnless(hasattr(posix, 'ftruncate'),
161 'test needs posix.ftruncate()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000162 def test_ftruncate(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200163 fp = open(support.TESTFN, 'w+')
164 try:
165 # we need to have some data to truncate
166 fp.write('test')
167 fp.flush()
168 posix.ftruncate(fp.fileno(), 0)
169 finally:
170 fp.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000171
Ross Lagerwall7807c352011-03-17 20:20:30 +0200172 @unittest.skipUnless(hasattr(posix, 'truncate'), "test needs posix.truncate()")
173 def test_truncate(self):
174 with open(support.TESTFN, 'w') as fp:
175 fp.write('test')
176 fp.flush()
177 posix.truncate(support.TESTFN, 0)
178
Larry Hastings9cf065c2012-06-22 16:30:09 -0700179 @unittest.skipUnless(getattr(os, 'execve', None) in os.supports_fd, "test needs execve() to support the fd parameter")
Ross Lagerwall7807c352011-03-17 20:20:30 +0200180 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
Ross Lagerwalldedf6cf2011-03-20 18:27:05 +0200181 @unittest.skipUnless(hasattr(os, 'waitpid'), "test needs os.waitpid()")
Ross Lagerwall7807c352011-03-17 20:20:30 +0200182 def test_fexecve(self):
183 fp = os.open(sys.executable, os.O_RDONLY)
184 try:
185 pid = os.fork()
186 if pid == 0:
187 os.chdir(os.path.split(sys.executable)[0])
Larry Hastings9cf065c2012-06-22 16:30:09 -0700188 posix.execve(fp, [sys.executable, '-c', 'pass'], os.environ)
Ross Lagerwall7807c352011-03-17 20:20:30 +0200189 else:
Ross Lagerwalldedf6cf2011-03-20 18:27:05 +0200190 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
Ross Lagerwall7807c352011-03-17 20:20:30 +0200191 finally:
192 os.close(fp)
193
Pablo Galindo6c6ddf92018-01-29 01:56:10 +0000194
Ross Lagerwall7807c352011-03-17 20:20:30 +0200195 @unittest.skipUnless(hasattr(posix, 'waitid'), "test needs posix.waitid()")
196 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
197 def test_waitid(self):
198 pid = os.fork()
199 if pid == 0:
200 os.chdir(os.path.split(sys.executable)[0])
201 posix.execve(sys.executable, [sys.executable, '-c', 'pass'], os.environ)
202 else:
203 res = posix.waitid(posix.P_PID, pid, posix.WEXITED)
204 self.assertEqual(pid, res.si_pid)
205
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200206 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
Gregory P. Smith163468a2017-05-29 10:03:41 -0700207 def test_register_at_fork(self):
208 with self.assertRaises(TypeError, msg="Positional args not allowed"):
209 os.register_at_fork(lambda: None)
210 with self.assertRaises(TypeError, msg="Args must be callable"):
211 os.register_at_fork(before=2)
212 with self.assertRaises(TypeError, msg="Args must be callable"):
213 os.register_at_fork(after_in_child="three")
214 with self.assertRaises(TypeError, msg="Args must be callable"):
215 os.register_at_fork(after_in_parent=b"Five")
216 with self.assertRaises(TypeError, msg="Args must not be None"):
217 os.register_at_fork(before=None)
218 with self.assertRaises(TypeError, msg="Args must not be None"):
219 os.register_at_fork(after_in_child=None)
220 with self.assertRaises(TypeError, msg="Args must not be None"):
221 os.register_at_fork(after_in_parent=None)
222 with self.assertRaises(TypeError, msg="Invalid arg was allowed"):
223 # Ensure a combination of valid and invalid is an error.
224 os.register_at_fork(before=None, after_in_parent=lambda: 3)
225 with self.assertRaises(TypeError, msg="Invalid arg was allowed"):
226 # Ensure a combination of valid and invalid is an error.
227 os.register_at_fork(before=lambda: None, after_in_child='')
228 # We test actual registrations in their own process so as not to
229 # pollute this one. There is no way to unregister for cleanup.
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200230 code = """if 1:
231 import os
232
233 r, w = os.pipe()
234 fin_r, fin_w = os.pipe()
235
Gregory P. Smith163468a2017-05-29 10:03:41 -0700236 os.register_at_fork(before=lambda: os.write(w, b'A'))
237 os.register_at_fork(after_in_parent=lambda: os.write(w, b'C'))
238 os.register_at_fork(after_in_child=lambda: os.write(w, b'E'))
239 os.register_at_fork(before=lambda: os.write(w, b'B'),
240 after_in_parent=lambda: os.write(w, b'D'),
241 after_in_child=lambda: os.write(w, b'F'))
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200242
243 pid = os.fork()
244 if pid == 0:
245 # At this point, after-forkers have already been executed
246 os.close(w)
247 # Wait for parent to tell us to exit
248 os.read(fin_r, 1)
249 os._exit(0)
250 else:
251 try:
252 os.close(w)
253 with open(r, "rb") as f:
254 data = f.read()
255 assert len(data) == 6, data
256 # Check before-fork callbacks
257 assert data[:2] == b'BA', data
258 # Check after-fork callbacks
259 assert sorted(data[2:]) == list(b'CDEF'), data
260 assert data.index(b'C') < data.index(b'D'), data
261 assert data.index(b'E') < data.index(b'F'), data
262 finally:
263 os.write(fin_w, b'!')
264 """
265 assert_python_ok('-c', code)
266
Ross Lagerwall7807c352011-03-17 20:20:30 +0200267 @unittest.skipUnless(hasattr(posix, 'lockf'), "test needs posix.lockf()")
268 def test_lockf(self):
269 fd = os.open(support.TESTFN, os.O_WRONLY | os.O_CREAT)
270 try:
271 os.write(fd, b'test')
272 os.lseek(fd, 0, os.SEEK_SET)
273 posix.lockf(fd, posix.F_LOCK, 4)
274 # section is locked
275 posix.lockf(fd, posix.F_ULOCK, 4)
276 finally:
277 os.close(fd)
278
279 @unittest.skipUnless(hasattr(posix, 'pread'), "test needs posix.pread()")
280 def test_pread(self):
281 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
282 try:
283 os.write(fd, b'test')
284 os.lseek(fd, 0, os.SEEK_SET)
285 self.assertEqual(b'es', posix.pread(fd, 2, 1))
Florent Xiclunae41f0de2011-11-11 19:39:25 +0100286 # the first pread() shouldn't disturb the file offset
Ross Lagerwall7807c352011-03-17 20:20:30 +0200287 self.assertEqual(b'te', posix.read(fd, 2))
288 finally:
289 os.close(fd)
290
Pablo Galindo4defba32018-01-27 16:16:37 +0000291 @unittest.skipUnless(hasattr(posix, 'preadv'), "test needs posix.preadv()")
292 def test_preadv(self):
293 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
294 try:
295 os.write(fd, b'test1tt2t3t5t6t6t8')
296 buf = [bytearray(i) for i in [5, 3, 2]]
297 self.assertEqual(posix.preadv(fd, buf, 3), 10)
298 self.assertEqual([b't1tt2', b't3t', b'5t'], list(buf))
299 finally:
300 os.close(fd)
301
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -0700302 @unittest.skipUnless(hasattr(posix, 'preadv'), "test needs posix.preadv()")
Pablo Galindo4defba32018-01-27 16:16:37 +0000303 @unittest.skipUnless(hasattr(posix, 'RWF_HIPRI'), "test needs posix.RWF_HIPRI")
304 def test_preadv_flags(self):
305 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
306 try:
307 os.write(fd, b'test1tt2t3t5t6t6t8')
308 buf = [bytearray(i) for i in [5, 3, 2]]
309 self.assertEqual(posix.preadv(fd, buf, 3, os.RWF_HIPRI), 10)
310 self.assertEqual([b't1tt2', b't3t', b'5t'], list(buf))
311 finally:
312 os.close(fd)
313
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -0700314 @unittest.skipUnless(hasattr(posix, 'preadv'), "test needs posix.preadv()")
315 @requires_32b
316 def test_preadv_overflow_32bits(self):
317 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
318 try:
319 buf = [bytearray(2**16)] * 2**15
320 with self.assertRaises(OSError) as cm:
321 os.preadv(fd, buf, 0)
322 self.assertEqual(cm.exception.errno, errno.EINVAL)
323 self.assertEqual(bytes(buf[0]), b'\0'* 2**16)
324 finally:
325 os.close(fd)
326
Ross Lagerwall7807c352011-03-17 20:20:30 +0200327 @unittest.skipUnless(hasattr(posix, 'pwrite'), "test needs posix.pwrite()")
328 def test_pwrite(self):
329 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
330 try:
331 os.write(fd, b'test')
332 os.lseek(fd, 0, os.SEEK_SET)
333 posix.pwrite(fd, b'xx', 1)
334 self.assertEqual(b'txxt', posix.read(fd, 4))
335 finally:
336 os.close(fd)
337
Pablo Galindo4defba32018-01-27 16:16:37 +0000338 @unittest.skipUnless(hasattr(posix, 'pwritev'), "test needs posix.pwritev()")
339 def test_pwritev(self):
340 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
341 try:
342 os.write(fd, b"xx")
343 os.lseek(fd, 0, os.SEEK_SET)
344 n = os.pwritev(fd, [b'test1', b'tt2', b't3'], 2)
345 self.assertEqual(n, 10)
346
347 os.lseek(fd, 0, os.SEEK_SET)
348 self.assertEqual(b'xxtest1tt2t3', posix.read(fd, 100))
349 finally:
350 os.close(fd)
351
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -0700352 @unittest.skipUnless(hasattr(posix, 'pwritev'), "test needs posix.pwritev()")
Pablo Galindo4defba32018-01-27 16:16:37 +0000353 @unittest.skipUnless(hasattr(posix, 'os.RWF_SYNC'), "test needs os.RWF_SYNC")
354 def test_pwritev_flags(self):
355 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
356 try:
357 os.write(fd,b"xx")
358 os.lseek(fd, 0, os.SEEK_SET)
359 n = os.pwritev(fd, [b'test1', b'tt2', b't3'], 2, os.RWF_SYNC)
360 self.assertEqual(n, 10)
361
362 os.lseek(fd, 0, os.SEEK_SET)
363 self.assertEqual(b'xxtest1tt2', posix.read(fd, 100))
364 finally:
365 os.close(fd)
366
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -0700367 @unittest.skipUnless(hasattr(posix, 'pwritev'), "test needs posix.pwritev()")
368 @requires_32b
369 def test_pwritev_overflow_32bits(self):
370 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
371 try:
372 with self.assertRaises(OSError) as cm:
373 os.pwritev(fd, [b"x" * 2**16] * 2**15, 0)
374 self.assertEqual(cm.exception.errno, errno.EINVAL)
375 finally:
376 os.close(fd)
377
Ross Lagerwall7807c352011-03-17 20:20:30 +0200378 @unittest.skipUnless(hasattr(posix, 'posix_fallocate'),
379 "test needs posix.posix_fallocate()")
380 def test_posix_fallocate(self):
381 fd = os.open(support.TESTFN, os.O_WRONLY | os.O_CREAT)
382 try:
383 posix.posix_fallocate(fd, 0, 10)
384 except OSError as inst:
385 # issue10812, ZFS doesn't appear to support posix_fallocate,
386 # so skip Solaris-based since they are likely to have ZFS.
Miss Islington (bot)96fb8282018-05-26 14:57:01 -0700387 # issue33655: Also ignore EINVAL on *BSD since ZFS is also
388 # often used there.
389 if inst.errno == errno.EINVAL and sys.platform.startswith(
390 ('sunos', 'freebsd', 'netbsd', 'openbsd', 'gnukfreebsd')):
391 raise unittest.SkipTest("test may fail on ZFS filesystems")
392 else:
Ross Lagerwall7807c352011-03-17 20:20:30 +0200393 raise
394 finally:
395 os.close(fd)
396
Коренберг Маркd4b93e22017-08-14 18:55:16 +0500397 # issue31106 - posix_fallocate() does not set error in errno.
398 @unittest.skipUnless(hasattr(posix, 'posix_fallocate'),
399 "test needs posix.posix_fallocate()")
400 def test_posix_fallocate_errno(self):
401 try:
402 posix.posix_fallocate(-42, 0, 10)
403 except OSError as inst:
404 if inst.errno != errno.EBADF:
405 raise
406
Ross Lagerwall7807c352011-03-17 20:20:30 +0200407 @unittest.skipUnless(hasattr(posix, 'posix_fadvise'),
408 "test needs posix.posix_fadvise()")
409 def test_posix_fadvise(self):
410 fd = os.open(support.TESTFN, os.O_RDONLY)
411 try:
412 posix.posix_fadvise(fd, 0, 0, posix.POSIX_FADV_WILLNEED)
413 finally:
414 os.close(fd)
415
Коренберг Маркd4b93e22017-08-14 18:55:16 +0500416 @unittest.skipUnless(hasattr(posix, 'posix_fadvise'),
417 "test needs posix.posix_fadvise()")
418 def test_posix_fadvise_errno(self):
419 try:
420 posix.posix_fadvise(-42, 0, 0, posix.POSIX_FADV_WILLNEED)
421 except OSError as inst:
422 if inst.errno != errno.EBADF:
423 raise
424
Larry Hastings9cf065c2012-06-22 16:30:09 -0700425 @unittest.skipUnless(os.utime in os.supports_fd, "test needs fd support in os.utime")
426 def test_utime_with_fd(self):
Ross Lagerwall7807c352011-03-17 20:20:30 +0200427 now = time.time()
428 fd = os.open(support.TESTFN, os.O_RDONLY)
429 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -0700430 posix.utime(fd)
431 posix.utime(fd, None)
432 self.assertRaises(TypeError, posix.utime, fd, (None, None))
433 self.assertRaises(TypeError, posix.utime, fd, (now, None))
434 self.assertRaises(TypeError, posix.utime, fd, (None, now))
435 posix.utime(fd, (int(now), int(now)))
436 posix.utime(fd, (now, now))
437 self.assertRaises(ValueError, posix.utime, fd, (now, now), ns=(now, now))
438 self.assertRaises(ValueError, posix.utime, fd, (now, 0), ns=(None, None))
439 self.assertRaises(ValueError, posix.utime, fd, (None, None), ns=(now, 0))
440 posix.utime(fd, (int(now), int((now - int(now)) * 1e9)))
441 posix.utime(fd, ns=(int(now), int((now - int(now)) * 1e9)))
442
Ross Lagerwall7807c352011-03-17 20:20:30 +0200443 finally:
444 os.close(fd)
445
Larry Hastings9cf065c2012-06-22 16:30:09 -0700446 @unittest.skipUnless(os.utime in os.supports_follow_symlinks, "test needs follow_symlinks support in os.utime")
447 def test_utime_nofollow_symlinks(self):
Ross Lagerwall7807c352011-03-17 20:20:30 +0200448 now = time.time()
Larry Hastings9cf065c2012-06-22 16:30:09 -0700449 posix.utime(support.TESTFN, None, follow_symlinks=False)
450 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None), follow_symlinks=False)
451 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None), follow_symlinks=False)
452 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now), follow_symlinks=False)
453 posix.utime(support.TESTFN, (int(now), int(now)), follow_symlinks=False)
454 posix.utime(support.TESTFN, (now, now), follow_symlinks=False)
455 posix.utime(support.TESTFN, follow_symlinks=False)
Ross Lagerwall7807c352011-03-17 20:20:30 +0200456
457 @unittest.skipUnless(hasattr(posix, 'writev'), "test needs posix.writev()")
458 def test_writev(self):
459 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
460 try:
Victor Stinner57ddf782014-01-08 15:21:28 +0100461 n = os.writev(fd, (b'test1', b'tt2', b't3'))
462 self.assertEqual(n, 10)
463
Ross Lagerwall7807c352011-03-17 20:20:30 +0200464 os.lseek(fd, 0, os.SEEK_SET)
465 self.assertEqual(b'test1tt2t3', posix.read(fd, 10))
Victor Stinner57ddf782014-01-08 15:21:28 +0100466
467 # Issue #20113: empty list of buffers should not crash
Victor Stinnercd5ca6a2014-01-08 16:01:31 +0100468 try:
469 size = posix.writev(fd, [])
470 except OSError:
471 # writev(fd, []) raises OSError(22, "Invalid argument")
472 # on OpenIndiana
473 pass
474 else:
475 self.assertEqual(size, 0)
Ross Lagerwall7807c352011-03-17 20:20:30 +0200476 finally:
477 os.close(fd)
478
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -0700479 @unittest.skipUnless(hasattr(posix, 'writev'), "test needs posix.writev()")
480 @requires_32b
481 def test_writev_overflow_32bits(self):
482 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
483 try:
484 with self.assertRaises(OSError) as cm:
485 os.writev(fd, [b"x" * 2**16] * 2**15)
486 self.assertEqual(cm.exception.errno, errno.EINVAL)
487 finally:
488 os.close(fd)
489
Ross Lagerwall7807c352011-03-17 20:20:30 +0200490 @unittest.skipUnless(hasattr(posix, 'readv'), "test needs posix.readv()")
491 def test_readv(self):
492 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
493 try:
494 os.write(fd, b'test1tt2t3')
495 os.lseek(fd, 0, os.SEEK_SET)
496 buf = [bytearray(i) for i in [5, 3, 2]]
497 self.assertEqual(posix.readv(fd, buf), 10)
498 self.assertEqual([b'test1', b'tt2', b't3'], [bytes(i) for i in buf])
Victor Stinner57ddf782014-01-08 15:21:28 +0100499
500 # Issue #20113: empty list of buffers should not crash
Victor Stinnercd5ca6a2014-01-08 16:01:31 +0100501 try:
502 size = posix.readv(fd, [])
503 except OSError:
504 # readv(fd, []) raises OSError(22, "Invalid argument")
505 # on OpenIndiana
506 pass
507 else:
508 self.assertEqual(size, 0)
Ross Lagerwall7807c352011-03-17 20:20:30 +0200509 finally:
510 os.close(fd)
511
Miss Islington (bot)3e4b6882018-07-31 02:20:06 -0700512 @unittest.skipUnless(hasattr(posix, 'readv'), "test needs posix.readv()")
513 @requires_32b
514 def test_readv_overflow_32bits(self):
515 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
516 try:
517 buf = [bytearray(2**16)] * 2**15
518 with self.assertRaises(OSError) as cm:
519 os.readv(fd, buf)
520 self.assertEqual(cm.exception.errno, errno.EINVAL)
521 self.assertEqual(bytes(buf[0]), b'\0'* 2**16)
522 finally:
523 os.close(fd)
524
Serhiy Storchaka43767632013-11-03 21:31:38 +0200525 @unittest.skipUnless(hasattr(posix, 'dup'),
526 'test needs posix.dup()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000527 def test_dup(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200528 fp = open(support.TESTFN)
529 try:
530 fd = posix.dup(fp.fileno())
531 self.assertIsInstance(fd, int)
532 os.close(fd)
533 finally:
534 fp.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000535
Serhiy Storchaka43767632013-11-03 21:31:38 +0200536 @unittest.skipUnless(hasattr(posix, 'confstr'),
537 'test needs posix.confstr()')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000538 def test_confstr(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200539 self.assertRaises(ValueError, posix.confstr, "CS_garbage")
540 self.assertEqual(len(posix.confstr("CS_PATH")) > 0, True)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000541
Serhiy Storchaka43767632013-11-03 21:31:38 +0200542 @unittest.skipUnless(hasattr(posix, 'dup2'),
543 'test needs posix.dup2()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000544 def test_dup2(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200545 fp1 = open(support.TESTFN)
546 fp2 = open(support.TESTFN)
547 try:
548 posix.dup2(fp1.fileno(), fp2.fileno())
549 finally:
550 fp1.close()
551 fp2.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000552
Charles-François Natali1e045b12011-05-22 20:42:32 +0200553 @unittest.skipUnless(hasattr(os, 'O_CLOEXEC'), "needs os.O_CLOEXEC")
Charles-François Natali239bb962011-06-03 12:55:15 +0200554 @support.requires_linux_version(2, 6, 23)
Charles-François Natali1e045b12011-05-22 20:42:32 +0200555 def test_oscloexec(self):
556 fd = os.open(support.TESTFN, os.O_RDONLY|os.O_CLOEXEC)
557 self.addCleanup(os.close, fd)
Victor Stinner1db9e7b2014-07-29 22:32:47 +0200558 self.assertFalse(os.get_inheritable(fd))
Charles-François Natali1e045b12011-05-22 20:42:32 +0200559
Serhiy Storchaka43767632013-11-03 21:31:38 +0200560 @unittest.skipUnless(hasattr(posix, 'O_EXLOCK'),
561 'test needs posix.O_EXLOCK')
Skip Montanaro98470002005-06-17 01:14:49 +0000562 def test_osexlock(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200563 fd = os.open(support.TESTFN,
564 os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
565 self.assertRaises(OSError, os.open, support.TESTFN,
566 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
567 os.close(fd)
568
569 if hasattr(posix, "O_SHLOCK"):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000570 fd = os.open(support.TESTFN,
Serhiy Storchaka43767632013-11-03 21:31:38 +0200571 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000572 self.assertRaises(OSError, os.open, support.TESTFN,
Skip Montanaro98470002005-06-17 01:14:49 +0000573 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
574 os.close(fd)
575
Serhiy Storchaka43767632013-11-03 21:31:38 +0200576 @unittest.skipUnless(hasattr(posix, 'O_SHLOCK'),
577 'test needs posix.O_SHLOCK')
Skip Montanaro98470002005-06-17 01:14:49 +0000578 def test_osshlock(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200579 fd1 = os.open(support.TESTFN,
580 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
581 fd2 = os.open(support.TESTFN,
582 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
583 os.close(fd2)
584 os.close(fd1)
585
586 if hasattr(posix, "O_EXLOCK"):
587 fd = os.open(support.TESTFN,
Skip Montanaro98470002005-06-17 01:14:49 +0000588 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
Serhiy Storchaka43767632013-11-03 21:31:38 +0200589 self.assertRaises(OSError, os.open, support.TESTFN,
590 os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
591 os.close(fd)
Skip Montanaro98470002005-06-17 01:14:49 +0000592
Serhiy Storchaka43767632013-11-03 21:31:38 +0200593 @unittest.skipUnless(hasattr(posix, 'fstat'),
594 'test needs posix.fstat()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000595 def test_fstat(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200596 fp = open(support.TESTFN)
597 try:
598 self.assertTrue(posix.fstat(fp.fileno()))
599 self.assertTrue(posix.stat(fp.fileno()))
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200600
Serhiy Storchaka43767632013-11-03 21:31:38 +0200601 self.assertRaisesRegex(TypeError,
Brett Cannon3f9183b2016-08-26 14:44:48 -0700602 'should be string, bytes, os.PathLike or integer, not',
Serhiy Storchaka43767632013-11-03 21:31:38 +0200603 posix.stat, float(fp.fileno()))
604 finally:
605 fp.close()
Neal Norwitze241ce82003-02-17 18:17:05 +0000606
Serhiy Storchaka43767632013-11-03 21:31:38 +0200607 @unittest.skipUnless(hasattr(posix, 'stat'),
608 'test needs posix.stat()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000609 def test_stat(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200610 self.assertTrue(posix.stat(support.TESTFN))
611 self.assertTrue(posix.stat(os.fsencode(support.TESTFN)))
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200612
Serhiy Storchakad73c3182016-08-06 23:22:08 +0300613 self.assertWarnsRegex(DeprecationWarning,
Brett Cannon3f9183b2016-08-26 14:44:48 -0700614 'should be string, bytes, os.PathLike or integer, not',
Serhiy Storchakad73c3182016-08-06 23:22:08 +0300615 posix.stat, bytearray(os.fsencode(support.TESTFN)))
Serhiy Storchaka43767632013-11-03 21:31:38 +0200616 self.assertRaisesRegex(TypeError,
Brett Cannon3f9183b2016-08-26 14:44:48 -0700617 'should be string, bytes, os.PathLike or integer, not',
Serhiy Storchaka43767632013-11-03 21:31:38 +0200618 posix.stat, None)
619 self.assertRaisesRegex(TypeError,
Brett Cannon3f9183b2016-08-26 14:44:48 -0700620 'should be string, bytes, os.PathLike or integer, not',
Serhiy Storchaka43767632013-11-03 21:31:38 +0200621 posix.stat, list(support.TESTFN))
622 self.assertRaisesRegex(TypeError,
Brett Cannon3f9183b2016-08-26 14:44:48 -0700623 'should be string, bytes, os.PathLike or integer, not',
Serhiy Storchaka43767632013-11-03 21:31:38 +0200624 posix.stat, list(os.fsencode(support.TESTFN)))
Neal Norwitze241ce82003-02-17 18:17:05 +0000625
Benjamin Peterson052a02b2010-08-17 01:27:09 +0000626 @unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()")
627 def test_mkfifo(self):
628 support.unlink(support.TESTFN)
xdegaye92c2ca72017-11-12 17:31:07 +0100629 try:
630 posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR)
631 except PermissionError as e:
632 self.skipTest('posix.mkfifo(): %s' % e)
Benjamin Peterson052a02b2010-08-17 01:27:09 +0000633 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
634
635 @unittest.skipUnless(hasattr(posix, 'mknod') and hasattr(stat, 'S_IFIFO'),
636 "don't have mknod()/S_IFIFO")
637 def test_mknod(self):
638 # Test using mknod() to create a FIFO (the only use specified
639 # by POSIX).
640 support.unlink(support.TESTFN)
641 mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR
642 try:
643 posix.mknod(support.TESTFN, mode, 0)
644 except OSError as e:
645 # Some old systems don't allow unprivileged users to use
646 # mknod(), or only support creating device nodes.
xdegaye92c2ca72017-11-12 17:31:07 +0100647 self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES))
Benjamin Peterson052a02b2010-08-17 01:27:09 +0000648 else:
649 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
650
Martin Panterbf19d162015-09-09 01:01:13 +0000651 # Keyword arguments are also supported
652 support.unlink(support.TESTFN)
653 try:
654 posix.mknod(path=support.TESTFN, mode=mode, device=0,
655 dir_fd=None)
656 except OSError as e:
xdegaye92c2ca72017-11-12 17:31:07 +0100657 self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES))
Martin Panterbf19d162015-09-09 01:01:13 +0000658
Serhiy Storchaka16b2e4f2015-04-20 09:22:13 +0300659 @unittest.skipUnless(hasattr(posix, 'stat'), 'test needs posix.stat()')
660 @unittest.skipUnless(hasattr(posix, 'makedev'), 'test needs posix.makedev()')
661 def test_makedev(self):
662 st = posix.stat(support.TESTFN)
663 dev = st.st_dev
664 self.assertIsInstance(dev, int)
665 self.assertGreaterEqual(dev, 0)
666
667 major = posix.major(dev)
668 self.assertIsInstance(major, int)
669 self.assertGreaterEqual(major, 0)
670 self.assertEqual(posix.major(dev), major)
671 self.assertRaises(TypeError, posix.major, float(dev))
672 self.assertRaises(TypeError, posix.major)
673 self.assertRaises((ValueError, OverflowError), posix.major, -1)
674
675 minor = posix.minor(dev)
676 self.assertIsInstance(minor, int)
677 self.assertGreaterEqual(minor, 0)
678 self.assertEqual(posix.minor(dev), minor)
679 self.assertRaises(TypeError, posix.minor, float(dev))
680 self.assertRaises(TypeError, posix.minor)
681 self.assertRaises((ValueError, OverflowError), posix.minor, -1)
682
Victor Stinner13ff2452018-01-22 18:32:50 +0100683 # FIXME: reenable these tests on FreeBSD with the kernel fix
Victor Stinner12953ff2017-07-27 16:55:54 +0200684 if sys.platform.startswith('freebsd') and dev >= 0x1_0000_0000:
685 self.skipTest("bpo-31044: on FreeBSD CURRENT, minor() truncates "
686 "64-bit dev to 32-bit")
687
Serhiy Storchaka16b2e4f2015-04-20 09:22:13 +0300688 self.assertEqual(posix.makedev(major, minor), dev)
689 self.assertRaises(TypeError, posix.makedev, float(major), minor)
690 self.assertRaises(TypeError, posix.makedev, major, float(minor))
691 self.assertRaises(TypeError, posix.makedev, major)
692 self.assertRaises(TypeError, posix.makedev)
693
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200694 def _test_all_chown_common(self, chown_func, first_param, stat_func):
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000695 """Common code for chown, fchown and lchown tests."""
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200696 def check_stat(uid, gid):
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200697 if stat_func is not None:
698 stat = stat_func(first_param)
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200699 self.assertEqual(stat.st_uid, uid)
700 self.assertEqual(stat.st_gid, gid)
701 uid = os.getuid()
702 gid = os.getgid()
Charles-François Nataliab2d58e2012-04-17 19:48:35 +0200703 # test a successful chown call
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200704 chown_func(first_param, uid, gid)
705 check_stat(uid, gid)
706 chown_func(first_param, -1, gid)
707 check_stat(uid, gid)
708 chown_func(first_param, uid, -1)
709 check_stat(uid, gid)
Charles-François Nataliab2d58e2012-04-17 19:48:35 +0200710
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200711 if uid == 0:
712 # Try an amusingly large uid/gid to make sure we handle
713 # large unsigned values. (chown lets you use any
714 # uid/gid you like, even if they aren't defined.)
715 #
716 # This problem keeps coming up:
717 # http://bugs.python.org/issue1747858
718 # http://bugs.python.org/issue4591
719 # http://bugs.python.org/issue15301
720 # Hopefully the fix in 4591 fixes it for good!
721 #
722 # This part of the test only runs when run as root.
723 # Only scary people run their tests as root.
724
725 big_value = 2**31
726 chown_func(first_param, big_value, big_value)
727 check_stat(big_value, big_value)
728 chown_func(first_param, -1, -1)
729 check_stat(big_value, big_value)
730 chown_func(first_param, uid, gid)
731 check_stat(uid, gid)
Charles-François Nataliab2d58e2012-04-17 19:48:35 +0200732 elif platform.system() in ('HP-UX', 'SunOS'):
733 # HP-UX and Solaris can allow a non-root user to chown() to root
734 # (issue #5113)
735 raise unittest.SkipTest("Skipping because of non-standard chown() "
736 "behavior")
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000737 else:
738 # non-root cannot chown to root, raises OSError
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200739 self.assertRaises(OSError, chown_func, first_param, 0, 0)
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200740 check_stat(uid, gid)
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200741 self.assertRaises(OSError, chown_func, first_param, 0, -1)
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200742 check_stat(uid, gid)
Serhiy Storchakaa2964b32013-02-21 14:34:36 +0200743 if 0 not in os.getgroups():
Serhiy Storchakab3d62ce2013-02-20 19:48:22 +0200744 self.assertRaises(OSError, chown_func, first_param, -1, 0)
745 check_stat(uid, gid)
Serhiy Storchaka54db2fd2013-02-20 19:40:25 +0200746 # test illegal types
747 for t in str, float:
748 self.assertRaises(TypeError, chown_func, first_param, t(uid), gid)
749 check_stat(uid, gid)
750 self.assertRaises(TypeError, chown_func, first_param, uid, t(gid))
751 check_stat(uid, gid)
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000752
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000753 @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()")
754 def test_chown(self):
755 # raise an OSError if the file does not exist
756 os.unlink(support.TESTFN)
757 self.assertRaises(OSError, posix.chown, support.TESTFN, -1, -1)
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000758
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000759 # re-create the file
Victor Stinnerbf816222011-06-30 23:25:47 +0200760 support.create_empty_file(support.TESTFN)
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200761 self._test_all_chown_common(posix.chown, support.TESTFN,
762 getattr(posix, 'stat', None))
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000763
764 @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()")
765 def test_fchown(self):
766 os.unlink(support.TESTFN)
767
768 # re-create the file
769 test_file = open(support.TESTFN, 'w')
770 try:
771 fd = test_file.fileno()
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200772 self._test_all_chown_common(posix.fchown, fd,
773 getattr(posix, 'fstat', None))
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000774 finally:
775 test_file.close()
776
777 @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()")
778 def test_lchown(self):
779 os.unlink(support.TESTFN)
780 # create a symlink
Ned Deily3eb67d52011-06-28 00:00:28 -0700781 os.symlink(_DUMMY_SYMLINK, support.TESTFN)
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200782 self._test_all_chown_common(posix.lchown, support.TESTFN,
783 getattr(posix, 'lstat', None))
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000784
Serhiy Storchaka43767632013-11-03 21:31:38 +0200785 @unittest.skipUnless(hasattr(posix, 'chdir'), 'test needs posix.chdir()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000786 def test_chdir(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200787 posix.chdir(os.curdir)
788 self.assertRaises(OSError, posix.chdir, support.TESTFN)
Neal Norwitze241ce82003-02-17 18:17:05 +0000789
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +0000790 def test_listdir(self):
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +0300791 self.assertIn(support.TESTFN, posix.listdir(os.curdir))
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +0000792
793 def test_listdir_default(self):
Larry Hastingsfdaea062012-06-25 04:42:23 -0700794 # When listdir is called without argument,
795 # it's the same as listdir(os.curdir).
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +0300796 self.assertIn(support.TESTFN, posix.listdir())
Neal Norwitze241ce82003-02-17 18:17:05 +0000797
Larry Hastingsfdaea062012-06-25 04:42:23 -0700798 def test_listdir_bytes(self):
799 # When listdir is called with a bytes object,
800 # the returned strings are of type bytes.
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +0300801 self.assertIn(os.fsencode(support.TESTFN), posix.listdir(b'.'))
802
803 def test_listdir_bytes_like(self):
804 for cls in bytearray, memoryview:
805 with self.assertWarns(DeprecationWarning):
806 names = posix.listdir(cls(b'.'))
807 self.assertIn(os.fsencode(support.TESTFN), names)
808 for name in names:
809 self.assertIs(type(name), bytes)
Larry Hastingsfdaea062012-06-25 04:42:23 -0700810
811 @unittest.skipUnless(posix.listdir in os.supports_fd,
812 "test needs fd support for posix.listdir()")
813 def test_listdir_fd(self):
Antoine Pitrou8250e232011-02-25 23:41:16 +0000814 f = posix.open(posix.getcwd(), posix.O_RDONLY)
Charles-François Natali7546ad32012-01-08 18:34:06 +0100815 self.addCleanup(posix.close, f)
Antoine Pitrou8250e232011-02-25 23:41:16 +0000816 self.assertEqual(
817 sorted(posix.listdir('.')),
Larry Hastings9cf065c2012-06-22 16:30:09 -0700818 sorted(posix.listdir(f))
Antoine Pitrou8250e232011-02-25 23:41:16 +0000819 )
Charles-François Natali7546ad32012-01-08 18:34:06 +0100820 # Check that the fd offset was reset (issue #13739)
Charles-François Natali7546ad32012-01-08 18:34:06 +0100821 self.assertEqual(
822 sorted(posix.listdir('.')),
Larry Hastings9cf065c2012-06-22 16:30:09 -0700823 sorted(posix.listdir(f))
Charles-François Natali7546ad32012-01-08 18:34:06 +0100824 )
Antoine Pitrou8250e232011-02-25 23:41:16 +0000825
Serhiy Storchaka43767632013-11-03 21:31:38 +0200826 @unittest.skipUnless(hasattr(posix, 'access'), 'test needs posix.access()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000827 def test_access(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200828 self.assertTrue(posix.access(support.TESTFN, os.R_OK))
Neal Norwitze241ce82003-02-17 18:17:05 +0000829
Serhiy Storchaka43767632013-11-03 21:31:38 +0200830 @unittest.skipUnless(hasattr(posix, 'umask'), 'test needs posix.umask()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000831 def test_umask(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200832 old_mask = posix.umask(0)
833 self.assertIsInstance(old_mask, int)
834 posix.umask(old_mask)
Neal Norwitze241ce82003-02-17 18:17:05 +0000835
Serhiy Storchaka43767632013-11-03 21:31:38 +0200836 @unittest.skipUnless(hasattr(posix, 'strerror'),
837 'test needs posix.strerror()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000838 def test_strerror(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200839 self.assertTrue(posix.strerror(0))
Neal Norwitze241ce82003-02-17 18:17:05 +0000840
Serhiy Storchaka43767632013-11-03 21:31:38 +0200841 @unittest.skipUnless(hasattr(posix, 'pipe'), 'test needs posix.pipe()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000842 def test_pipe(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200843 reader, writer = posix.pipe()
844 os.close(reader)
845 os.close(writer)
Neal Norwitze241ce82003-02-17 18:17:05 +0000846
Charles-François Natalidaafdd52011-05-29 20:07:40 +0200847 @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()")
Charles-François Natali239bb962011-06-03 12:55:15 +0200848 @support.requires_linux_version(2, 6, 27)
Charles-François Natalidaafdd52011-05-29 20:07:40 +0200849 def test_pipe2(self):
850 self.assertRaises(TypeError, os.pipe2, 'DEADBEEF')
851 self.assertRaises(TypeError, os.pipe2, 0, 0)
852
Charles-François Natali368f34b2011-06-06 19:49:47 +0200853 # try calling with flags = 0, like os.pipe()
854 r, w = os.pipe2(0)
Charles-François Natalidaafdd52011-05-29 20:07:40 +0200855 os.close(r)
856 os.close(w)
857
858 # test flags
859 r, w = os.pipe2(os.O_CLOEXEC|os.O_NONBLOCK)
860 self.addCleanup(os.close, r)
861 self.addCleanup(os.close, w)
Victor Stinnerbff989e2013-08-28 12:25:40 +0200862 self.assertFalse(os.get_inheritable(r))
863 self.assertFalse(os.get_inheritable(w))
Victor Stinner1db9e7b2014-07-29 22:32:47 +0200864 self.assertFalse(os.get_blocking(r))
865 self.assertFalse(os.get_blocking(w))
Charles-François Natalidaafdd52011-05-29 20:07:40 +0200866 # try reading from an empty pipe: this should fail, not block
867 self.assertRaises(OSError, os.read, r, 1)
868 # try a write big enough to fill-up the pipe: this should either
869 # fail or perform a partial write, not block
870 try:
871 os.write(w, b'x' * support.PIPE_MAX_SIZE)
872 except OSError:
873 pass
874
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200875 @support.cpython_only
876 @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()")
877 @support.requires_linux_version(2, 6, 27)
878 def test_pipe2_c_limits(self):
Serhiy Storchaka78980432013-01-15 01:12:17 +0200879 # Issue 15989
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200880 import _testcapi
Serhiy Storchaka78980432013-01-15 01:12:17 +0200881 self.assertRaises(OverflowError, os.pipe2, _testcapi.INT_MAX + 1)
882 self.assertRaises(OverflowError, os.pipe2, _testcapi.UINT_MAX + 1)
883
Serhiy Storchaka43767632013-11-03 21:31:38 +0200884 @unittest.skipUnless(hasattr(posix, 'utime'), 'test needs posix.utime()')
Neal Norwitze241ce82003-02-17 18:17:05 +0000885 def test_utime(self):
Serhiy Storchaka43767632013-11-03 21:31:38 +0200886 now = time.time()
887 posix.utime(support.TESTFN, None)
888 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None))
889 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None))
890 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now))
891 posix.utime(support.TESTFN, (int(now), int(now)))
892 posix.utime(support.TESTFN, (now, now))
Neal Norwitze241ce82003-02-17 18:17:05 +0000893
Larry Hastings9cf065c2012-06-22 16:30:09 -0700894 def _test_chflags_regular_file(self, chflags_func, target_file, **kwargs):
Ned Deily3eb67d52011-06-28 00:00:28 -0700895 st = os.stat(target_file)
896 self.assertTrue(hasattr(st, 'st_flags'))
Trent Nelson75959cf2012-08-21 23:59:31 +0000897
898 # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE.
899 flags = st.st_flags | stat.UF_IMMUTABLE
900 try:
901 chflags_func(target_file, flags, **kwargs)
902 except OSError as err:
903 if err.errno != errno.EOPNOTSUPP:
904 raise
905 msg = 'chflag UF_IMMUTABLE not supported by underlying fs'
906 self.skipTest(msg)
907
Ned Deily3eb67d52011-06-28 00:00:28 -0700908 try:
909 new_st = os.stat(target_file)
910 self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags)
911 try:
912 fd = open(target_file, 'w+')
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200913 except OSError as e:
Ned Deily3eb67d52011-06-28 00:00:28 -0700914 self.assertEqual(e.errno, errno.EPERM)
915 finally:
916 posix.chflags(target_file, st.st_flags)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000917
Ned Deily3eb67d52011-06-28 00:00:28 -0700918 @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()')
919 def test_chflags(self):
920 self._test_chflags_regular_file(posix.chflags, support.TESTFN)
921
922 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
923 def test_lchflags_regular_file(self):
924 self._test_chflags_regular_file(posix.lchflags, support.TESTFN)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700925 self._test_chflags_regular_file(posix.chflags, support.TESTFN, follow_symlinks=False)
Ned Deily3eb67d52011-06-28 00:00:28 -0700926
927 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
928 def test_lchflags_symlink(self):
929 testfn_st = os.stat(support.TESTFN)
930
931 self.assertTrue(hasattr(testfn_st, 'st_flags'))
932
933 os.symlink(support.TESTFN, _DUMMY_SYMLINK)
934 self.teardown_files.append(_DUMMY_SYMLINK)
935 dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
936
Larry Hastings9cf065c2012-06-22 16:30:09 -0700937 def chflags_nofollow(path, flags):
938 return posix.chflags(path, flags, follow_symlinks=False)
Ned Deily3eb67d52011-06-28 00:00:28 -0700939
Larry Hastings9cf065c2012-06-22 16:30:09 -0700940 for fn in (posix.lchflags, chflags_nofollow):
Trent Nelson75959cf2012-08-21 23:59:31 +0000941 # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE.
942 flags = dummy_symlink_st.st_flags | stat.UF_IMMUTABLE
943 try:
944 fn(_DUMMY_SYMLINK, flags)
945 except OSError as err:
946 if err.errno != errno.EOPNOTSUPP:
947 raise
948 msg = 'chflag UF_IMMUTABLE not supported by underlying fs'
949 self.skipTest(msg)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700950 try:
951 new_testfn_st = os.stat(support.TESTFN)
952 new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
953
954 self.assertEqual(testfn_st.st_flags, new_testfn_st.st_flags)
955 self.assertEqual(dummy_symlink_st.st_flags | stat.UF_IMMUTABLE,
956 new_dummy_symlink_st.st_flags)
957 finally:
958 fn(_DUMMY_SYMLINK, dummy_symlink_st.st_flags)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000959
Guido van Rossum98297ee2007-11-06 21:34:58 +0000960 def test_environ(self):
Victor Stinner17b490d2010-05-06 22:19:30 +0000961 if os.name == "nt":
962 item_type = str
963 else:
964 item_type = bytes
Guido van Rossum98297ee2007-11-06 21:34:58 +0000965 for k, v in posix.environ.items():
Victor Stinner17b490d2010-05-06 22:19:30 +0000966 self.assertEqual(type(k), item_type)
967 self.assertEqual(type(v), item_type)
Guido van Rossum98297ee2007-11-06 21:34:58 +0000968
Serhiy Storchaka77703942017-06-25 07:33:01 +0300969 @unittest.skipUnless(hasattr(os, "putenv"), "requires os.putenv()")
970 def test_putenv(self):
971 with self.assertRaises(ValueError):
972 os.putenv('FRUIT\0VEGETABLE', 'cabbage')
973 with self.assertRaises(ValueError):
974 os.putenv(b'FRUIT\0VEGETABLE', b'cabbage')
975 with self.assertRaises(ValueError):
976 os.putenv('FRUIT', 'orange\0VEGETABLE=cabbage')
977 with self.assertRaises(ValueError):
978 os.putenv(b'FRUIT', b'orange\0VEGETABLE=cabbage')
979 with self.assertRaises(ValueError):
980 os.putenv('FRUIT=ORANGE', 'lemon')
981 with self.assertRaises(ValueError):
982 os.putenv(b'FRUIT=ORANGE', b'lemon')
983
Serhiy Storchaka43767632013-11-03 21:31:38 +0200984 @unittest.skipUnless(hasattr(posix, 'getcwd'), 'test needs posix.getcwd()')
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000985 def test_getcwd_long_pathnames(self):
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -0500986 dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
987 curdir = os.getcwd()
988 base_path = os.path.abspath(support.TESTFN) + '.getcwd'
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000989
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -0500990 try:
991 os.mkdir(base_path)
992 os.chdir(base_path)
993 except:
994 # Just returning nothing instead of the SkipTest exception, because
995 # the test results in Error in that case. Is that ok?
996 # raise unittest.SkipTest("cannot create directory for testing")
997 return
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000998
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -0500999 def _create_and_do_getcwd(dirname, current_path_length = 0):
1000 try:
1001 os.mkdir(dirname)
1002 except:
1003 raise unittest.SkipTest("mkdir cannot create directory sufficiently deep for getcwd test")
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001004
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -05001005 os.chdir(dirname)
1006 try:
1007 os.getcwd()
1008 if current_path_length < 1027:
1009 _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
1010 finally:
1011 os.chdir('..')
1012 os.rmdir(dirname)
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001013
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -05001014 _create_and_do_getcwd(dirname)
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001015
Benjamin Peterson3a7dffa2013-08-23 21:01:48 -05001016 finally:
1017 os.chdir(curdir)
1018 support.rmtree(base_path)
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001019
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02001020 @unittest.skipUnless(hasattr(posix, 'getgrouplist'), "test needs posix.getgrouplist()")
1021 @unittest.skipUnless(hasattr(pwd, 'getpwuid'), "test needs pwd.getpwuid()")
1022 @unittest.skipUnless(hasattr(os, 'getuid'), "test needs os.getuid()")
1023 def test_getgrouplist(self):
Ross Lagerwalla0b315f2012-12-13 15:20:26 +00001024 user = pwd.getpwuid(os.getuid())[0]
1025 group = pwd.getpwuid(os.getuid())[3]
1026 self.assertIn(group, posix.getgrouplist(user, group))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02001027
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02001028
Antoine Pitrou318b8f32011-01-12 18:45:27 +00001029 @unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()")
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001030 def test_getgroups(self):
Jesus Cea61f32cb2014-06-28 18:39:35 +02001031 with os.popen('id -G 2>/dev/null') as idg:
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001032 groups = idg.read().strip()
Charles-François Natalie8a255a2012-05-02 20:01:38 +02001033 ret = idg.close()
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001034
Xavier de Gaye24c3b492016-10-19 11:00:26 +02001035 try:
1036 idg_groups = set(int(g) for g in groups.split())
1037 except ValueError:
1038 idg_groups = set()
1039 if ret is not None or not idg_groups:
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001040 raise unittest.SkipTest("need working 'id -G'")
1041
Ned Deily028915e2013-02-02 15:08:52 -08001042 # Issues 16698: OS X ABIs prior to 10.6 have limits on getgroups()
1043 if sys.platform == 'darwin':
1044 import sysconfig
1045 dt = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') or '10.0'
Ned Deily04cdfa12014-06-25 13:36:14 -07001046 if tuple(int(n) for n in dt.split('.')[0:2]) < (10, 6):
Ned Deily028915e2013-02-02 15:08:52 -08001047 raise unittest.SkipTest("getgroups(2) is broken prior to 10.6")
1048
Ronald Oussoren7fb6f512010-08-01 19:18:13 +00001049 # 'id -G' and 'os.getgroups()' should return the same
Xavier de Gaye24c3b492016-10-19 11:00:26 +02001050 # groups, ignoring order, duplicates, and the effective gid.
1051 # #10822/#26944 - It is implementation defined whether
1052 # posix.getgroups() includes the effective gid.
1053 symdiff = idg_groups.symmetric_difference(posix.getgroups())
1054 self.assertTrue(not symdiff or symdiff == {posix.getegid()})
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001055
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001056 # tests for the posix *at functions follow
1057
Larry Hastings9cf065c2012-06-22 16:30:09 -07001058 @unittest.skipUnless(os.access in os.supports_dir_fd, "test needs dir_fd support for os.access()")
1059 def test_access_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001060 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1061 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001062 self.assertTrue(posix.access(support.TESTFN, os.R_OK, dir_fd=f))
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001063 finally:
1064 posix.close(f)
1065
Larry Hastings9cf065c2012-06-22 16:30:09 -07001066 @unittest.skipUnless(os.chmod in os.supports_dir_fd, "test needs dir_fd support in os.chmod()")
1067 def test_chmod_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001068 os.chmod(support.TESTFN, stat.S_IRUSR)
1069
1070 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1071 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001072 posix.chmod(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR, dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001073
1074 s = posix.stat(support.TESTFN)
1075 self.assertEqual(s[0] & stat.S_IRWXU, stat.S_IRUSR | stat.S_IWUSR)
1076 finally:
1077 posix.close(f)
1078
Larry Hastings9cf065c2012-06-22 16:30:09 -07001079 @unittest.skipUnless(os.chown in os.supports_dir_fd, "test needs dir_fd support in os.chown()")
1080 def test_chown_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001081 support.unlink(support.TESTFN)
Victor Stinnerbf816222011-06-30 23:25:47 +02001082 support.create_empty_file(support.TESTFN)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001083
1084 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1085 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001086 posix.chown(support.TESTFN, os.getuid(), os.getgid(), dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001087 finally:
1088 posix.close(f)
1089
Larry Hastings9cf065c2012-06-22 16:30:09 -07001090 @unittest.skipUnless(os.stat in os.supports_dir_fd, "test needs dir_fd support in os.stat()")
1091 def test_stat_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001092 support.unlink(support.TESTFN)
1093 with open(support.TESTFN, 'w') as outfile:
1094 outfile.write("testline\n")
1095
1096 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1097 try:
1098 s1 = posix.stat(support.TESTFN)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001099 s2 = posix.stat(support.TESTFN, dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001100 self.assertEqual(s1, s2)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001101 s2 = posix.stat(support.TESTFN, dir_fd=None)
1102 self.assertEqual(s1, s2)
Serhiy Storchaka7155b882016-04-08 08:48:20 +03001103 self.assertRaisesRegex(TypeError, 'should be integer or None, not',
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001104 posix.stat, support.TESTFN, dir_fd=posix.getcwd())
Serhiy Storchaka7155b882016-04-08 08:48:20 +03001105 self.assertRaisesRegex(TypeError, 'should be integer or None, not',
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001106 posix.stat, support.TESTFN, dir_fd=float(f))
1107 self.assertRaises(OverflowError,
1108 posix.stat, support.TESTFN, dir_fd=10**20)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001109 finally:
1110 posix.close(f)
1111
Larry Hastings9cf065c2012-06-22 16:30:09 -07001112 @unittest.skipUnless(os.utime in os.supports_dir_fd, "test needs dir_fd support in os.utime()")
1113 def test_utime_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001114 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1115 try:
1116 now = time.time()
Larry Hastings9cf065c2012-06-22 16:30:09 -07001117 posix.utime(support.TESTFN, None, dir_fd=f)
1118 posix.utime(support.TESTFN, dir_fd=f)
1119 self.assertRaises(TypeError, posix.utime, support.TESTFN, now, dir_fd=f)
1120 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None), dir_fd=f)
1121 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None), dir_fd=f)
1122 self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now), dir_fd=f)
1123 self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, "x"), dir_fd=f)
1124 posix.utime(support.TESTFN, (int(now), int(now)), dir_fd=f)
1125 posix.utime(support.TESTFN, (now, now), dir_fd=f)
1126 posix.utime(support.TESTFN,
1127 (int(now), int((now - int(now)) * 1e9)), dir_fd=f)
1128 posix.utime(support.TESTFN, dir_fd=f,
1129 times=(int(now), int((now - int(now)) * 1e9)))
1130
Larry Hastings90867a52012-06-22 17:01:41 -07001131 # try dir_fd and follow_symlinks together
Larry Hastings9cf065c2012-06-22 16:30:09 -07001132 if os.utime in os.supports_follow_symlinks:
Larry Hastings90867a52012-06-22 17:01:41 -07001133 try:
1134 posix.utime(support.TESTFN, follow_symlinks=False, dir_fd=f)
Georg Brandl969288e2012-06-26 09:25:44 +02001135 except ValueError:
Larry Hastings90867a52012-06-22 17:01:41 -07001136 # whoops! using both together not supported on this platform.
1137 pass
Larry Hastings9cf065c2012-06-22 16:30:09 -07001138
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001139 finally:
1140 posix.close(f)
1141
Larry Hastings9cf065c2012-06-22 16:30:09 -07001142 @unittest.skipUnless(os.link in os.supports_dir_fd, "test needs dir_fd support in os.link()")
1143 def test_link_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001144 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1145 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001146 posix.link(support.TESTFN, support.TESTFN + 'link', src_dir_fd=f, dst_dir_fd=f)
xdegaye92c2ca72017-11-12 17:31:07 +01001147 except PermissionError as e:
1148 self.skipTest('posix.link(): %s' % e)
1149 else:
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001150 # should have same inodes
1151 self.assertEqual(posix.stat(support.TESTFN)[1],
1152 posix.stat(support.TESTFN + 'link')[1])
1153 finally:
1154 posix.close(f)
1155 support.unlink(support.TESTFN + 'link')
1156
Larry Hastings9cf065c2012-06-22 16:30:09 -07001157 @unittest.skipUnless(os.mkdir in os.supports_dir_fd, "test needs dir_fd support in os.mkdir()")
1158 def test_mkdir_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001159 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1160 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001161 posix.mkdir(support.TESTFN + 'dir', dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001162 posix.stat(support.TESTFN + 'dir') # should not raise exception
1163 finally:
1164 posix.close(f)
1165 support.rmtree(support.TESTFN + 'dir')
1166
Larry Hastings9cf065c2012-06-22 16:30:09 -07001167 @unittest.skipUnless((os.mknod in os.supports_dir_fd) and hasattr(stat, 'S_IFIFO'),
1168 "test requires both stat.S_IFIFO and dir_fd support for os.mknod()")
1169 def test_mknod_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001170 # Test using mknodat() to create a FIFO (the only use specified
1171 # by POSIX).
1172 support.unlink(support.TESTFN)
1173 mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR
1174 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1175 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001176 posix.mknod(support.TESTFN, mode, 0, dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001177 except OSError as e:
1178 # Some old systems don't allow unprivileged users to use
1179 # mknod(), or only support creating device nodes.
xdegaye92c2ca72017-11-12 17:31:07 +01001180 self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES))
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001181 else:
1182 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
1183 finally:
1184 posix.close(f)
1185
Larry Hastings9cf065c2012-06-22 16:30:09 -07001186 @unittest.skipUnless(os.open in os.supports_dir_fd, "test needs dir_fd support in os.open()")
1187 def test_open_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001188 support.unlink(support.TESTFN)
1189 with open(support.TESTFN, 'w') as outfile:
1190 outfile.write("testline\n")
1191 a = posix.open(posix.getcwd(), posix.O_RDONLY)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001192 b = posix.open(support.TESTFN, posix.O_RDONLY, dir_fd=a)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001193 try:
1194 res = posix.read(b, 9).decode(encoding="utf-8")
1195 self.assertEqual("testline\n", res)
1196 finally:
1197 posix.close(a)
1198 posix.close(b)
1199
Larry Hastings9cf065c2012-06-22 16:30:09 -07001200 @unittest.skipUnless(os.readlink in os.supports_dir_fd, "test needs dir_fd support in os.readlink()")
1201 def test_readlink_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001202 os.symlink(support.TESTFN, support.TESTFN + 'link')
1203 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1204 try:
1205 self.assertEqual(posix.readlink(support.TESTFN + 'link'),
Larry Hastings9cf065c2012-06-22 16:30:09 -07001206 posix.readlink(support.TESTFN + 'link', dir_fd=f))
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001207 finally:
1208 support.unlink(support.TESTFN + 'link')
1209 posix.close(f)
1210
Larry Hastings9cf065c2012-06-22 16:30:09 -07001211 @unittest.skipUnless(os.rename in os.supports_dir_fd, "test needs dir_fd support in os.rename()")
1212 def test_rename_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001213 support.unlink(support.TESTFN)
Victor Stinnerbf816222011-06-30 23:25:47 +02001214 support.create_empty_file(support.TESTFN + 'ren')
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001215 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1216 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001217 posix.rename(support.TESTFN + 'ren', support.TESTFN, src_dir_fd=f, dst_dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001218 except:
1219 posix.rename(support.TESTFN + 'ren', support.TESTFN)
1220 raise
1221 else:
Andrew Svetlov5b898402012-12-18 21:26:36 +02001222 posix.stat(support.TESTFN) # should not raise exception
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001223 finally:
1224 posix.close(f)
1225
Larry Hastings9cf065c2012-06-22 16:30:09 -07001226 @unittest.skipUnless(os.symlink in os.supports_dir_fd, "test needs dir_fd support in os.symlink()")
1227 def test_symlink_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001228 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1229 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001230 posix.symlink(support.TESTFN, support.TESTFN + 'link', dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001231 self.assertEqual(posix.readlink(support.TESTFN + 'link'), support.TESTFN)
1232 finally:
1233 posix.close(f)
1234 support.unlink(support.TESTFN + 'link')
1235
Larry Hastings9cf065c2012-06-22 16:30:09 -07001236 @unittest.skipUnless(os.unlink in os.supports_dir_fd, "test needs dir_fd support in os.unlink()")
1237 def test_unlink_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001238 f = posix.open(posix.getcwd(), posix.O_RDONLY)
Victor Stinnerbf816222011-06-30 23:25:47 +02001239 support.create_empty_file(support.TESTFN + 'del')
Andrew Svetlov5b898402012-12-18 21:26:36 +02001240 posix.stat(support.TESTFN + 'del') # should not raise exception
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001241 try:
Larry Hastings9cf065c2012-06-22 16:30:09 -07001242 posix.unlink(support.TESTFN + 'del', dir_fd=f)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001243 except:
1244 support.unlink(support.TESTFN + 'del')
1245 raise
1246 else:
1247 self.assertRaises(OSError, posix.stat, support.TESTFN + 'link')
1248 finally:
1249 posix.close(f)
1250
Larry Hastings9cf065c2012-06-22 16:30:09 -07001251 @unittest.skipUnless(os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()")
1252 def test_mkfifo_dir_fd(self):
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001253 support.unlink(support.TESTFN)
1254 f = posix.open(posix.getcwd(), posix.O_RDONLY)
1255 try:
xdegaye92c2ca72017-11-12 17:31:07 +01001256 try:
1257 posix.mkfifo(support.TESTFN,
1258 stat.S_IRUSR | stat.S_IWUSR, dir_fd=f)
1259 except PermissionError as e:
1260 self.skipTest('posix.mkfifo(): %s' % e)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00001261 self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
1262 finally:
1263 posix.close(f)
1264
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001265 requires_sched_h = unittest.skipUnless(hasattr(posix, 'sched_yield'),
1266 "don't have scheduling support")
Antoine Pitrou84869872012-08-04 16:16:35 +02001267 requires_sched_affinity = unittest.skipUnless(hasattr(posix, 'sched_setaffinity'),
Benjamin Peterson50ba2712011-08-02 22:15:40 -05001268 "don't have sched affinity support")
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001269
1270 @requires_sched_h
1271 def test_sched_yield(self):
1272 # This has no error conditions (at least on Linux).
1273 posix.sched_yield()
1274
1275 @requires_sched_h
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02001276 @unittest.skipUnless(hasattr(posix, 'sched_get_priority_max'),
1277 "requires sched_get_priority_max()")
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001278 def test_sched_priority(self):
1279 # Round-robin usually has interesting priorities.
1280 pol = posix.SCHED_RR
1281 lo = posix.sched_get_priority_min(pol)
1282 hi = posix.sched_get_priority_max(pol)
1283 self.assertIsInstance(lo, int)
1284 self.assertIsInstance(hi, int)
1285 self.assertGreaterEqual(hi, lo)
Benjamin Peterson539b6c42011-08-02 22:09:37 -05001286 # OSX evidently just returns 15 without checking the argument.
1287 if sys.platform != "darwin":
Benjamin Petersonc1581582011-08-02 22:10:55 -05001288 self.assertRaises(OSError, posix.sched_get_priority_min, -23)
1289 self.assertRaises(OSError, posix.sched_get_priority_max, -23)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001290
Benjamin Peterson0aef9092018-09-12 16:00:06 -07001291 @requires_sched
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001292 def test_get_and_set_scheduler_and_param(self):
1293 possible_schedulers = [sched for name, sched in posix.__dict__.items()
1294 if name.startswith("SCHED_")]
1295 mine = posix.sched_getscheduler(0)
1296 self.assertIn(mine, possible_schedulers)
1297 try:
Jesus Ceaceb5d162011-09-10 01:16:55 +02001298 parent = posix.sched_getscheduler(os.getppid())
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001299 except OSError as e:
Jesus Ceaceb5d162011-09-10 01:16:55 +02001300 if e.errno != errno.EPERM:
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001301 raise
1302 else:
Jesus Ceaceb5d162011-09-10 01:16:55 +02001303 self.assertIn(parent, possible_schedulers)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001304 self.assertRaises(OSError, posix.sched_getscheduler, -1)
1305 self.assertRaises(OSError, posix.sched_getparam, -1)
1306 param = posix.sched_getparam(0)
1307 self.assertIsInstance(param.sched_priority, int)
Charles-François Natali7b911cb2011-08-21 12:41:43 +02001308
Charles-François Natalib402a5c2013-01-13 14:13:25 +01001309 # POSIX states that calling sched_setparam() or sched_setscheduler() on
1310 # a process with a scheduling policy other than SCHED_FIFO or SCHED_RR
1311 # is implementation-defined: NetBSD and FreeBSD can return EINVAL.
1312 if not sys.platform.startswith(('freebsd', 'netbsd')):
1313 try:
1314 posix.sched_setscheduler(0, mine, param)
1315 posix.sched_setparam(0, param)
1316 except OSError as e:
1317 if e.errno != errno.EPERM:
1318 raise
Charles-François Natali7b911cb2011-08-21 12:41:43 +02001319 self.assertRaises(OSError, posix.sched_setparam, -1, param)
1320
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001321 self.assertRaises(OSError, posix.sched_setscheduler, -1, mine, param)
1322 self.assertRaises(TypeError, posix.sched_setscheduler, 0, mine, None)
1323 self.assertRaises(TypeError, posix.sched_setparam, 0, 43)
1324 param = posix.sched_param(None)
1325 self.assertRaises(TypeError, posix.sched_setparam, 0, param)
1326 large = 214748364700
1327 param = posix.sched_param(large)
1328 self.assertRaises(OverflowError, posix.sched_setparam, 0, param)
1329 param = posix.sched_param(sched_priority=-large)
1330 self.assertRaises(OverflowError, posix.sched_setparam, 0, param)
1331
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05001332 @unittest.skipUnless(hasattr(posix, "sched_rr_get_interval"), "no function")
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001333 def test_sched_rr_get_interval(self):
Benjamin Peterson43234ab2011-08-02 22:19:14 -05001334 try:
1335 interval = posix.sched_rr_get_interval(0)
1336 except OSError as e:
1337 # This likely means that sched_rr_get_interval is only valid for
1338 # processes with the SCHED_RR scheduler in effect.
1339 if e.errno != errno.EINVAL:
1340 raise
1341 self.skipTest("only works on SCHED_RR processes")
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001342 self.assertIsInstance(interval, float)
1343 # Reasonable constraints, I think.
1344 self.assertGreaterEqual(interval, 0.)
1345 self.assertLess(interval, 1.)
1346
Benjamin Peterson2740af82011-08-02 17:41:34 -05001347 @requires_sched_affinity
Antoine Pitrou84869872012-08-04 16:16:35 +02001348 def test_sched_getaffinity(self):
1349 mask = posix.sched_getaffinity(0)
1350 self.assertIsInstance(mask, set)
1351 self.assertGreaterEqual(len(mask), 1)
1352 self.assertRaises(OSError, posix.sched_getaffinity, -1)
1353 for cpu in mask:
1354 self.assertIsInstance(cpu, int)
1355 self.assertGreaterEqual(cpu, 0)
1356 self.assertLess(cpu, 1 << 32)
1357
1358 @requires_sched_affinity
1359 def test_sched_setaffinity(self):
1360 mask = posix.sched_getaffinity(0)
1361 if len(mask) > 1:
1362 # Empty masks are forbidden
1363 mask.pop()
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001364 posix.sched_setaffinity(0, mask)
Antoine Pitrou84869872012-08-04 16:16:35 +02001365 self.assertEqual(posix.sched_getaffinity(0), mask)
1366 self.assertRaises(OSError, posix.sched_setaffinity, 0, [])
1367 self.assertRaises(ValueError, posix.sched_setaffinity, 0, [-10])
1368 self.assertRaises(OverflowError, posix.sched_setaffinity, 0, [1<<128])
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001369 self.assertRaises(OSError, posix.sched_setaffinity, -1, mask)
1370
Victor Stinner8b905bd2011-10-25 13:34:04 +02001371 def test_rtld_constants(self):
1372 # check presence of major RTLD_* constants
1373 posix.RTLD_LAZY
1374 posix.RTLD_NOW
1375 posix.RTLD_GLOBAL
1376 posix.RTLD_LOCAL
1377
Jesus Cea60c13dd2012-06-23 02:58:14 +02001378 @unittest.skipUnless(hasattr(os, 'SEEK_HOLE'),
1379 "test needs an OS that reports file holes")
Hynek Schlawackf841e422012-06-24 09:51:46 +02001380 def test_fs_holes(self):
Jesus Cea94363612012-06-22 18:32:07 +02001381 # Even if the filesystem doesn't report holes,
1382 # if the OS supports it the SEEK_* constants
1383 # will be defined and will have a consistent
1384 # behaviour:
1385 # os.SEEK_DATA = current position
1386 # os.SEEK_HOLE = end of file position
Hynek Schlawackf841e422012-06-24 09:51:46 +02001387 with open(support.TESTFN, 'r+b') as fp:
Jesus Cea94363612012-06-22 18:32:07 +02001388 fp.write(b"hello")
1389 fp.flush()
1390 size = fp.tell()
1391 fno = fp.fileno()
Jesus Cead46f7d22012-07-07 14:56:04 +02001392 try :
1393 for i in range(size):
1394 self.assertEqual(i, os.lseek(fno, i, os.SEEK_DATA))
1395 self.assertLessEqual(size, os.lseek(fno, i, os.SEEK_HOLE))
1396 self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_DATA)
1397 self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_HOLE)
1398 except OSError :
1399 # Some OSs claim to support SEEK_HOLE/SEEK_DATA
1400 # but it is not true.
1401 # For instance:
1402 # http://lists.freebsd.org/pipermail/freebsd-amd64/2012-January/014332.html
1403 raise unittest.SkipTest("OSError raised!")
Jesus Cea94363612012-06-22 18:32:07 +02001404
Larry Hastingsb0827312014-02-09 22:05:19 -08001405 def test_path_error2(self):
1406 """
1407 Test functions that call path_error2(), providing two filenames in their exceptions.
1408 """
Victor Stinner047b7ae2014-10-05 17:37:41 +02001409 for name in ("rename", "replace", "link"):
Larry Hastingsb0827312014-02-09 22:05:19 -08001410 function = getattr(os, name, None)
Victor Stinnerbed04a72014-10-05 17:37:59 +02001411 if function is None:
1412 continue
Larry Hastingsb0827312014-02-09 22:05:19 -08001413
Victor Stinnerbed04a72014-10-05 17:37:59 +02001414 for dst in ("noodly2", support.TESTFN):
1415 try:
1416 function('doesnotexistfilename', dst)
1417 except OSError as e:
1418 self.assertIn("'doesnotexistfilename' -> '{}'".format(dst), str(e))
1419 break
1420 else:
1421 self.fail("No valid path_error2() test for os." + name)
Larry Hastingsb0827312014-02-09 22:05:19 -08001422
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001423 def test_path_with_null_character(self):
1424 fn = support.TESTFN
1425 fn_with_NUL = fn + '\0'
1426 self.addCleanup(support.unlink, fn)
1427 support.unlink(fn)
1428 fd = None
1429 try:
Serhiy Storchaka7e9d1d12015-04-20 10:12:28 +03001430 with self.assertRaises(ValueError):
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001431 fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises
1432 finally:
1433 if fd is not None:
1434 os.close(fd)
1435 self.assertFalse(os.path.exists(fn))
Serhiy Storchaka7e9d1d12015-04-20 10:12:28 +03001436 self.assertRaises(ValueError, os.mkdir, fn_with_NUL)
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001437 self.assertFalse(os.path.exists(fn))
1438 open(fn, 'wb').close()
Serhiy Storchaka7e9d1d12015-04-20 10:12:28 +03001439 self.assertRaises(ValueError, os.stat, fn_with_NUL)
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001440
1441 def test_path_with_null_byte(self):
1442 fn = os.fsencode(support.TESTFN)
1443 fn_with_NUL = fn + b'\0'
1444 self.addCleanup(support.unlink, fn)
1445 support.unlink(fn)
1446 fd = None
1447 try:
1448 with self.assertRaises(ValueError):
1449 fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises
1450 finally:
1451 if fd is not None:
1452 os.close(fd)
1453 self.assertFalse(os.path.exists(fn))
1454 self.assertRaises(ValueError, os.mkdir, fn_with_NUL)
1455 self.assertFalse(os.path.exists(fn))
1456 open(fn, 'wb').close()
1457 self.assertRaises(ValueError, os.stat, fn_with_NUL)
1458
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001459class PosixGroupsTester(unittest.TestCase):
1460
1461 def setUp(self):
1462 if posix.getuid() != 0:
1463 raise unittest.SkipTest("not enough privileges")
1464 if not hasattr(posix, 'getgroups'):
1465 raise unittest.SkipTest("need posix.getgroups")
1466 if sys.platform == 'darwin':
1467 raise unittest.SkipTest("getgroups(2) is broken on OSX")
1468 self.saved_groups = posix.getgroups()
1469
1470 def tearDown(self):
1471 if hasattr(posix, 'setgroups'):
1472 posix.setgroups(self.saved_groups)
1473 elif hasattr(posix, 'initgroups'):
1474 name = pwd.getpwuid(posix.getuid()).pw_name
1475 posix.initgroups(name, self.saved_groups[0])
1476
1477 @unittest.skipUnless(hasattr(posix, 'initgroups'),
1478 "test needs posix.initgroups()")
1479 def test_initgroups(self):
1480 # find missing group
1481
Benjamin Peterson659a6f52014-03-01 19:14:12 -05001482 g = max(self.saved_groups or [0]) + 1
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001483 name = pwd.getpwuid(posix.getuid()).pw_name
1484 posix.initgroups(name, g)
1485 self.assertIn(g, posix.getgroups())
1486
1487 @unittest.skipUnless(hasattr(posix, 'setgroups'),
1488 "test needs posix.setgroups()")
1489 def test_setgroups(self):
Antoine Pitroue5a91012010-09-04 17:32:06 +00001490 for groups in [[0], list(range(16))]:
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00001491 posix.setgroups(groups)
1492 self.assertListEqual(groups, posix.getgroups())
1493
Neal Norwitze241ce82003-02-17 18:17:05 +00001494def test_main():
Antoine Pitrou68c95922011-03-20 17:33:57 +01001495 try:
Pablo Galindo8e633a42018-05-14 17:52:43 -04001496 support.run_unittest(PosixTester, PosixGroupsTester)
Antoine Pitrou68c95922011-03-20 17:33:57 +01001497 finally:
1498 support.reap_children()
Neal Norwitze241ce82003-02-17 18:17:05 +00001499
1500if __name__ == '__main__':
1501 test_main()