Debian Codegraph @

Lib/test/test_posix.py

raw
1 "Test posix functions"
2
3 from test import support
4 from test.support import is_apple
5 from test.support import os_helper
6 from test.support import warnings_helper
7 from test.support.script_helper import assert_python_ok
8
9 import copy
10 import errno
11 import sys
12 import signal
13 import time
14 import os
15 import platform
16 import pickle
17 import stat
18 import tempfile
19 import unittest
20 import warnings
21 import textwrap
22 from contextlib import contextmanager
23
24 try:
25 import posix
26 except ImportError:
27 import nt as posix
28
29 try:
30 import pwd
31 except ImportError:
32 pwd = None
33
34 _DUMMY_SYMLINK = os.path.join(tempfile.gettempdir(),
35 os_helper.TESTFN + '-dummy-symlink')
36
37 requires_32b = unittest.skipUnless(
38 # Emscripten/WASI have 32 bits pointers, but support 64 bits syscall args.
39 sys.maxsize < 2**32 and not (support.is_emscripten or support.is_wasi),
40 'test is only meaningful on 32-bit builds'
41 )
42
43 def _supports_sched():
44 if not hasattr(posix, 'sched_getscheduler'):
45 return False
46 try:
47 posix.sched_getscheduler(0)
48 except OSError as e:
49 if e.errno == errno.ENOSYS:
50 return False
51 return True
52
53 requires_sched = unittest.skipUnless(_supports_sched(), 'requires POSIX scheduler API')
54
55
56 class PosixTester(unittest.TestCase):
57
58 def setUp(self):
59 # create empty file
60 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
61 with open(os_helper.TESTFN, "wb"):
62 pass
63 self.enterContext(warnings_helper.check_warnings())
64 warnings.filterwarnings('ignore', '.* potential security risk .*',
65 RuntimeWarning)
66
67 def testNoArgFunctions(self):
68 # test posix functions which take no arguments and have
69 # no side-effects which we need to cleanup (e.g., fork, wait, abort)
70 NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdb", "uname",
71 "times", "getloadavg",
72 "getegid", "geteuid", "getgid", "getgroups",
73 "getpid", "getpgrp", "getppid", "getuid", "sync",
74 ]
75
76 for name in NO_ARG_FUNCTIONS:
77 posix_func = getattr(posix, name, None)
78 if posix_func is not None:
79 with self.subTest(name):
80 posix_func()
81 self.assertRaises(TypeError, posix_func, 1)
82
83 @unittest.skipUnless(hasattr(posix, 'getresuid'),
84 'test needs posix.getresuid()')
85 def test_getresuid(self):
86 user_ids = posix.getresuid()
87 self.assertEqual(len(user_ids), 3)
88 for val in user_ids:
89 self.assertGreaterEqual(val, 0)
90
91 @unittest.skipUnless(hasattr(posix, 'getresgid'),
92 'test needs posix.getresgid()')
93 def test_getresgid(self):
94 group_ids = posix.getresgid()
95 self.assertEqual(len(group_ids), 3)
96 for val in group_ids:
97 self.assertGreaterEqual(val, 0)
98
99 @unittest.skipUnless(hasattr(posix, 'setresuid'),
100 'test needs posix.setresuid()')
101 def test_setresuid(self):
102 current_user_ids = posix.getresuid()
103 self.assertIsNone(posix.setresuid(*current_user_ids))
104 # -1 means don't change that value.
105 self.assertIsNone(posix.setresuid(-1, -1, -1))
106
107 @unittest.skipUnless(hasattr(posix, 'setresuid'),
108 'test needs posix.setresuid()')
109 def test_setresuid_exception(self):
110 # Don't do this test if someone is silly enough to run us as root.
111 current_user_ids = posix.getresuid()
112 if 0 not in current_user_ids:
113 new_user_ids = (current_user_ids[0]+1, -1, -1)
114 self.assertRaises(OSError, posix.setresuid, *new_user_ids)
115
116 @unittest.skipUnless(hasattr(posix, 'setresgid'),
117 'test needs posix.setresgid()')
118 def test_setresgid(self):
119 current_group_ids = posix.getresgid()
120 self.assertIsNone(posix.setresgid(*current_group_ids))
121 # -1 means don't change that value.
122 self.assertIsNone(posix.setresgid(-1, -1, -1))
123
124 @unittest.skipUnless(hasattr(posix, 'setresgid'),
125 'test needs posix.setresgid()')
126 def test_setresgid_exception(self):
127 # Don't do this test if someone is silly enough to run us as root.
128 current_group_ids = posix.getresgid()
129 if 0 not in current_group_ids:
130 new_group_ids = (current_group_ids[0]+1, -1, -1)
131 self.assertRaises(OSError, posix.setresgid, *new_group_ids)
132
133 @unittest.skipUnless(hasattr(posix, 'initgroups'),
134 "test needs os.initgroups()")
135 @unittest.skipUnless(hasattr(pwd, 'getpwuid'), "test needs pwd.getpwuid()")
136 def test_initgroups(self):
137 # It takes a string and an integer; check that it raises a TypeError
138 # for other argument lists.
139 self.assertRaises(TypeError, posix.initgroups)
140 self.assertRaises(TypeError, posix.initgroups, None)
141 self.assertRaises(TypeError, posix.initgroups, 3, "foo")
142 self.assertRaises(TypeError, posix.initgroups, "foo", 3, object())
143
144 # If a non-privileged user invokes it, it should fail with OSError
145 # EPERM.
146 if os.getuid() != 0:
147 try:
148 name = pwd.getpwuid(posix.getuid()).pw_name
149 except KeyError:
150 # the current UID may not have a pwd entry
151 raise unittest.SkipTest("need a pwd entry")
152 try:
153 posix.initgroups(name, 13)
154 except OSError as e:
155 self.assertEqual(e.errno, errno.EPERM)
156 else:
157 self.fail("Expected OSError to be raised by initgroups")
158
159 @unittest.skipUnless(hasattr(posix, 'statvfs'),
160 'test needs posix.statvfs()')
161 def test_statvfs(self):
162 self.assertTrue(posix.statvfs(os.curdir))
163
164 @unittest.skipUnless(hasattr(posix, 'fstatvfs'),
165 'test needs posix.fstatvfs()')
166 def test_fstatvfs(self):
167 fp = open(os_helper.TESTFN)
168 try:
169 self.assertTrue(posix.fstatvfs(fp.fileno()))
170 self.assertTrue(posix.statvfs(fp.fileno()))
171 finally:
172 fp.close()
173
174 @unittest.skipUnless(hasattr(posix, 'ftruncate'),
175 'test needs posix.ftruncate()')
176 def test_ftruncate(self):
177 fp = open(os_helper.TESTFN, 'w+')
178 try:
179 # we need to have some data to truncate
180 fp.write('test')
181 fp.flush()
182 posix.ftruncate(fp.fileno(), 0)
183 finally:
184 fp.close()
185
186 @unittest.skipUnless(hasattr(posix, 'truncate'), "test needs posix.truncate()")
187 def test_truncate(self):
188 with open(os_helper.TESTFN, 'w') as fp:
189 fp.write('test')
190 fp.flush()
191 posix.truncate(os_helper.TESTFN, 0)
192
193 @unittest.skipUnless(getattr(os, 'execve', None) in os.supports_fd, "test needs execve() to support the fd parameter")
194 @support.requires_fork()
195 def test_fexecve(self):
196 fp = os.open(sys.executable, os.O_RDONLY)
197 try:
198 pid = os.fork()
199 if pid == 0:
200 os.chdir(os.path.split(sys.executable)[0])
201 posix.execve(fp, [sys.executable, '-c', 'pass'], os.environ)
202 else:
203 support.wait_process(pid, exitcode=0)
204 finally:
205 os.close(fp)
206
207
208 @unittest.skipUnless(hasattr(posix, 'waitid'), "test needs posix.waitid()")
209 @support.requires_fork()
210 def test_waitid(self):
211 pid = os.fork()
212 if pid == 0:
213 os.chdir(os.path.split(sys.executable)[0])
214 posix.execve(sys.executable, [sys.executable, '-c', 'pass'], os.environ)
215 else:
216 res = posix.waitid(posix.P_PID, pid, posix.WEXITED)
217 self.assertEqual(pid, res.si_pid)
218
219 @support.requires_fork()
220 def test_register_at_fork(self):
221 with self.assertRaises(TypeError, msg="Positional args not allowed"):
222 os.register_at_fork(lambda: None)
223 with self.assertRaises(TypeError, msg="Args must be callable"):
224 os.register_at_fork(before=2)
225 with self.assertRaises(TypeError, msg="Args must be callable"):
226 os.register_at_fork(after_in_child="three")
227 with self.assertRaises(TypeError, msg="Args must be callable"):
228 os.register_at_fork(after_in_parent=b"Five")
229 with self.assertRaises(TypeError, msg="Args must not be None"):
230 os.register_at_fork(before=None)
231 with self.assertRaises(TypeError, msg="Args must not be None"):
232 os.register_at_fork(after_in_child=None)
233 with self.assertRaises(TypeError, msg="Args must not be None"):
234 os.register_at_fork(after_in_parent=None)
235 with self.assertRaises(TypeError, msg="Invalid arg was allowed"):
236 # Ensure a combination of valid and invalid is an error.
237 os.register_at_fork(before=None, after_in_parent=lambda: 3)
238 with self.assertRaises(TypeError, msg="At least one argument is required"):
239 # when no arg is passed
240 os.register_at_fork()
241 with self.assertRaises(TypeError, msg="Invalid arg was allowed"):
242 # Ensure a combination of valid and invalid is an error.
243 os.register_at_fork(before=lambda: None, after_in_child='')
244 # We test actual registrations in their own process so as not to
245 # pollute this one. There is no way to unregister for cleanup.
246 code = """if 1:
247 import os
248
249 r, w = os.pipe()
250 fin_r, fin_w = os.pipe()
251
252 os.register_at_fork(before=lambda: os.write(w, b'A'))
253 os.register_at_fork(after_in_parent=lambda: os.write(w, b'C'))
254 os.register_at_fork(after_in_child=lambda: os.write(w, b'E'))
255 os.register_at_fork(before=lambda: os.write(w, b'B'),
256 after_in_parent=lambda: os.write(w, b'D'),
257 after_in_child=lambda: os.write(w, b'F'))
258
259 pid = os.fork()
260 if pid == 0:
261 # At this point, after-forkers have already been executed
262 os.close(w)
263 # Wait for parent to tell us to exit
264 os.read(fin_r, 1)
265 os._exit(0)
266 else:
267 try:
268 os.close(w)
269 with open(r, "rb") as f:
270 data = f.read()
271 assert len(data) == 6, data
272 # Check before-fork callbacks
273 assert data[:2] == b'BA', data
274 # Check after-fork callbacks
275 assert sorted(data[2:]) == list(b'CDEF'), data
276 assert data.index(b'C') < data.index(b'D'), data
277 assert data.index(b'E') < data.index(b'F'), data
278 finally:
279 os.write(fin_w, b'!')
280 """
281 assert_python_ok('-c', code)
282
283 @unittest.skipUnless(hasattr(posix, 'lockf'), "test needs posix.lockf()")
284 def test_lockf(self):
285 fd = os.open(os_helper.TESTFN, os.O_WRONLY | os.O_CREAT)
286 try:
287 os.write(fd, b'test')
288 os.lseek(fd, 0, os.SEEK_SET)
289 posix.lockf(fd, posix.F_LOCK, 4)
290 # section is locked
291 posix.lockf(fd, posix.F_ULOCK, 4)
292 finally:
293 os.close(fd)
294
295 @unittest.skipUnless(hasattr(posix, 'pread'), "test needs posix.pread()")
296 def test_pread(self):
297 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
298 try:
299 os.write(fd, b'test')
300 os.lseek(fd, 0, os.SEEK_SET)
301 self.assertEqual(b'es', posix.pread(fd, 2, 1))
302 # the first pread() shouldn't disturb the file offset
303 self.assertEqual(b'te', posix.read(fd, 2))
304 finally:
305 os.close(fd)
306
307 @unittest.skipUnless(hasattr(posix, 'preadv'), "test needs posix.preadv()")
308 def test_preadv(self):
309 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
310 try:
311 os.write(fd, b'test1tt2t3t5t6t6t8')
312 buf = [bytearray(i) for i in [5, 3, 2]]
313 self.assertEqual(posix.preadv(fd, buf, 3), 10)
314 self.assertEqual([b't1tt2', b't3t', b'5t'], list(buf))
315 finally:
316 os.close(fd)
317
318 @unittest.skipUnless(hasattr(posix, 'preadv'), "test needs posix.preadv()")
319 @unittest.skipUnless(hasattr(posix, 'RWF_HIPRI'), "test needs posix.RWF_HIPRI")
320 def test_preadv_flags(self):
321 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
322 try:
323 os.write(fd, b'test1tt2t3t5t6t6t8')
324 buf = [bytearray(i) for i in [5, 3, 2]]
325 self.assertEqual(posix.preadv(fd, buf, 3, os.RWF_HIPRI), 10)
326 self.assertEqual([b't1tt2', b't3t', b'5t'], list(buf))
327 except NotImplementedError:
328 self.skipTest("preadv2 not available")
329 except OSError as inst:
330 # Is possible that the macro RWF_HIPRI was defined at compilation time
331 # but the option is not supported by the kernel or the runtime libc shared
332 # library.
333 if inst.errno in {errno.EINVAL, errno.ENOTSUP}:
334 raise unittest.SkipTest("RWF_HIPRI is not supported by the current system")
335 else:
336 raise
337 finally:
338 os.close(fd)
339
340 @unittest.skipUnless(hasattr(posix, 'preadv'), "test needs posix.preadv()")
341 @requires_32b
342 def test_preadv_overflow_32bits(self):
343 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
344 try:
345 buf = [bytearray(2**16)] * 2**15
346 with self.assertRaises(OSError) as cm:
347 os.preadv(fd, buf, 0)
348 self.assertEqual(cm.exception.errno, errno.EINVAL)
349 self.assertEqual(bytes(buf[0]), b'\0'* 2**16)
350 finally:
351 os.close(fd)
352
353 @unittest.skipUnless(hasattr(posix, 'pwrite'), "test needs posix.pwrite()")
354 def test_pwrite(self):
355 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
356 try:
357 os.write(fd, b'test')
358 os.lseek(fd, 0, os.SEEK_SET)
359 posix.pwrite(fd, b'xx', 1)
360 self.assertEqual(b'txxt', posix.read(fd, 4))
361 finally:
362 os.close(fd)
363
364 @unittest.skipUnless(hasattr(posix, 'pwritev'), "test needs posix.pwritev()")
365 def test_pwritev(self):
366 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
367 try:
368 os.write(fd, b"xx")
369 os.lseek(fd, 0, os.SEEK_SET)
370 n = os.pwritev(fd, [b'test1', b'tt2', b't3'], 2)
371 self.assertEqual(n, 10)
372
373 os.lseek(fd, 0, os.SEEK_SET)
374 self.assertEqual(b'xxtest1tt2t3', posix.read(fd, 100))
375 finally:
376 os.close(fd)
377
378 @unittest.skipUnless(hasattr(posix, 'pwritev'), "test needs posix.pwritev()")
379 @unittest.skipUnless(hasattr(posix, 'os.RWF_SYNC'), "test needs os.RWF_SYNC")
380 def test_pwritev_flags(self):
381 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
382 try:
383 os.write(fd,b"xx")
384 os.lseek(fd, 0, os.SEEK_SET)
385 n = os.pwritev(fd, [b'test1', b'tt2', b't3'], 2, os.RWF_SYNC)
386 self.assertEqual(n, 10)
387
388 os.lseek(fd, 0, os.SEEK_SET)
389 self.assertEqual(b'xxtest1tt2', posix.read(fd, 100))
390 finally:
391 os.close(fd)
392
393 @unittest.skipUnless(hasattr(posix, 'pwritev'), "test needs posix.pwritev()")
394 @requires_32b
395 def test_pwritev_overflow_32bits(self):
396 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
397 try:
398 with self.assertRaises(OSError) as cm:
399 os.pwritev(fd, [b"x" * 2**16] * 2**15, 0)
400 self.assertEqual(cm.exception.errno, errno.EINVAL)
401 finally:
402 os.close(fd)
403
404 @unittest.skipUnless(hasattr(posix, 'posix_fallocate'),
405 "test needs posix.posix_fallocate()")
406 def test_posix_fallocate(self):
407 fd = os.open(os_helper.TESTFN, os.O_WRONLY | os.O_CREAT)
408 try:
409 posix.posix_fallocate(fd, 0, 10)
410 except OSError as inst:
411 # issue10812, ZFS doesn't appear to support posix_fallocate,
412 # so skip Solaris-based since they are likely to have ZFS.
413 # issue33655: Also ignore EINVAL on *BSD since ZFS is also
414 # often used there.
415 if inst.errno == errno.EINVAL and sys.platform.startswith(
416 ('sunos', 'freebsd', 'openbsd', 'gnukfreebsd')):
417 raise unittest.SkipTest("test may fail on ZFS filesystems")
418 elif inst.errno == errno.EOPNOTSUPP and sys.platform.startswith("netbsd"):
419 raise unittest.SkipTest("test may fail on FFS filesystems")
420 else:
421 raise
422 finally:
423 os.close(fd)
424
425 # issue31106 - posix_fallocate() does not set error in errno.
426 @unittest.skipUnless(hasattr(posix, 'posix_fallocate'),
427 "test needs posix.posix_fallocate()")
428 def test_posix_fallocate_errno(self):
429 try:
430 posix.posix_fallocate(-42, 0, 10)
431 except OSError as inst:
432 if inst.errno != errno.EBADF:
433 raise
434
435 @unittest.skipUnless(hasattr(posix, 'posix_fadvise'),
436 "test needs posix.posix_fadvise()")
437 def test_posix_fadvise(self):
438 fd = os.open(os_helper.TESTFN, os.O_RDONLY)
439 try:
440 posix.posix_fadvise(fd, 0, 0, posix.POSIX_FADV_WILLNEED)
441 finally:
442 os.close(fd)
443
444 @unittest.skipUnless(hasattr(posix, 'posix_fadvise'),
445 "test needs posix.posix_fadvise()")
446 def test_posix_fadvise_errno(self):
447 try:
448 posix.posix_fadvise(-42, 0, 0, posix.POSIX_FADV_WILLNEED)
449 except OSError as inst:
450 if inst.errno != errno.EBADF:
451 raise
452
453 @unittest.skipUnless(os.utime in os.supports_fd, "test needs fd support in os.utime")
454 def test_utime_with_fd(self):
455 now = time.time()
456 fd = os.open(os_helper.TESTFN, os.O_RDONLY)
457 try:
458 posix.utime(fd)
459 posix.utime(fd, None)
460 self.assertRaises(TypeError, posix.utime, fd, (None, None))
461 self.assertRaises(TypeError, posix.utime, fd, (now, None))
462 self.assertRaises(TypeError, posix.utime, fd, (None, now))
463 posix.utime(fd, (int(now), int(now)))
464 posix.utime(fd, (now, now))
465 self.assertRaises(ValueError, posix.utime, fd, (now, now), ns=(now, now))
466 self.assertRaises(ValueError, posix.utime, fd, (now, 0), ns=(None, None))
467 self.assertRaises(ValueError, posix.utime, fd, (None, None), ns=(now, 0))
468 posix.utime(fd, (int(now), int((now - int(now)) * 1e9)))
469 posix.utime(fd, ns=(int(now), int((now - int(now)) * 1e9)))
470
471 finally:
472 os.close(fd)
473
474 @unittest.skipUnless(os.utime in os.supports_follow_symlinks, "test needs follow_symlinks support in os.utime")
475 def test_utime_nofollow_symlinks(self):
476 now = time.time()
477 posix.utime(os_helper.TESTFN, None, follow_symlinks=False)
478 self.assertRaises(TypeError, posix.utime, os_helper.TESTFN,
479 (None, None), follow_symlinks=False)
480 self.assertRaises(TypeError, posix.utime, os_helper.TESTFN,
481 (now, None), follow_symlinks=False)
482 self.assertRaises(TypeError, posix.utime, os_helper.TESTFN,
483 (None, now), follow_symlinks=False)
484 posix.utime(os_helper.TESTFN, (int(now), int(now)),
485 follow_symlinks=False)
486 posix.utime(os_helper.TESTFN, (now, now), follow_symlinks=False)
487 posix.utime(os_helper.TESTFN, follow_symlinks=False)
488
489 @unittest.skipUnless(hasattr(posix, 'writev'), "test needs posix.writev()")
490 def test_writev(self):
491 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
492 try:
493 n = os.writev(fd, (b'test1', b'tt2', b't3'))
494 self.assertEqual(n, 10)
495
496 os.lseek(fd, 0, os.SEEK_SET)
497 self.assertEqual(b'test1tt2t3', posix.read(fd, 10))
498
499 # Issue #20113: empty list of buffers should not crash
500 try:
501 size = posix.writev(fd, [])
502 except OSError:
503 # writev(fd, []) raises OSError(22, "Invalid argument")
504 # on OpenIndiana
505 pass
506 else:
507 self.assertEqual(size, 0)
508 finally:
509 os.close(fd)
510
511 @unittest.skipUnless(hasattr(posix, 'writev'), "test needs posix.writev()")
512 @requires_32b
513 def test_writev_overflow_32bits(self):
514 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
515 try:
516 with self.assertRaises(OSError) as cm:
517 os.writev(fd, [b"x" * 2**16] * 2**15)
518 self.assertEqual(cm.exception.errno, errno.EINVAL)
519 finally:
520 os.close(fd)
521
522 @unittest.skipUnless(hasattr(posix, 'readv'), "test needs posix.readv()")
523 def test_readv(self):
524 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
525 try:
526 os.write(fd, b'test1tt2t3')
527 os.lseek(fd, 0, os.SEEK_SET)
528 buf = [bytearray(i) for i in [5, 3, 2]]
529 self.assertEqual(posix.readv(fd, buf), 10)
530 self.assertEqual([b'test1', b'tt2', b't3'], [bytes(i) for i in buf])
531
532 # Issue #20113: empty list of buffers should not crash
533 try:
534 size = posix.readv(fd, [])
535 except OSError:
536 # readv(fd, []) raises OSError(22, "Invalid argument")
537 # on OpenIndiana
538 pass
539 else:
540 self.assertEqual(size, 0)
541 finally:
542 os.close(fd)
543
544 @unittest.skipUnless(hasattr(posix, 'readv'), "test needs posix.readv()")
545 @requires_32b
546 def test_readv_overflow_32bits(self):
547 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
548 try:
549 buf = [bytearray(2**16)] * 2**15
550 with self.assertRaises(OSError) as cm:
551 os.readv(fd, buf)
552 self.assertEqual(cm.exception.errno, errno.EINVAL)
553 self.assertEqual(bytes(buf[0]), b'\0'* 2**16)
554 finally:
555 os.close(fd)
556
557 @unittest.skipUnless(hasattr(posix, 'dup'),
558 'test needs posix.dup()')
559 @unittest.skipIf(support.is_wasi, "WASI does not have dup()")
560 def test_dup(self):
561 fp = open(os_helper.TESTFN)
562 try:
563 fd = posix.dup(fp.fileno())
564 self.assertIsInstance(fd, int)
565 os.close(fd)
566 finally:
567 fp.close()
568
569 @unittest.skipUnless(hasattr(posix, 'confstr'),
570 'test needs posix.confstr()')
571 def test_confstr(self):
572 with self.assertRaisesRegex(
573 ValueError, "unrecognized configuration name"
574 ):
575 posix.confstr("CS_garbage")
576
577 with self.assertRaisesRegex(
578 TypeError, "configuration names must be strings or integers"
579 ):
580 posix.confstr(1.23)
581
582 path = posix.confstr("CS_PATH")
583 self.assertGreater(len(path), 0)
584 self.assertEqual(posix.confstr(posix.confstr_names["CS_PATH"]), path)
585
586 @unittest.skipUnless(hasattr(posix, 'sysconf'),
587 'test needs posix.sysconf()')
588 def test_sysconf(self):
589 with self.assertRaisesRegex(
590 ValueError, "unrecognized configuration name"
591 ):
592 posix.sysconf("SC_garbage")
593
594 with self.assertRaisesRegex(
595 TypeError, "configuration names must be strings or integers"
596 ):
597 posix.sysconf(1.23)
598
599 arg_max = posix.sysconf("SC_ARG_MAX")
600 self.assertGreater(arg_max, 0)
601 self.assertEqual(
602 posix.sysconf(posix.sysconf_names["SC_ARG_MAX"]), arg_max)
603
604 @unittest.skipUnless(hasattr(posix, 'dup2'),
605 'test needs posix.dup2()')
606 @unittest.skipIf(support.is_wasi, "WASI does not have dup2()")
607 def test_dup2(self):
608 fp1 = open(os_helper.TESTFN)
609 fp2 = open(os_helper.TESTFN)
610 try:
611 posix.dup2(fp1.fileno(), fp2.fileno())
612 finally:
613 fp1.close()
614 fp2.close()
615
616 @unittest.skipUnless(hasattr(os, 'O_CLOEXEC'), "needs os.O_CLOEXEC")
617 @support.requires_linux_version(2, 6, 23)
618 @support.requires_subprocess()
619 def test_oscloexec(self):
620 fd = os.open(os_helper.TESTFN, os.O_RDONLY|os.O_CLOEXEC)
621 self.addCleanup(os.close, fd)
622 self.assertFalse(os.get_inheritable(fd))
623
624 @unittest.skipUnless(hasattr(posix, 'O_EXLOCK'),
625 'test needs posix.O_EXLOCK')
626 def test_osexlock(self):
627 fd = os.open(os_helper.TESTFN,
628 os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
629 self.assertRaises(OSError, os.open, os_helper.TESTFN,
630 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
631 os.close(fd)
632
633 if hasattr(posix, "O_SHLOCK"):
634 fd = os.open(os_helper.TESTFN,
635 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
636 self.assertRaises(OSError, os.open, os_helper.TESTFN,
637 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
638 os.close(fd)
639
640 @unittest.skipUnless(hasattr(posix, 'O_SHLOCK'),
641 'test needs posix.O_SHLOCK')
642 def test_osshlock(self):
643 fd1 = os.open(os_helper.TESTFN,
644 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
645 fd2 = os.open(os_helper.TESTFN,
646 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
647 os.close(fd2)
648 os.close(fd1)
649
650 if hasattr(posix, "O_EXLOCK"):
651 fd = os.open(os_helper.TESTFN,
652 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
653 self.assertRaises(OSError, os.open, os_helper.TESTFN,
654 os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
655 os.close(fd)
656
657 @unittest.skipUnless(hasattr(posix, 'fstat'),
658 'test needs posix.fstat()')
659 def test_fstat(self):
660 fp = open(os_helper.TESTFN)
661 try:
662 self.assertTrue(posix.fstat(fp.fileno()))
663 self.assertTrue(posix.stat(fp.fileno()))
664
665 self.assertRaisesRegex(TypeError,
666 'should be string, bytes, os.PathLike or integer, not',
667 posix.stat, float(fp.fileno()))
668 finally:
669 fp.close()
670
671 @unittest.skipUnless(hasattr(posix, 'stat'),
672 'test needs posix.stat()')
673 @unittest.skipUnless(os.stat in os.supports_follow_symlinks,
674 'test needs follow_symlinks support in os.stat()')
675 def test_stat_fd_zero_follow_symlinks(self):
676 with self.assertRaisesRegex(ValueError,
677 'cannot use fd and follow_symlinks together'):
678 posix.stat(0, follow_symlinks=False)
679 with self.assertRaisesRegex(ValueError,
680 'cannot use fd and follow_symlinks together'):
681 posix.stat(1, follow_symlinks=False)
682
683 def test_stat(self):
684 self.assertTrue(posix.stat(os_helper.TESTFN))
685 self.assertTrue(posix.stat(os.fsencode(os_helper.TESTFN)))
686
687 self.assertRaisesRegex(TypeError,
688 'should be string, bytes, os.PathLike or integer, not',
689 posix.stat, bytearray(os.fsencode(os_helper.TESTFN)))
690 self.assertRaisesRegex(TypeError,
691 'should be string, bytes, os.PathLike or integer, not',
692 posix.stat, None)
693 self.assertRaisesRegex(TypeError,
694 'should be string, bytes, os.PathLike or integer, not',
695 posix.stat, list(os_helper.TESTFN))
696 self.assertRaisesRegex(TypeError,
697 'should be string, bytes, os.PathLike or integer, not',
698 posix.stat, list(os.fsencode(os_helper.TESTFN)))
699
700 @unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()")
701 def test_mkfifo(self):
702 if sys.platform == "vxworks":
703 fifo_path = os.path.join("/fifos/", os_helper.TESTFN)
704 else:
705 fifo_path = os_helper.TESTFN
706 os_helper.unlink(fifo_path)
707 self.addCleanup(os_helper.unlink, fifo_path)
708 try:
709 posix.mkfifo(fifo_path, stat.S_IRUSR | stat.S_IWUSR)
710 except PermissionError as e:
711 self.skipTest('posix.mkfifo(): %s' % e)
712 self.assertTrue(stat.S_ISFIFO(posix.stat(fifo_path).st_mode))
713
714 @unittest.skipUnless(hasattr(posix, 'mknod') and hasattr(stat, 'S_IFIFO'),
715 "don't have mknod()/S_IFIFO")
716 def test_mknod(self):
717 # Test using mknod() to create a FIFO (the only use specified
718 # by POSIX).
719 os_helper.unlink(os_helper.TESTFN)
720 mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR
721 try:
722 posix.mknod(os_helper.TESTFN, mode, 0)
723 except OSError as e:
724 # Some old systems don't allow unprivileged users to use
725 # mknod(), or only support creating device nodes.
726 self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES))
727 else:
728 self.assertTrue(stat.S_ISFIFO(posix.stat(os_helper.TESTFN).st_mode))
729
730 # Keyword arguments are also supported
731 os_helper.unlink(os_helper.TESTFN)
732 try:
733 posix.mknod(path=os_helper.TESTFN, mode=mode, device=0,
734 dir_fd=None)
735 except OSError as e:
736 self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES))
737
738 @unittest.skipUnless(hasattr(posix, 'makedev'), 'test needs posix.makedev()')
739 def test_makedev(self):
740 st = posix.stat(os_helper.TESTFN)
741 dev = st.st_dev
742 self.assertIsInstance(dev, int)
743 self.assertGreaterEqual(dev, 0)
744
745 major = posix.major(dev)
746 self.assertIsInstance(major, int)
747 self.assertGreaterEqual(major, 0)
748 self.assertEqual(posix.major(dev), major)
749 self.assertRaises(TypeError, posix.major, float(dev))
750 self.assertRaises(TypeError, posix.major)
751 for x in -2, 2**64, -2**63-1:
752 self.assertRaises((ValueError, OverflowError), posix.major, x)
753
754 minor = posix.minor(dev)
755 self.assertIsInstance(minor, int)
756 self.assertGreaterEqual(minor, 0)
757 self.assertEqual(posix.minor(dev), minor)
758 self.assertRaises(TypeError, posix.minor, float(dev))
759 self.assertRaises(TypeError, posix.minor)
760 for x in -2, 2**64, -2**63-1:
761 self.assertRaises((ValueError, OverflowError), posix.minor, x)
762
763 self.assertEqual(posix.makedev(major, minor), dev)
764 self.assertRaises(TypeError, posix.makedev, float(major), minor)
765 self.assertRaises(TypeError, posix.makedev, major, float(minor))
766 self.assertRaises(TypeError, posix.makedev, major)
767 self.assertRaises(TypeError, posix.makedev)
768 for x in -2, 2**32, 2**64, -2**63-1:
769 self.assertRaises((ValueError, OverflowError), posix.makedev, x, minor)
770 self.assertRaises((ValueError, OverflowError), posix.makedev, major, x)
771
772 if sys.platform == 'linux' and not support.linked_to_musl():
773 NODEV = -1
774 self.assertEqual(posix.major(NODEV), NODEV)
775 self.assertEqual(posix.minor(NODEV), NODEV)
776 self.assertEqual(posix.makedev(NODEV, NODEV), NODEV)
777
778 def _test_all_chown_common(self, chown_func, first_param, stat_func):
779 """Common code for chown, fchown and lchown tests."""
780 def check_stat(uid, gid):
781 if stat_func is not None:
782 stat = stat_func(first_param)
783 self.assertEqual(stat.st_uid, uid)
784 self.assertEqual(stat.st_gid, gid)
785 uid = os.getuid()
786 gid = os.getgid()
787 # test a successful chown call
788 chown_func(first_param, uid, gid)
789 check_stat(uid, gid)
790 chown_func(first_param, -1, gid)
791 check_stat(uid, gid)
792 chown_func(first_param, uid, -1)
793 check_stat(uid, gid)
794
795 if sys.platform == "vxworks":
796 # On VxWorks, root user id is 1 and 0 means no login user:
797 # both are super users.
798 is_root = (uid in (0, 1))
799 else:
800 is_root = (uid == 0)
801 if support.is_emscripten:
802 # Emscripten getuid() / geteuid() always return 0 (root), but
803 # cannot chown uid/gid to random value.
804 pass
805 elif is_root:
806 # Try an amusingly large uid/gid to make sure we handle
807 # large unsigned values. (chown lets you use any
808 # uid/gid you like, even if they aren't defined.)
809 #
810 # On VxWorks uid_t is defined as unsigned short. A big
811 # value greater than 65535 will result in underflow error.
812 #
813 # This problem keeps coming up:
814 # http://bugs.python.org/issue1747858
815 # http://bugs.python.org/issue4591
816 # http://bugs.python.org/issue15301
817 # Hopefully the fix in 4591 fixes it for good!
818 #
819 # This part of the test only runs when run as root.
820 # Only scary people run their tests as root.
821
822 big_value = (2**31 if sys.platform != "vxworks" else 2**15)
823 chown_func(first_param, big_value, big_value)
824 check_stat(big_value, big_value)
825 chown_func(first_param, -1, -1)
826 check_stat(big_value, big_value)
827 chown_func(first_param, uid, gid)
828 check_stat(uid, gid)
829 elif platform.system() in ('HP-UX', 'SunOS'):
830 # HP-UX and Solaris can allow a non-root user to chown() to root
831 # (issue #5113)
832 raise unittest.SkipTest("Skipping because of non-standard chown() "
833 "behavior")
834 else:
835 # non-root cannot chown to root, raises OSError
836 self.assertRaises(OSError, chown_func, first_param, 0, 0)
837 check_stat(uid, gid)
838 self.assertRaises(OSError, chown_func, first_param, 0, -1)
839 check_stat(uid, gid)
840 if hasattr(os, 'getgroups'):
841 if 0 not in os.getgroups():
842 self.assertRaises(OSError, chown_func, first_param, -1, 0)
843 check_stat(uid, gid)
844 # test illegal types
845 for t in str, float:
846 self.assertRaises(TypeError, chown_func, first_param, t(uid), gid)
847 check_stat(uid, gid)
848 self.assertRaises(TypeError, chown_func, first_param, uid, t(gid))
849 check_stat(uid, gid)
850
851 @unittest.skipUnless(hasattr(os, "chown"), "requires os.chown()")
852 @unittest.skipIf(support.is_emscripten, "getgid() is a stub")
853 def test_chown(self):
854 # raise an OSError if the file does not exist
855 os.unlink(os_helper.TESTFN)
856 self.assertRaises(OSError, posix.chown, os_helper.TESTFN, -1, -1)
857
858 # re-create the file
859 os_helper.create_empty_file(os_helper.TESTFN)
860 self._test_all_chown_common(posix.chown, os_helper.TESTFN, posix.stat)
861
862 @os_helper.skip_unless_working_chmod
863 @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()")
864 @unittest.skipIf(support.is_emscripten, "getgid() is a stub")
865 def test_fchown(self):
866 os.unlink(os_helper.TESTFN)
867
868 # re-create the file
869 test_file = open(os_helper.TESTFN, 'w')
870 try:
871 fd = test_file.fileno()
872 self._test_all_chown_common(posix.fchown, fd,
873 getattr(posix, 'fstat', None))
874 finally:
875 test_file.close()
876
877 @os_helper.skip_unless_working_chmod
878 @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()")
879 def test_lchown(self):
880 os.unlink(os_helper.TESTFN)
881 # create a symlink
882 os.symlink(_DUMMY_SYMLINK, os_helper.TESTFN)
883 self._test_all_chown_common(posix.lchown, os_helper.TESTFN,
884 getattr(posix, 'lstat', None))
885
886 @unittest.skipUnless(hasattr(posix, 'chdir'), 'test needs posix.chdir()')
887 def test_chdir(self):
888 posix.chdir(os.curdir)
889 self.assertRaises(OSError, posix.chdir, os_helper.TESTFN)
890
891 def test_listdir(self):
892 self.assertIn(os_helper.TESTFN, posix.listdir(os.curdir))
893
894 def test_listdir_default(self):
895 # When listdir is called without argument,
896 # it's the same as listdir(os.curdir).
897 self.assertIn(os_helper.TESTFN, posix.listdir())
898
899 def test_listdir_bytes(self):
900 # When listdir is called with a bytes object,
901 # the returned strings are of type bytes.
902 self.assertIn(os.fsencode(os_helper.TESTFN), posix.listdir(b'.'))
903
904 def test_listdir_bytes_like(self):
905 for cls in bytearray, memoryview:
906 with self.assertRaises(TypeError):
907 posix.listdir(cls(b'.'))
908
909 @unittest.skipUnless(posix.listdir in os.supports_fd,
910 "test needs fd support for posix.listdir()")
911 def test_listdir_fd(self):
912 f = posix.open(posix.getcwd(), posix.O_RDONLY)
913 self.addCleanup(posix.close, f)
914 self.assertEqual(
915 sorted(posix.listdir('.')),
916 sorted(posix.listdir(f))
917 )
918 # Check that the fd offset was reset (issue #13739)
919 self.assertEqual(
920 sorted(posix.listdir('.')),
921 sorted(posix.listdir(f))
922 )
923
924 @unittest.skipUnless(hasattr(posix, 'access'), 'test needs posix.access()')
925 def test_access(self):
926 self.assertTrue(posix.access(os_helper.TESTFN, os.R_OK))
927
928 @unittest.skipUnless(hasattr(posix, 'umask'), 'test needs posix.umask()')
929 def test_umask(self):
930 old_mask = posix.umask(0)
931 self.assertIsInstance(old_mask, int)
932 posix.umask(old_mask)
933
934 @unittest.skipUnless(hasattr(posix, 'strerror'),
935 'test needs posix.strerror()')
936 def test_strerror(self):
937 self.assertTrue(posix.strerror(0))
938
939 @unittest.skipUnless(hasattr(posix, 'pipe'), 'test needs posix.pipe()')
940 def test_pipe(self):
941 reader, writer = posix.pipe()
942 os.close(reader)
943 os.close(writer)
944
945 @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()")
946 @support.requires_linux_version(2, 6, 27)
947 def test_pipe2(self):
948 self.assertRaises(TypeError, os.pipe2, 'DEADBEEF')
949 self.assertRaises(TypeError, os.pipe2, 0, 0)
950
951 # try calling with flags = 0, like os.pipe()
952 r, w = os.pipe2(0)
953 os.close(r)
954 os.close(w)
955
956 # test flags
957 r, w = os.pipe2(os.O_CLOEXEC|os.O_NONBLOCK)
958 self.addCleanup(os.close, r)
959 self.addCleanup(os.close, w)
960 self.assertFalse(os.get_inheritable(r))
961 self.assertFalse(os.get_inheritable(w))
962 self.assertFalse(os.get_blocking(r))
963 self.assertFalse(os.get_blocking(w))
964 # try reading from an empty pipe: this should fail, not block
965 self.assertRaises(OSError, os.read, r, 1)
966 # try a write big enough to fill-up the pipe: this should either
967 # fail or perform a partial write, not block
968 try:
969 os.write(w, b'x' * support.PIPE_MAX_SIZE)
970 except OSError:
971 pass
972
973 @support.cpython_only
974 @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()")
975 @support.requires_linux_version(2, 6, 27)
976 def test_pipe2_c_limits(self):
977 # Issue 15989
978 import _testcapi
979 self.assertRaises(OverflowError, os.pipe2, _testcapi.INT_MAX + 1)
980 self.assertRaises(OverflowError, os.pipe2, _testcapi.UINT_MAX + 1)
981
982 @unittest.skipUnless(hasattr(posix, 'utime'), 'test needs posix.utime()')
983 def test_utime(self):
984 now = time.time()
985 posix.utime(os_helper.TESTFN, None)
986 self.assertRaises(TypeError, posix.utime,
987 os_helper.TESTFN, (None, None))
988 self.assertRaises(TypeError, posix.utime,
989 os_helper.TESTFN, (now, None))
990 self.assertRaises(TypeError, posix.utime,
991 os_helper.TESTFN, (None, now))
992 posix.utime(os_helper.TESTFN, (int(now), int(now)))
993 posix.utime(os_helper.TESTFN, (now, now))
994
995 def check_chmod(self, chmod_func, target, **kwargs):
996 closefd = not isinstance(target, int)
997 mode = os.stat(target).st_mode
998 try:
999 new_mode = mode & ~(stat.S_IWOTH | stat.S_IWGRP | stat.S_IWUSR)
1000 chmod_func(target, new_mode, **kwargs)
1001 self.assertEqual(os.stat(target).st_mode, new_mode)
1002 if stat.S_ISREG(mode):
1003 try:
1004 with open(target, 'wb+', closefd=closefd):
1005 pass
1006 except PermissionError:
1007 pass
1008 new_mode = mode | (stat.S_IWOTH | stat.S_IWGRP | stat.S_IWUSR)
1009 chmod_func(target, new_mode, **kwargs)
1010 self.assertEqual(os.stat(target).st_mode, new_mode)
1011 if stat.S_ISREG(mode):
1012 with open(target, 'wb+', closefd=closefd):
1013 pass
1014 finally:
1015 chmod_func(target, mode)
1016
1017 @os_helper.skip_unless_working_chmod
1018 def test_chmod_file(self):
1019 self.check_chmod(posix.chmod, os_helper.TESTFN)
1020
1021 def tempdir(self):
1022 target = os_helper.TESTFN + 'd'
1023 posix.mkdir(target)
1024 self.addCleanup(posix.rmdir, target)
1025 return target
1026
1027 @os_helper.skip_unless_working_chmod
1028 def test_chmod_dir(self):
1029 target = self.tempdir()
1030 self.check_chmod(posix.chmod, target)
1031
1032 @os_helper.skip_unless_working_chmod
1033 def test_fchmod_file(self):
1034 with open(os_helper.TESTFN, 'wb+') as f:
1035 self.check_chmod(posix.fchmod, f.fileno())
1036 self.check_chmod(posix.chmod, f.fileno())
1037
1038 @unittest.skipUnless(hasattr(posix, 'lchmod'), 'test needs os.lchmod()')
1039 def test_lchmod_file(self):
1040 self.check_chmod(posix.lchmod, os_helper.TESTFN)
1041 self.check_chmod(posix.chmod, os_helper.TESTFN, follow_symlinks=False)
1042
1043 @unittest.skipUnless(hasattr(posix, 'lchmod'), 'test needs os.lchmod()')
1044 def test_lchmod_dir(self):
1045 target = self.tempdir()
1046 self.check_chmod(posix.lchmod, target)
1047 self.check_chmod(posix.chmod, target, follow_symlinks=False)
1048
1049 def check_chmod_link(self, chmod_func, target, link, **kwargs):
1050 target_mode = os.stat(target).st_mode
1051 link_mode = os.lstat(link).st_mode
1052 try:
1053 new_mode = target_mode & ~(stat.S_IWOTH | stat.S_IWGRP | stat.S_IWUSR)
1054 chmod_func(link, new_mode, **kwargs)
1055 self.assertEqual(os.stat(target).st_mode, new_mode)
1056 self.assertEqual(os.lstat(link).st_mode, link_mode)
1057 new_mode = target_mode | (stat.S_IWOTH | stat.S_IWGRP | stat.S_IWUSR)
1058 chmod_func(link, new_mode, **kwargs)
1059 self.assertEqual(os.stat(target).st_mode, new_mode)
1060 self.assertEqual(os.lstat(link).st_mode, link_mode)
1061 finally:
1062 posix.chmod(target, target_mode)
1063
1064 def check_lchmod_link(self, chmod_func, target, link, **kwargs):
1065 target_mode = os.stat(target).st_mode
1066 link_mode = os.lstat(link).st_mode
1067 new_mode = link_mode & ~(stat.S_IWOTH | stat.S_IWGRP | stat.S_IWUSR)
1068 chmod_func(link, new_mode, **kwargs)
1069 self.assertEqual(os.stat(target).st_mode, target_mode)
1070 self.assertEqual(os.lstat(link).st_mode, new_mode)
1071 new_mode = link_mode | (stat.S_IWOTH | stat.S_IWGRP | stat.S_IWUSR)
1072 chmod_func(link, new_mode, **kwargs)
1073 self.assertEqual(os.stat(target).st_mode, target_mode)
1074 self.assertEqual(os.lstat(link).st_mode, new_mode)
1075
1076 @os_helper.skip_unless_symlink
1077 def test_chmod_file_symlink(self):
1078 target = os_helper.TESTFN
1079 link = os_helper.TESTFN + '-link'
1080 os.symlink(target, link)
1081 self.addCleanup(posix.unlink, link)
1082 if os.name == 'nt':
1083 self.check_lchmod_link(posix.chmod, target, link)
1084 else:
1085 self.check_chmod_link(posix.chmod, target, link)
1086 self.check_chmod_link(posix.chmod, target, link, follow_symlinks=True)
1087
1088 @os_helper.skip_unless_symlink
1089 def test_chmod_dir_symlink(self):
1090 target = self.tempdir()
1091 link = os_helper.TESTFN + '-link'
1092 os.symlink(target, link, target_is_directory=True)
1093 self.addCleanup(posix.unlink, link)
1094 if os.name == 'nt':
1095 self.check_lchmod_link(posix.chmod, target, link)
1096 else:
1097 self.check_chmod_link(posix.chmod, target, link)
1098 self.check_chmod_link(posix.chmod, target, link, follow_symlinks=True)
1099
1100 @unittest.skipUnless(hasattr(posix, 'lchmod'), 'test needs os.lchmod()')
1101 @os_helper.skip_unless_symlink
1102 def test_lchmod_file_symlink(self):
1103 target = os_helper.TESTFN
1104 link = os_helper.TESTFN + '-link'
1105 os.symlink(target, link)
1106 self.addCleanup(posix.unlink, link)
1107 self.check_lchmod_link(posix.chmod, target, link, follow_symlinks=False)
1108 self.check_lchmod_link(posix.lchmod, target, link)
1109
1110 @unittest.skipUnless(hasattr(posix, 'lchmod'), 'test needs os.lchmod()')
1111 @os_helper.skip_unless_symlink
1112 def test_lchmod_dir_symlink(self):
1113 target = self.tempdir()
1114 link = os_helper.TESTFN + '-link'
1115 os.symlink(target, link)
1116 self.addCleanup(posix.unlink, link)
1117 self.check_lchmod_link(posix.chmod, target, link, follow_symlinks=False)
1118 self.check_lchmod_link(posix.lchmod, target, link)
1119
1120 def _test_chflags_regular_file(self, chflags_func, target_file, **kwargs):
1121 st = os.stat(target_file)
1122 self.assertHasAttr(st, 'st_flags')
1123
1124 # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE.
1125 flags = st.st_flags | stat.UF_IMMUTABLE
1126 try:
1127 chflags_func(target_file, flags, **kwargs)
1128 except OSError as err:
1129 if err.errno != errno.EOPNOTSUPP:
1130 raise
1131 msg = 'chflag UF_IMMUTABLE not supported by underlying fs'
1132 self.skipTest(msg)
1133
1134 try:
1135 new_st = os.stat(target_file)
1136 self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags)
1137 try:
1138 fd = open(target_file, 'w+')
1139 except OSError as e:
1140 self.assertEqual(e.errno, errno.EPERM)
1141 finally:
1142 posix.chflags(target_file, st.st_flags)
1143
1144 @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()')
1145 def test_chflags(self):
1146 self._test_chflags_regular_file(posix.chflags, os_helper.TESTFN)
1147
1148 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
1149 def test_lchflags_regular_file(self):
1150 self._test_chflags_regular_file(posix.lchflags, os_helper.TESTFN)
1151 self._test_chflags_regular_file(posix.chflags, os_helper.TESTFN,
1152 follow_symlinks=False)
1153
1154 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
1155 def test_lchflags_symlink(self):
1156 testfn_st = os.stat(os_helper.TESTFN)
1157
1158 self.assertHasAttr(testfn_st, 'st_flags')
1159
1160 self.addCleanup(os_helper.unlink, _DUMMY_SYMLINK)
1161 os.symlink(os_helper.TESTFN, _DUMMY_SYMLINK)
1162 dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
1163
1164 def chflags_nofollow(path, flags):
1165 return posix.chflags(path, flags, follow_symlinks=False)
1166
1167 for fn in (posix.lchflags, chflags_nofollow):
1168 # ZFS returns EOPNOTSUPP when attempting to set flag UF_IMMUTABLE.
1169 flags = dummy_symlink_st.st_flags | stat.UF_IMMUTABLE
1170 try:
1171 fn(_DUMMY_SYMLINK, flags)
1172 except OSError as err:
1173 if err.errno != errno.EOPNOTSUPP:
1174 raise
1175 msg = 'chflag UF_IMMUTABLE not supported by underlying fs'
1176 self.skipTest(msg)
1177 try:
1178 new_testfn_st = os.stat(os_helper.TESTFN)
1179 new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
1180
1181 self.assertEqual(testfn_st.st_flags, new_testfn_st.st_flags)
1182 self.assertEqual(dummy_symlink_st.st_flags | stat.UF_IMMUTABLE,
1183 new_dummy_symlink_st.st_flags)
1184 finally:
1185 fn(_DUMMY_SYMLINK, dummy_symlink_st.st_flags)
1186
1187 def test_environ(self):
1188 if os.name == "nt":
1189 item_type = str
1190 else:
1191 item_type = bytes
1192 for k, v in posix.environ.items():
1193 self.assertEqual(type(k), item_type)
1194 self.assertEqual(type(v), item_type)
1195
1196 def test_putenv(self):
1197 with self.assertRaises(ValueError):
1198 os.putenv('FRUIT\0VEGETABLE', 'cabbage')
1199 with self.assertRaises(ValueError):
1200 os.putenv('FRUIT', 'orange\0VEGETABLE=cabbage')
1201 with self.assertRaises(ValueError):
1202 os.putenv('FRUIT=ORANGE', 'lemon')
1203 if os.name == 'posix':
1204 with self.assertRaises(ValueError):
1205 os.putenv(b'FRUIT\0VEGETABLE', b'cabbage')
1206 with self.assertRaises(ValueError):
1207 os.putenv(b'FRUIT', b'orange\0VEGETABLE=cabbage')
1208 with self.assertRaises(ValueError):
1209 os.putenv(b'FRUIT=ORANGE', b'lemon')
1210
1211 @unittest.skipUnless(hasattr(posix, 'getcwd'), 'test needs posix.getcwd()')
1212 def test_getcwd_long_pathnames(self):
1213 dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
1214 curdir = os.getcwd()
1215 base_path = os.path.abspath(os_helper.TESTFN) + '.getcwd'
1216
1217 try:
1218 os.mkdir(base_path)
1219 os.chdir(base_path)
1220 except:
1221 # Just returning nothing instead of the SkipTest exception, because
1222 # the test results in Error in that case. Is that ok?
1223 # raise unittest.SkipTest("cannot create directory for testing")
1224 return
1225
1226 def _create_and_do_getcwd(dirname, current_path_length = 0):
1227 try:
1228 os.mkdir(dirname)
1229 except:
1230 raise unittest.SkipTest("mkdir cannot create directory sufficiently deep for getcwd test")
1231
1232 os.chdir(dirname)
1233 try:
1234 os.getcwd()
1235 if current_path_length < 1027:
1236 _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
1237 finally:
1238 os.chdir('..')
1239 os.rmdir(dirname)
1240
1241 _create_and_do_getcwd(dirname)
1242
1243 finally:
1244 os.chdir(curdir)
1245 os_helper.rmtree(base_path)
1246
1247 @unittest.skipUnless(hasattr(posix, 'getgrouplist'), "test needs posix.getgrouplist()")
1248 @unittest.skipUnless(hasattr(pwd, 'getpwuid'), "test needs pwd.getpwuid()")
1249 @unittest.skipUnless(hasattr(os, 'getuid'), "test needs os.getuid()")
1250 def test_getgrouplist(self):
1251 user = pwd.getpwuid(os.getuid())[0]
1252 group = pwd.getpwuid(os.getuid())[3]
1253 self.assertIn(group, posix.getgrouplist(user, group))
1254
1255
1256 @unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()")
1257 @unittest.skipUnless(hasattr(os, 'popen'), "test needs os.popen()")
1258 @support.requires_subprocess()
1259 def test_getgroups(self):
1260 with os.popen('id -G 2>/dev/null') as idg:
1261 groups = idg.read().strip()
1262 ret = idg.close()
1263
1264 try:
1265 idg_groups = set(int(g) for g in groups.split())
1266 except ValueError:
1267 idg_groups = set()
1268 if ret is not None or not idg_groups:
1269 raise unittest.SkipTest("need working 'id -G'")
1270
1271 # Issues 16698: OS X ABIs prior to 10.6 have limits on getgroups()
1272 if sys.platform == 'darwin':
1273 import sysconfig
1274 dt = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') or '10.3'
1275 if tuple(int(n) for n in dt.split('.')[0:2]) < (10, 6):
1276 raise unittest.SkipTest("getgroups(2) is broken prior to 10.6")
1277
1278 # 'id -G' and 'os.getgroups()' should return the same
1279 # groups, ignoring order, duplicates, and the effective gid.
1280 # #10822/#26944 - It is implementation defined whether
1281 # posix.getgroups() includes the effective gid.
1282 symdiff = idg_groups.symmetric_difference(posix.getgroups())
1283 self.assertTrue(not symdiff or symdiff == {posix.getegid()})
1284
1285 @unittest.skipUnless(hasattr(signal, 'SIGCHLD'), 'CLD_XXXX be placed in si_code for a SIGCHLD signal')
1286 @unittest.skipUnless(hasattr(os, 'waitid_result'), "test needs os.waitid_result")
1287 def test_cld_xxxx_constants(self):
1288 os.CLD_EXITED
1289 os.CLD_KILLED
1290 os.CLD_DUMPED
1291 os.CLD_TRAPPED
1292 os.CLD_STOPPED
1293 os.CLD_CONTINUED
1294
1295 requires_sched_h = unittest.skipUnless(hasattr(posix, 'sched_yield'),
1296 "don't have scheduling support")
1297 requires_sched_affinity = unittest.skipUnless(hasattr(posix, 'sched_setaffinity'),
1298 "don't have sched affinity support")
1299
1300 @requires_sched_h
1301 def test_sched_yield(self):
1302 # This has no error conditions (at least on Linux).
1303 posix.sched_yield()
1304
1305 @requires_sched_h
1306 @unittest.skipUnless(hasattr(posix, 'sched_get_priority_max'),
1307 "requires sched_get_priority_max()")
1308 def test_sched_priority(self):
1309 # Round-robin usually has interesting priorities.
1310 pol = posix.SCHED_RR
1311 lo = posix.sched_get_priority_min(pol)
1312 hi = posix.sched_get_priority_max(pol)
1313 self.assertIsInstance(lo, int)
1314 self.assertIsInstance(hi, int)
1315 self.assertGreaterEqual(hi, lo)
1316 # Apple platforms return 15 without checking the argument.
1317 if not is_apple:
1318 self.assertRaises(OSError, posix.sched_get_priority_min, -23)
1319 self.assertRaises(OSError, posix.sched_get_priority_max, -23)
1320
1321 @requires_sched
1322 def test_get_and_set_scheduler_and_param(self):
1323 possible_schedulers = [sched for name, sched in posix.__dict__.items()
1324 if name.startswith("SCHED_")]
1325 mine = posix.sched_getscheduler(0)
1326 self.assertIn(mine, possible_schedulers)
1327 try:
1328 parent = posix.sched_getscheduler(os.getppid())
1329 except PermissionError:
1330 # POSIX specifies EPERM, but Android returns EACCES. Both errno
1331 # values are mapped to PermissionError.
1332 pass
1333 else:
1334 self.assertIn(parent, possible_schedulers)
1335 self.assertRaises(OSError, posix.sched_getscheduler, -1)
1336 self.assertRaises(OSError, posix.sched_getparam, -1)
1337 param = posix.sched_getparam(0)
1338 self.assertIsInstance(param.sched_priority, int)
1339
1340 # POSIX states that calling sched_setparam() or sched_setscheduler() on
1341 # a process with a scheduling policy other than SCHED_FIFO or SCHED_RR
1342 # is implementation-defined: NetBSD and FreeBSD can return EINVAL.
1343 if not sys.platform.startswith(('freebsd', 'netbsd')):
1344 try:
1345 posix.sched_setscheduler(0, mine, param)
1346 posix.sched_setparam(0, param)
1347 except PermissionError:
1348 pass
1349 self.assertRaises(OSError, posix.sched_setparam, -1, param)
1350
1351 self.assertRaises(OSError, posix.sched_setscheduler, -1, mine, param)
1352 self.assertRaises(TypeError, posix.sched_setscheduler, 0, mine, None)
1353 self.assertRaises(TypeError, posix.sched_setparam, 0, 43)
1354 param = posix.sched_param(None)
1355 self.assertRaises(TypeError, posix.sched_setparam, 0, param)
1356 large = 214748364700
1357 param = posix.sched_param(large)
1358 self.assertRaises(OverflowError, posix.sched_setparam, 0, param)
1359 param = posix.sched_param(sched_priority=-large)
1360 self.assertRaises(OverflowError, posix.sched_setparam, 0, param)
1361
1362 @requires_sched
1363 def test_sched_param(self):
1364 param = posix.sched_param(1)
1365 for proto in range(pickle.HIGHEST_PROTOCOL+1):
1366 newparam = pickle.loads(pickle.dumps(param, proto))
1367 self.assertEqual(newparam, param)
1368 newparam = copy.copy(param)
1369 self.assertIsNot(newparam, param)
1370 self.assertEqual(newparam, param)
1371 newparam = copy.deepcopy(param)
1372 self.assertIsNot(newparam, param)
1373 self.assertEqual(newparam, param)
1374 newparam = copy.replace(param)
1375 self.assertIsNot(newparam, param)
1376 self.assertEqual(newparam, param)
1377 newparam = copy.replace(param, sched_priority=0)
1378 self.assertNotEqual(newparam, param)
1379 self.assertEqual(newparam.sched_priority, 0)
1380
1381 @requires_sched
1382 def test_bug_140634(self):
1383 sched_priority = float('inf') # any new reference
1384 param = posix.sched_param(sched_priority)
1385 param.__reduce__()
1386 del sched_priority, param # should not crash
1387 support.gc_collect() # just to be sure
1388
1389 @unittest.skipUnless(hasattr(posix, "sched_rr_get_interval"), "no function")
1390 def test_sched_rr_get_interval(self):
1391 try:
1392 interval = posix.sched_rr_get_interval(0)
1393 except OSError as e:
1394 # This likely means that sched_rr_get_interval is only valid for
1395 # processes with the SCHED_RR scheduler in effect.
1396 if e.errno != errno.EINVAL:
1397 raise
1398 self.skipTest("only works on SCHED_RR processes")
1399 self.assertIsInstance(interval, float)
1400 # Reasonable constraints, I think.
1401 self.assertGreaterEqual(interval, 0.)
1402 self.assertLess(interval, 1.)
1403
1404 @requires_sched_affinity
1405 def test_sched_getaffinity(self):
1406 mask = posix.sched_getaffinity(0)
1407 self.assertIsInstance(mask, set)
1408 self.assertGreaterEqual(len(mask), 1)
1409 if not sys.platform.startswith("freebsd"):
1410 # bpo-47205: does not raise OSError on FreeBSD
1411 self.assertRaises(OSError, posix.sched_getaffinity, -1)
1412 for cpu in mask:
1413 self.assertIsInstance(cpu, int)
1414 self.assertGreaterEqual(cpu, 0)
1415 self.assertLess(cpu, 1 << 32)
1416
1417 @requires_sched_affinity
1418 def test_sched_setaffinity(self):
1419 mask = posix.sched_getaffinity(0)
1420 self.addCleanup(posix.sched_setaffinity, 0, list(mask))
1421
1422 if len(mask) > 1:
1423 # Empty masks are forbidden
1424 mask.pop()
1425 posix.sched_setaffinity(0, mask)
1426 self.assertEqual(posix.sched_getaffinity(0), mask)
1427
1428 try:
1429 posix.sched_setaffinity(0, [])
1430 # gh-117061: On RHEL9, sched_setaffinity(0, []) does not fail
1431 except OSError:
1432 # sched_setaffinity() manual page documents EINVAL error
1433 # when the mask is empty.
1434 pass
1435
1436 self.assertRaises(ValueError, posix.sched_setaffinity, 0, [-10])
1437 self.assertRaises(ValueError, posix.sched_setaffinity, 0, map(int, "0X"))
1438 self.assertRaises(OverflowError, posix.sched_setaffinity, 0, [1<<128])
1439 if not sys.platform.startswith("freebsd"):
1440 # bpo-47205: does not raise OSError on FreeBSD
1441 self.assertRaises(OSError, posix.sched_setaffinity, -1, mask)
1442
1443 @unittest.skipIf(support.is_wasi, "No dynamic linking on WASI")
1444 @unittest.skipUnless(os.name == 'posix', "POSIX-only test")
1445 def test_rtld_constants(self):
1446 # check presence of major RTLD_* constants
1447 posix.RTLD_LAZY
1448 posix.RTLD_NOW
1449 posix.RTLD_GLOBAL
1450 posix.RTLD_LOCAL
1451
1452 @unittest.skipUnless(hasattr(os, 'SEEK_HOLE'),
1453 "test needs an OS that reports file holes")
1454 def test_fs_holes(self):
1455 # Even if the filesystem doesn't report holes,
1456 # if the OS supports it the SEEK_* constants
1457 # will be defined and will have a consistent
1458 # behaviour:
1459 # os.SEEK_DATA = current position
1460 # os.SEEK_HOLE = end of file position
1461 with open(os_helper.TESTFN, 'r+b') as fp:
1462 fp.write(b"hello")
1463 fp.flush()
1464 size = fp.tell()
1465 fno = fp.fileno()
1466 try :
1467 for i in range(size):
1468 self.assertEqual(i, os.lseek(fno, i, os.SEEK_DATA))
1469 self.assertLessEqual(size, os.lseek(fno, i, os.SEEK_HOLE))
1470 self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_DATA)
1471 self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_HOLE)
1472 except OSError :
1473 # Some OSs claim to support SEEK_HOLE/SEEK_DATA
1474 # but it is not true.
1475 # For instance:
1476 # http://lists.freebsd.org/pipermail/freebsd-amd64/2012-January/014332.html
1477 raise unittest.SkipTest("OSError raised!")
1478
1479 def test_path_error2(self):
1480 """
1481 Test functions that call path_error2(), providing two filenames in their exceptions.
1482 """
1483 for name in ("rename", "replace", "link"):
1484 function = getattr(os, name, None)
1485 if function is None:
1486 continue
1487
1488 for dst in ("noodly2", os_helper.TESTFN):
1489 try:
1490 function('doesnotexistfilename', dst)
1491 except OSError as e:
1492 self.assertIn("'doesnotexistfilename' -> '{}'".format(dst), str(e))
1493 break
1494 else:
1495 self.fail("No valid path_error2() test for os." + name)
1496
1497 def test_path_with_null_character(self):
1498 fn = os_helper.TESTFN
1499 fn_with_NUL = fn + '\0'
1500 self.addCleanup(os_helper.unlink, fn)
1501 os_helper.unlink(fn)
1502 fd = None
1503 try:
1504 with self.assertRaises(ValueError):
1505 fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises
1506 finally:
1507 if fd is not None:
1508 os.close(fd)
1509 self.assertFalse(os.path.exists(fn))
1510 self.assertRaises(ValueError, os.mkdir, fn_with_NUL)
1511 self.assertFalse(os.path.exists(fn))
1512 open(fn, 'wb').close()
1513 self.assertRaises(ValueError, os.stat, fn_with_NUL)
1514
1515 def test_path_with_null_byte(self):
1516 fn = os.fsencode(os_helper.TESTFN)
1517 fn_with_NUL = fn + b'\0'
1518 self.addCleanup(os_helper.unlink, fn)
1519 os_helper.unlink(fn)
1520 fd = None
1521 try:
1522 with self.assertRaises(ValueError):
1523 fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises
1524 finally:
1525 if fd is not None:
1526 os.close(fd)
1527 self.assertFalse(os.path.exists(fn))
1528 self.assertRaises(ValueError, os.mkdir, fn_with_NUL)
1529 self.assertFalse(os.path.exists(fn))
1530 open(fn, 'wb').close()
1531 self.assertRaises(ValueError, os.stat, fn_with_NUL)
1532
1533 @unittest.skipUnless(hasattr(os, "pidfd_open"), "pidfd_open unavailable")
1534 def test_pidfd_open(self):
1535 with self.assertRaises(OSError) as cm:
1536 os.pidfd_open(-1)
1537 if cm.exception.errno == errno.ENOSYS:
1538 self.skipTest("system does not support pidfd_open")
1539 if isinstance(cm.exception, PermissionError):
1540 self.skipTest(f"pidfd_open syscall blocked: {cm.exception!r}")
1541 self.assertEqual(cm.exception.errno, errno.EINVAL)
1542 os.close(os.pidfd_open(os.getpid(), 0))
1543
1544 @os_helper.skip_unless_hardlink
1545 @os_helper.skip_unless_symlink
1546 def test_link_follow_symlinks(self):
1547 default_follow = sys.platform.startswith(
1548 ('darwin', 'freebsd', 'netbsd', 'openbsd', 'dragonfly', 'sunos5'))
1549 default_no_follow = sys.platform.startswith(('win32', 'linux'))
1550 orig = os_helper.TESTFN
1551 symlink = orig + 'symlink'
1552 posix.symlink(orig, symlink)
1553 self.addCleanup(os_helper.unlink, symlink)
1554
1555 with self.subTest('no follow_symlinks'):
1556 # no follow_symlinks -> platform depending
1557 link = orig + 'link'
1558 posix.link(symlink, link)
1559 self.addCleanup(os_helper.unlink, link)
1560 if os.link in os.supports_follow_symlinks or default_follow:
1561 self.assertEqual(posix.lstat(link), posix.lstat(orig))
1562 elif default_no_follow:
1563 self.assertEqual(posix.lstat(link), posix.lstat(symlink))
1564
1565 with self.subTest('follow_symlinks=False'):
1566 # follow_symlinks=False -> duplicate the symlink itself
1567 link = orig + 'link_nofollow'
1568 try:
1569 posix.link(symlink, link, follow_symlinks=False)
1570 except NotImplementedError:
1571 if os.link in os.supports_follow_symlinks or default_no_follow:
1572 raise
1573 else:
1574 self.addCleanup(os_helper.unlink, link)
1575 self.assertEqual(posix.lstat(link), posix.lstat(symlink))
1576
1577 with self.subTest('follow_symlinks=True'):
1578 # follow_symlinks=True -> duplicate the target file
1579 link = orig + 'link_following'
1580 try:
1581 posix.link(symlink, link, follow_symlinks=True)
1582 except NotImplementedError:
1583 if os.link in os.supports_follow_symlinks or default_follow:
1584 raise
1585 else:
1586 self.addCleanup(os_helper.unlink, link)
1587 self.assertEqual(posix.lstat(link), posix.lstat(orig))
1588
1589
1590 # tests for the posix *at functions follow
1591 class TestPosixDirFd(unittest.TestCase):
1592 count = 0
1593
1594 @contextmanager
1595 def prepare(self):
1596 TestPosixDirFd.count += 1
1597 name = f'{os_helper.TESTFN}_{self.count}'
1598 base_dir = f'{os_helper.TESTFN}_{self.count}base'
1599 posix.mkdir(base_dir)
1600 self.addCleanup(posix.rmdir, base_dir)
1601 fullname = os.path.join(base_dir, name)
1602 assert not os.path.exists(fullname)
1603 with os_helper.open_dir_fd(base_dir) as dir_fd:
1604 yield (dir_fd, name, fullname)
1605
1606 @contextmanager
1607 def prepare_file(self):
1608 with self.prepare() as (dir_fd, name, fullname):
1609 os_helper.create_empty_file(fullname)
1610 self.addCleanup(posix.unlink, fullname)
1611 yield (dir_fd, name, fullname)
1612
1613 @unittest.skipUnless(os.access in os.supports_dir_fd, "test needs dir_fd support for os.access()")
1614 def test_access_dir_fd(self):
1615 with self.prepare_file() as (dir_fd, name, fullname):
1616 self.assertTrue(posix.access(name, os.R_OK, dir_fd=dir_fd))
1617
1618 @unittest.skipUnless(os.chmod in os.supports_dir_fd, "test needs dir_fd support in os.chmod()")
1619 def test_chmod_dir_fd(self):
1620 with self.prepare_file() as (dir_fd, name, fullname):
1621 posix.chmod(fullname, stat.S_IRUSR)
1622 posix.chmod(name, stat.S_IRUSR | stat.S_IWUSR, dir_fd=dir_fd)
1623 s = posix.stat(fullname)
1624 self.assertEqual(s.st_mode & stat.S_IRWXU,
1625 stat.S_IRUSR | stat.S_IWUSR)
1626
1627 @unittest.skipUnless(hasattr(os, 'chown') and (os.chown in os.supports_dir_fd),
1628 "test needs dir_fd support in os.chown()")
1629 @unittest.skipIf(support.is_emscripten, "getgid() is a stub")
1630 def test_chown_dir_fd(self):
1631 with self.prepare_file() as (dir_fd, name, fullname):
1632 posix.chown(name, os.getuid(), os.getgid(), dir_fd=dir_fd)
1633
1634 @unittest.skipUnless(os.stat in os.supports_dir_fd, "test needs dir_fd support in os.stat()")
1635 def test_stat_dir_fd(self):
1636 with self.prepare() as (dir_fd, name, fullname):
1637 with open(fullname, 'w') as outfile:
1638 outfile.write("testline\n")
1639 self.addCleanup(posix.unlink, fullname)
1640
1641 s1 = posix.stat(fullname)
1642 s2 = posix.stat(name, dir_fd=dir_fd)
1643 self.assertEqual(s1, s2)
1644 s2 = posix.stat(fullname, dir_fd=None)
1645 self.assertEqual(s1, s2)
1646
1647 self.assertRaisesRegex(TypeError, 'should be integer or None, not',
1648 posix.stat, name, dir_fd=posix.getcwd())
1649 self.assertRaisesRegex(TypeError, 'should be integer or None, not',
1650 posix.stat, name, dir_fd=float(dir_fd))
1651 self.assertRaises(OverflowError,
1652 posix.stat, name, dir_fd=10**20)
1653
1654 for fd in False, True:
1655 with self.assertWarnsRegex(RuntimeWarning,
1656 'bool is used as a file descriptor') as cm:
1657 with self.assertRaises(OSError):
1658 posix.stat('nonexisting', dir_fd=fd)
1659 self.assertEqual(cm.filename, __file__)
1660
1661 @unittest.skipUnless(os.utime in os.supports_dir_fd, "test needs dir_fd support in os.utime()")
1662 def test_utime_dir_fd(self):
1663 with self.prepare_file() as (dir_fd, name, fullname):
1664 now = time.time()
1665 posix.utime(name, None, dir_fd=dir_fd)
1666 posix.utime(name, dir_fd=dir_fd)
1667 self.assertRaises(TypeError, posix.utime, name,
1668 now, dir_fd=dir_fd)
1669 self.assertRaises(TypeError, posix.utime, name,
1670 (None, None), dir_fd=dir_fd)
1671 self.assertRaises(TypeError, posix.utime, name,
1672 (now, None), dir_fd=dir_fd)
1673 self.assertRaises(TypeError, posix.utime, name,
1674 (None, now), dir_fd=dir_fd)
1675 self.assertRaises(TypeError, posix.utime, name,
1676 (now, "x"), dir_fd=dir_fd)
1677 posix.utime(name, (int(now), int(now)), dir_fd=dir_fd)
1678 posix.utime(name, (now, now), dir_fd=dir_fd)
1679 posix.utime(name,
1680 (int(now), int((now - int(now)) * 1e9)), dir_fd=dir_fd)
1681 posix.utime(name, dir_fd=dir_fd,
1682 times=(int(now), int((now - int(now)) * 1e9)))
1683
1684 # try dir_fd and follow_symlinks together
1685 if os.utime in os.supports_follow_symlinks:
1686 try:
1687 posix.utime(name, follow_symlinks=False, dir_fd=dir_fd)
1688 except ValueError:
1689 # whoops! using both together not supported on this platform.
1690 pass
1691
1692 @unittest.skipIf(
1693 support.is_wasi,
1694 "WASI: symlink following on path_link is not supported"
1695 )
1696 @unittest.skipUnless(
1697 hasattr(os, "link") and os.link in os.supports_dir_fd,
1698 "test needs dir_fd support in os.link()"
1699 )
1700 def test_link_dir_fd(self):
1701 with self.prepare_file() as (dir_fd, name, fullname), \
1702 self.prepare() as (dir_fd2, linkname, fulllinkname):
1703 try:
1704 posix.link(name, linkname, src_dir_fd=dir_fd, dst_dir_fd=dir_fd2)
1705 except PermissionError as e:
1706 self.skipTest('posix.link(): %s' % e)
1707 self.addCleanup(posix.unlink, fulllinkname)
1708 # should have same inodes
1709 self.assertEqual(posix.stat(fullname)[1],
1710 posix.stat(fulllinkname)[1])
1711
1712 @unittest.skipUnless(os.mkdir in os.supports_dir_fd, "test needs dir_fd support in os.mkdir()")
1713 def test_mkdir_dir_fd(self):
1714 with self.prepare() as (dir_fd, name, fullname):
1715 posix.mkdir(name, dir_fd=dir_fd)
1716 self.addCleanup(posix.rmdir, fullname)
1717 posix.stat(fullname) # should not raise exception
1718
1719 @unittest.skipUnless(hasattr(os, 'mknod')
1720 and (os.mknod in os.supports_dir_fd)
1721 and hasattr(stat, 'S_IFIFO'),
1722 "test requires both stat.S_IFIFO and dir_fd support for os.mknod()")
1723 def test_mknod_dir_fd(self):
1724 # Test using mknodat() to create a FIFO (the only use specified
1725 # by POSIX).
1726 with self.prepare() as (dir_fd, name, fullname):
1727 mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR
1728 try:
1729 posix.mknod(name, mode, 0, dir_fd=dir_fd)
1730 except OSError as e:
1731 # Some old systems don't allow unprivileged users to use
1732 # mknod(), or only support creating device nodes.
1733 self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES))
1734 else:
1735 self.addCleanup(posix.unlink, fullname)
1736 self.assertTrue(stat.S_ISFIFO(posix.stat(fullname).st_mode))
1737
1738 @unittest.skipUnless(os.open in os.supports_dir_fd, "test needs dir_fd support in os.open()")
1739 def test_open_dir_fd(self):
1740 with self.prepare() as (dir_fd, name, fullname):
1741 with open(fullname, 'wb') as outfile:
1742 outfile.write(b"testline\n")
1743 self.addCleanup(posix.unlink, fullname)
1744 fd = posix.open(name, posix.O_RDONLY, dir_fd=dir_fd)
1745 try:
1746 res = posix.read(fd, 9)
1747 self.assertEqual(b"testline\n", res)
1748 finally:
1749 posix.close(fd)
1750
1751 @unittest.skipUnless(hasattr(os, 'readlink') and (os.readlink in os.supports_dir_fd),
1752 "test needs dir_fd support in os.readlink()")
1753 def test_readlink_dir_fd(self):
1754 with self.prepare() as (dir_fd, name, fullname):
1755 os.symlink('symlink', fullname)
1756 self.addCleanup(posix.unlink, fullname)
1757 self.assertEqual(posix.readlink(name, dir_fd=dir_fd), 'symlink')
1758
1759 @unittest.skipUnless(os.rename in os.supports_dir_fd, "test needs dir_fd support in os.rename()")
1760 def test_rename_dir_fd(self):
1761 with self.prepare_file() as (dir_fd, name, fullname), \
1762 self.prepare() as (dir_fd2, name2, fullname2):
1763 posix.rename(name, name2,
1764 src_dir_fd=dir_fd, dst_dir_fd=dir_fd2)
1765 posix.stat(fullname2) # should not raise exception
1766 posix.rename(fullname2, fullname)
1767
1768 @unittest.skipUnless(os.symlink in os.supports_dir_fd, "test needs dir_fd support in os.symlink()")
1769 def test_symlink_dir_fd(self):
1770 with self.prepare() as (dir_fd, name, fullname):
1771 posix.symlink('symlink', name, dir_fd=dir_fd)
1772 self.addCleanup(posix.unlink, fullname)
1773 self.assertEqual(posix.readlink(fullname), 'symlink')
1774
1775 @unittest.skipUnless(os.unlink in os.supports_dir_fd, "test needs dir_fd support in os.unlink()")
1776 def test_unlink_dir_fd(self):
1777 with self.prepare() as (dir_fd, name, fullname):
1778 os_helper.create_empty_file(fullname)
1779 posix.stat(fullname) # should not raise exception
1780 try:
1781 posix.unlink(name, dir_fd=dir_fd)
1782 self.assertRaises(OSError, posix.stat, fullname)
1783 except:
1784 self.addCleanup(posix.unlink, fullname)
1785 raise
1786
1787 @unittest.skipUnless(hasattr(os, 'mkfifo') and os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()")
1788 def test_mkfifo_dir_fd(self):
1789 with self.prepare() as (dir_fd, name, fullname):
1790 try:
1791 posix.mkfifo(name, stat.S_IRUSR | stat.S_IWUSR, dir_fd=dir_fd)
1792 except PermissionError as e:
1793 self.skipTest('posix.mkfifo(): %s' % e)
1794 self.addCleanup(posix.unlink, fullname)
1795 self.assertTrue(stat.S_ISFIFO(posix.stat(fullname).st_mode))
1796
1797
1798 class PosixGroupsTester(unittest.TestCase):
1799
1800 def setUp(self):
1801 if posix.getuid() != 0:
1802 raise unittest.SkipTest("not enough privileges")
1803 if not hasattr(posix, 'getgroups'):
1804 raise unittest.SkipTest("need posix.getgroups")
1805 if sys.platform == 'darwin':
1806 raise unittest.SkipTest("getgroups(2) is broken on OSX")
1807 self.saved_groups = posix.getgroups()
1808
1809 def tearDown(self):
1810 if hasattr(posix, 'setgroups'):
1811 posix.setgroups(self.saved_groups)
1812 elif hasattr(posix, 'initgroups'):
1813 name = pwd.getpwuid(posix.getuid()).pw_name
1814 posix.initgroups(name, self.saved_groups[0])
1815
1816 @unittest.skipUnless(hasattr(posix, 'initgroups'),
1817 "test needs posix.initgroups()")
1818 def test_initgroups(self):
1819 # find missing group
1820
1821 g = max(self.saved_groups or [0]) + 1
1822 name = pwd.getpwuid(posix.getuid()).pw_name
1823 posix.initgroups(name, g)
1824 self.assertIn(g, posix.getgroups())
1825
1826 @unittest.skipUnless(hasattr(posix, 'setgroups'),
1827 "test needs posix.setgroups()")
1828 def test_setgroups(self):
1829 for groups in [[0], list(range(16))]:
1830 posix.setgroups(groups)
1831 self.assertListEqual(groups, posix.getgroups())
1832
1833
1834 class _PosixSpawnMixin:
1835 # Program which does nothing and exits with status 0 (success)
1836 NOOP_PROGRAM = (sys.executable, '-I', '-S', '-c', 'pass')
1837 spawn_func = None
1838
1839 def python_args(self, *args):
1840 # Disable site module to avoid side effects. For example,
1841 # on Fedora 28, if the HOME environment variable is not set,
1842 # site._getuserbase() calls pwd.getpwuid() which opens
1843 # /var/lib/sss/mc/passwd but then leaves the file open which makes
1844 # test_close_file() to fail.
1845 return (sys.executable, '-I', '-S', *args)
1846
1847 def test_returns_pid(self):
1848 pidfile = os_helper.TESTFN
1849 self.addCleanup(os_helper.unlink, pidfile)
1850 script = f"""if 1:
1851 import os
1852 with open({pidfile!r}, "w") as pidfile:
1853 pidfile.write(str(os.getpid()))
1854 """
1855 args = self.python_args('-c', script)
1856 pid = self.spawn_func(args[0], args, os.environ)
1857 support.wait_process(pid, exitcode=0)
1858 with open(pidfile, encoding="utf-8") as f:
1859 self.assertEqual(f.read(), str(pid))
1860
1861 def test_no_such_executable(self):
1862 no_such_executable = 'no_such_executable'
1863 try:
1864 pid = self.spawn_func(no_such_executable,
1865 [no_such_executable],
1866 os.environ)
1867 # bpo-35794: PermissionError can be raised if there are
1868 # directories in the $PATH that are not accessible.
1869 except (FileNotFoundError, PermissionError) as exc:
1870 self.assertEqual(exc.filename, no_such_executable)
1871 else:
1872 pid2, status = os.waitpid(pid, 0)
1873 self.assertEqual(pid2, pid)
1874 self.assertNotEqual(status, 0)
1875
1876 def test_specify_environment(self):
1877 envfile = os_helper.TESTFN
1878 self.addCleanup(os_helper.unlink, envfile)
1879 script = f"""if 1:
1880 import os
1881 with open({envfile!r}, "w", encoding="utf-8") as envfile:
1882 envfile.write(os.environ['foo'])
1883 """
1884 args = self.python_args('-c', script)
1885 pid = self.spawn_func(args[0], args,
1886 {**os.environ, 'foo': 'bar'})
1887 support.wait_process(pid, exitcode=0)
1888 with open(envfile, encoding="utf-8") as f:
1889 self.assertEqual(f.read(), 'bar')
1890
1891 def test_none_file_actions(self):
1892 pid = self.spawn_func(
1893 self.NOOP_PROGRAM[0],
1894 self.NOOP_PROGRAM,
1895 os.environ,
1896 file_actions=None
1897 )
1898 support.wait_process(pid, exitcode=0)
1899
1900 def test_empty_file_actions(self):
1901 pid = self.spawn_func(
1902 self.NOOP_PROGRAM[0],
1903 self.NOOP_PROGRAM,
1904 os.environ,
1905 file_actions=[]
1906 )
1907 support.wait_process(pid, exitcode=0)
1908
1909 def test_resetids_explicit_default(self):
1910 pid = self.spawn_func(
1911 sys.executable,
1912 [sys.executable, '-c', 'pass'],
1913 os.environ,
1914 resetids=False
1915 )
1916 support.wait_process(pid, exitcode=0)
1917
1918 def test_resetids(self):
1919 pid = self.spawn_func(
1920 sys.executable,
1921 [sys.executable, '-c', 'pass'],
1922 os.environ,
1923 resetids=True
1924 )
1925 support.wait_process(pid, exitcode=0)
1926
1927 def test_setpgroup(self):
1928 pid = self.spawn_func(
1929 sys.executable,
1930 [sys.executable, '-c', 'pass'],
1931 os.environ,
1932 setpgroup=os.getpgrp()
1933 )
1934 support.wait_process(pid, exitcode=0)
1935
1936 def test_setpgroup_allow_none(self):
1937 path, args = self.NOOP_PROGRAM[0], self.NOOP_PROGRAM
1938 pid = self.spawn_func(path, args, os.environ, setpgroup=None)
1939 support.wait_process(pid, exitcode=0)
1940
1941 def test_setpgroup_wrong_type(self):
1942 with self.assertRaises(TypeError):
1943 self.spawn_func(sys.executable,
1944 [sys.executable, "-c", "pass"],
1945 os.environ, setpgroup="023")
1946
1947 @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'),
1948 'need signal.pthread_sigmask()')
1949 def test_setsigmask(self):
1950 code = textwrap.dedent("""\
1951 import signal
1952 signal.raise_signal(signal.SIGUSR1)""")
1953
1954 pid = self.spawn_func(
1955 sys.executable,
1956 [sys.executable, '-c', code],
1957 os.environ,
1958 setsigmask=[signal.SIGUSR1]
1959 )
1960 support.wait_process(pid, exitcode=0)
1961
1962 def test_setsigmask_wrong_type(self):
1963 with self.assertRaises(TypeError):
1964 self.spawn_func(sys.executable,
1965 [sys.executable, "-c", "pass"],
1966 os.environ, setsigmask=34)
1967 with self.assertRaises(TypeError):
1968 self.spawn_func(sys.executable,
1969 [sys.executable, "-c", "pass"],
1970 os.environ, setsigmask=["j"])
1971 with self.assertRaises(ValueError):
1972 self.spawn_func(sys.executable,
1973 [sys.executable, "-c", "pass"],
1974 os.environ, setsigmask=[signal.NSIG,
1975 signal.NSIG+1])
1976
1977 def test_setsid(self):
1978 rfd, wfd = os.pipe()
1979 self.addCleanup(os.close, rfd)
1980 try:
1981 os.set_inheritable(wfd, True)
1982
1983 code = textwrap.dedent(f"""
1984 import os
1985 fd = {wfd}
1986 sid = os.getsid(0)
1987 os.write(fd, str(sid).encode())
1988 """)
1989
1990 try:
1991 pid = self.spawn_func(sys.executable,
1992 [sys.executable, "-c", code],
1993 os.environ, setsid=True)
1994 except NotImplementedError as exc:
1995 self.skipTest(f"setsid is not supported: {exc!r}")
1996 except PermissionError as exc:
1997 self.skipTest(f"setsid failed with: {exc!r}")
1998 finally:
1999 os.close(wfd)
2000
2001 support.wait_process(pid, exitcode=0)
2002
2003 output = os.read(rfd, 100)
2004 child_sid = int(output)
2005 parent_sid = os.getsid(os.getpid())
2006 self.assertNotEqual(parent_sid, child_sid)
2007
2008 @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'),
2009 'need signal.pthread_sigmask()')
2010 def test_setsigdef(self):
2011 original_handler = signal.signal(signal.SIGUSR1, signal.SIG_IGN)
2012 code = textwrap.dedent("""\
2013 import signal
2014 signal.raise_signal(signal.SIGUSR1)""")
2015 try:
2016 pid = self.spawn_func(
2017 sys.executable,
2018 [sys.executable, '-c', code],
2019 os.environ,
2020 setsigdef=[signal.SIGUSR1]
2021 )
2022 finally:
2023 signal.signal(signal.SIGUSR1, original_handler)
2024
2025 support.wait_process(pid, exitcode=-signal.SIGUSR1)
2026
2027 def test_setsigdef_wrong_type(self):
2028 with self.assertRaises(TypeError):
2029 self.spawn_func(sys.executable,
2030 [sys.executable, "-c", "pass"],
2031 os.environ, setsigdef=34)
2032 with self.assertRaises(TypeError):
2033 self.spawn_func(sys.executable,
2034 [sys.executable, "-c", "pass"],
2035 os.environ, setsigdef=["j"])
2036 with self.assertRaises(ValueError):
2037 self.spawn_func(sys.executable,
2038 [sys.executable, "-c", "pass"],
2039 os.environ, setsigdef=[signal.NSIG, signal.NSIG+1])
2040
2041 def test_scheduler_allow_none(self):
2042 path, args = self.NOOP_PROGRAM[0], self.NOOP_PROGRAM
2043 pid = self.spawn_func(path, args, os.environ, scheduler=None)
2044 support.wait_process(pid, exitcode=0)
2045
2046 @support.subTests("scheduler", [object(), 1, [1, 2]])
2047 def test_scheduler_wrong_type(self, scheduler):
2048 path, args = self.NOOP_PROGRAM[0], self.NOOP_PROGRAM
2049 with self.assertRaisesRegex(
2050 TypeError,
2051 "scheduler must be a tuple or None",
2052 ):
2053 self.spawn_func(path, args, os.environ, scheduler=scheduler)
2054
2055 @requires_sched
2056 @unittest.skipIf(sys.platform.startswith(('freebsd', 'netbsd')),
2057 "bpo-34685: test can fail on BSD")
2058 def test_setscheduler_only_param(self):
2059 policy = os.sched_getscheduler(0)
2060 priority = os.sched_get_priority_min(policy)
2061 code = textwrap.dedent(f"""\
2062 import os, sys
2063 if os.sched_getscheduler(0) != {policy}:
2064 sys.exit(101)
2065 if os.sched_getparam(0).sched_priority != {priority}:
2066 sys.exit(102)""")
2067 pid = self.spawn_func(
2068 sys.executable,
2069 [sys.executable, '-c', code],
2070 os.environ,
2071 scheduler=(None, os.sched_param(priority))
2072 )
2073 support.wait_process(pid, exitcode=0)
2074
2075 @requires_sched
2076 @unittest.skipIf(sys.platform.startswith(('freebsd', 'netbsd')),
2077 "bpo-34685: test can fail on BSD")
2078 @unittest.skipIf(platform.libc_ver()[0] == 'glibc' and
2079 os.sched_getscheduler(0) in [
2080 os.SCHED_BATCH,
2081 os.SCHED_IDLE,
2082 os.SCHED_DEADLINE],
2083 "Skip test due to glibc posix_spawn policy")
2084 def test_setscheduler_with_policy(self):
2085 policy = os.sched_getscheduler(0)
2086 priority = os.sched_get_priority_min(policy)
2087 code = textwrap.dedent(f"""\
2088 import os, sys
2089 if os.sched_getscheduler(0) != {policy}:
2090 sys.exit(101)
2091 if os.sched_getparam(0).sched_priority != {priority}:
2092 sys.exit(102)""")
2093 pid = self.spawn_func(
2094 sys.executable,
2095 [sys.executable, '-c', code],
2096 os.environ,
2097 scheduler=(policy, os.sched_param(priority))
2098 )
2099 support.wait_process(pid, exitcode=0)
2100
2101 def test_multiple_file_actions(self):
2102 file_actions = [
2103 (os.POSIX_SPAWN_OPEN, 3, os.path.realpath(__file__), os.O_RDONLY, 0),
2104 (os.POSIX_SPAWN_CLOSE, 0),
2105 (os.POSIX_SPAWN_DUP2, 1, 4),
2106 ]
2107 pid = self.spawn_func(self.NOOP_PROGRAM[0],
2108 self.NOOP_PROGRAM,
2109 os.environ,
2110 file_actions=file_actions)
2111 support.wait_process(pid, exitcode=0)
2112
2113 def test_bad_file_actions(self):
2114 args = self.NOOP_PROGRAM
2115 with self.assertRaises(TypeError):
2116 self.spawn_func(args[0], args, os.environ,
2117 file_actions=[None])
2118 with self.assertRaises(TypeError):
2119 self.spawn_func(args[0], args, os.environ,
2120 file_actions=[()])
2121 with self.assertRaises(TypeError):
2122 self.spawn_func(args[0], args, os.environ,
2123 file_actions=[(None,)])
2124 with self.assertRaises(TypeError):
2125 self.spawn_func(args[0], args, os.environ,
2126 file_actions=[(12345,)])
2127 with self.assertRaises(TypeError):
2128 self.spawn_func(args[0], args, os.environ,
2129 file_actions=[(os.POSIX_SPAWN_CLOSE,)])
2130 with self.assertRaises(TypeError):
2131 self.spawn_func(args[0], args, os.environ,
2132 file_actions=[(os.POSIX_SPAWN_CLOSE, 1, 2)])
2133 with self.assertRaises(TypeError):
2134 self.spawn_func(args[0], args, os.environ,
2135 file_actions=[(os.POSIX_SPAWN_CLOSE, None)])
2136 with self.assertRaises(ValueError):
2137 self.spawn_func(args[0], args, os.environ,
2138 file_actions=[(os.POSIX_SPAWN_OPEN,
2139 3, __file__ + '\0',
2140 os.O_RDONLY, 0)])
2141
2142 def test_open_file(self):
2143 outfile = os_helper.TESTFN
2144 self.addCleanup(os_helper.unlink, outfile)
2145 script = """if 1:
2146 import sys
2147 sys.stdout.write("hello")
2148 """
2149 file_actions = [
2150 (os.POSIX_SPAWN_OPEN, 1, outfile,
2151 os.O_WRONLY | os.O_CREAT | os.O_TRUNC,
2152 stat.S_IRUSR | stat.S_IWUSR),
2153 ]
2154 args = self.python_args('-c', script)
2155 pid = self.spawn_func(args[0], args, os.environ,
2156 file_actions=file_actions)
2157
2158 support.wait_process(pid, exitcode=0)
2159 with open(outfile, encoding="utf-8") as f:
2160 self.assertEqual(f.read(), 'hello')
2161
2162 def test_close_file(self):
2163 closefile = os_helper.TESTFN
2164 self.addCleanup(os_helper.unlink, closefile)
2165 script = f"""if 1:
2166 import os
2167 try:
2168 os.fstat(0)
2169 except OSError as e:
2170 with open({closefile!r}, 'w', encoding='utf-8') as closefile:
2171 closefile.write('is closed %d' % e.errno)
2172 """
2173 args = self.python_args('-c', script)
2174 pid = self.spawn_func(args[0], args, os.environ,
2175 file_actions=[(os.POSIX_SPAWN_CLOSE, 0)])
2176
2177 support.wait_process(pid, exitcode=0)
2178 with open(closefile, encoding="utf-8") as f:
2179 self.assertEqual(f.read(), 'is closed %d' % errno.EBADF)
2180
2181 def test_dup2(self):
2182 dupfile = os_helper.TESTFN
2183 self.addCleanup(os_helper.unlink, dupfile)
2184 script = """if 1:
2185 import sys
2186 sys.stdout.write("hello")
2187 """
2188 with open(dupfile, "wb") as childfile:
2189 file_actions = [
2190 (os.POSIX_SPAWN_DUP2, childfile.fileno(), 1),
2191 ]
2192 args = self.python_args('-c', script)
2193 pid = self.spawn_func(args[0], args, os.environ,
2194 file_actions=file_actions)
2195 support.wait_process(pid, exitcode=0)
2196 with open(dupfile, encoding="utf-8") as f:
2197 self.assertEqual(f.read(), 'hello')
2198
2199
2200 @unittest.skipUnless(hasattr(os, 'posix_spawn'), "test needs os.posix_spawn")
2201 @support.requires_subprocess()
2202 class TestPosixSpawn(unittest.TestCase, _PosixSpawnMixin):
2203 spawn_func = getattr(posix, 'posix_spawn', None)
2204
2205
2206 @unittest.skipUnless(hasattr(os, 'posix_spawnp'), "test needs os.posix_spawnp")
2207 @support.requires_subprocess()
2208 class TestPosixSpawnP(unittest.TestCase, _PosixSpawnMixin):
2209 spawn_func = getattr(posix, 'posix_spawnp', None)
2210
2211 @os_helper.skip_unless_symlink
2212 def test_posix_spawnp(self):
2213 # Use a symlink to create a program in its own temporary directory
2214 temp_dir = tempfile.mkdtemp()
2215 self.addCleanup(os_helper.rmtree, temp_dir)
2216
2217 program = 'posix_spawnp_test_program.exe'
2218 program_fullpath = os.path.join(temp_dir, program)
2219 os.symlink(sys.executable, program_fullpath)
2220
2221 try:
2222 path = os.pathsep.join((temp_dir, os.environ['PATH']))
2223 except KeyError:
2224 path = temp_dir # PATH is not set
2225
2226 spawn_args = (program, '-I', '-S', '-c', 'pass')
2227 code = textwrap.dedent("""
2228 import os
2229 from test import support
2230
2231 args = %a
2232 pid = os.posix_spawnp(args[0], args, os.environ)
2233
2234 support.wait_process(pid, exitcode=0)
2235 """ % (spawn_args,))
2236
2237 # Use a subprocess to test os.posix_spawnp() with a modified PATH
2238 # environment variable: posix_spawnp() uses the current environment
2239 # to locate the program, not its environment argument.
2240 args = ('-c', code)
2241 assert_python_ok(*args, PATH=path)
2242
2243
2244 @unittest.skipUnless(sys.platform == "darwin", "test weak linking on macOS")
2245 class TestPosixWeaklinking(unittest.TestCase):
2246 # These test cases verify that weak linking support on macOS works
2247 # as expected. These cases only test new behaviour introduced by weak linking,
2248 # regular behaviour is tested by the normal test cases.
2249 #
2250 # See the section on Weak Linking in Mac/README.txt for more information.
2251 def setUp(self):
2252 import sysconfig
2253 import platform
2254
2255 config_vars = sysconfig.get_config_vars()
2256 self.available = { nm for nm in config_vars if nm.startswith("HAVE_") and config_vars[nm] }
2257 self.mac_ver = tuple(int(part) for part in platform.mac_ver()[0].split("."))
2258
2259 def _verify_available(self, name):
2260 if name not in self.available:
2261 raise unittest.SkipTest(f"{name} not weak-linked")
2262
2263 def test_pwritev(self):
2264 self._verify_available("HAVE_PWRITEV")
2265 if self.mac_ver >= (10, 16):
2266 self.assertHasAttr(os, "pwritev")
2267 self.assertHasAttr(os, "preadv")
2268
2269 else:
2270 self.assertNotHasAttr(os, "pwritev")
2271 self.assertNotHasAttr(os, "preadv")
2272
2273 def test_stat(self):
2274 self._verify_available("HAVE_FSTATAT")
2275 if self.mac_ver >= (10, 10):
2276 self.assertIn("HAVE_FSTATAT", posix._have_functions)
2277
2278 else:
2279 self.assertNotIn("HAVE_FSTATAT", posix._have_functions)
2280
2281 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2282 os.stat("file", dir_fd=0)
2283
2284 def test_ptsname_r(self):
2285 self._verify_available("HAVE_PTSNAME_R")
2286 if self.mac_ver >= (10, 13, 4):
2287 self.assertIn("HAVE_PTSNAME_R", posix._have_functions)
2288 else:
2289 self.assertNotIn("HAVE_PTSNAME_R", posix._have_functions)
2290
2291 def test_access(self):
2292 self._verify_available("HAVE_FACCESSAT")
2293 if self.mac_ver >= (10, 10):
2294 self.assertIn("HAVE_FACCESSAT", posix._have_functions)
2295
2296 else:
2297 self.assertNotIn("HAVE_FACCESSAT", posix._have_functions)
2298
2299 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2300 os.access("file", os.R_OK, dir_fd=0)
2301
2302 with self.assertRaisesRegex(NotImplementedError, "follow_symlinks unavailable"):
2303 os.access("file", os.R_OK, follow_symlinks=False)
2304
2305 with self.assertRaisesRegex(NotImplementedError, "effective_ids unavailable"):
2306 os.access("file", os.R_OK, effective_ids=True)
2307
2308 def test_chmod(self):
2309 self._verify_available("HAVE_FCHMODAT")
2310 if self.mac_ver >= (10, 10):
2311 self.assertIn("HAVE_FCHMODAT", posix._have_functions)
2312
2313 else:
2314 self.assertNotIn("HAVE_FCHMODAT", posix._have_functions)
2315 self.assertIn("HAVE_LCHMOD", posix._have_functions)
2316
2317 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2318 os.chmod("file", 0o644, dir_fd=0)
2319
2320 def test_chown(self):
2321 self._verify_available("HAVE_FCHOWNAT")
2322 if self.mac_ver >= (10, 10):
2323 self.assertIn("HAVE_FCHOWNAT", posix._have_functions)
2324
2325 else:
2326 self.assertNotIn("HAVE_FCHOWNAT", posix._have_functions)
2327 self.assertIn("HAVE_LCHOWN", posix._have_functions)
2328
2329 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2330 os.chown("file", 0, 0, dir_fd=0)
2331
2332 def test_link(self):
2333 self._verify_available("HAVE_LINKAT")
2334 if self.mac_ver >= (10, 10):
2335 self.assertIn("HAVE_LINKAT", posix._have_functions)
2336
2337 else:
2338 self.assertNotIn("HAVE_LINKAT", posix._have_functions)
2339
2340 with self.assertRaisesRegex(NotImplementedError, "src_dir_fd unavailable"):
2341 os.link("source", "target", src_dir_fd=0)
2342
2343 with self.assertRaisesRegex(NotImplementedError, "dst_dir_fd unavailable"):
2344 os.link("source", "target", dst_dir_fd=0)
2345
2346 with self.assertRaisesRegex(NotImplementedError, "src_dir_fd unavailable"):
2347 os.link("source", "target", src_dir_fd=0, dst_dir_fd=0)
2348
2349 # issue 41355: !HAVE_LINKAT code path ignores the follow_symlinks flag
2350 with os_helper.temp_dir() as base_path:
2351 link_path = os.path.join(base_path, "link")
2352 target_path = os.path.join(base_path, "target")
2353 source_path = os.path.join(base_path, "source")
2354
2355 with open(source_path, "w") as fp:
2356 fp.write("data")
2357
2358 os.symlink("target", link_path)
2359
2360 # Calling os.link should fail in the link(2) call, and
2361 # should not reject *follow_symlinks* (to match the
2362 # behaviour you'd get when building on a platform without
2363 # linkat)
2364 with self.assertRaises(FileExistsError):
2365 os.link(source_path, link_path, follow_symlinks=True)
2366
2367 with self.assertRaises(FileExistsError):
2368 os.link(source_path, link_path, follow_symlinks=False)
2369
2370
2371 def test_listdir_scandir(self):
2372 self._verify_available("HAVE_FDOPENDIR")
2373 if self.mac_ver >= (10, 10):
2374 self.assertIn("HAVE_FDOPENDIR", posix._have_functions)
2375
2376 else:
2377 self.assertNotIn("HAVE_FDOPENDIR", posix._have_functions)
2378
2379 with self.assertRaisesRegex(TypeError, "listdir: path should be string, bytes, os.PathLike or None, not int"):
2380 os.listdir(0)
2381
2382 with self.assertRaisesRegex(TypeError, "scandir: path should be string, bytes, os.PathLike or None, not int"):
2383 os.scandir(0)
2384
2385 def test_mkdir(self):
2386 self._verify_available("HAVE_MKDIRAT")
2387 if self.mac_ver >= (10, 10):
2388 self.assertIn("HAVE_MKDIRAT", posix._have_functions)
2389
2390 else:
2391 self.assertNotIn("HAVE_MKDIRAT", posix._have_functions)
2392
2393 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2394 os.mkdir("dir", dir_fd=0)
2395
2396 def test_mkfifo(self):
2397 self._verify_available("HAVE_MKFIFOAT")
2398 if self.mac_ver >= (13, 0):
2399 self.assertIn("HAVE_MKFIFOAT", posix._have_functions)
2400
2401 else:
2402 self.assertNotIn("HAVE_MKFIFOAT", posix._have_functions)
2403
2404 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2405 os.mkfifo("path", dir_fd=0)
2406
2407 def test_mknod(self):
2408 self._verify_available("HAVE_MKNODAT")
2409 if self.mac_ver >= (13, 0):
2410 self.assertIn("HAVE_MKNODAT", posix._have_functions)
2411
2412 else:
2413 self.assertNotIn("HAVE_MKNODAT", posix._have_functions)
2414
2415 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2416 os.mknod("path", dir_fd=0)
2417
2418 def test_rename_replace(self):
2419 self._verify_available("HAVE_RENAMEAT")
2420 if self.mac_ver >= (10, 10):
2421 self.assertIn("HAVE_RENAMEAT", posix._have_functions)
2422
2423 else:
2424 self.assertNotIn("HAVE_RENAMEAT", posix._have_functions)
2425
2426 with self.assertRaisesRegex(NotImplementedError, "src_dir_fd and dst_dir_fd unavailable"):
2427 os.rename("a", "b", src_dir_fd=0)
2428
2429 with self.assertRaisesRegex(NotImplementedError, "src_dir_fd and dst_dir_fd unavailable"):
2430 os.rename("a", "b", dst_dir_fd=0)
2431
2432 with self.assertRaisesRegex(NotImplementedError, "src_dir_fd and dst_dir_fd unavailable"):
2433 os.replace("a", "b", src_dir_fd=0)
2434
2435 with self.assertRaisesRegex(NotImplementedError, "src_dir_fd and dst_dir_fd unavailable"):
2436 os.replace("a", "b", dst_dir_fd=0)
2437
2438 def test_unlink_rmdir(self):
2439 self._verify_available("HAVE_UNLINKAT")
2440 if self.mac_ver >= (10, 10):
2441 self.assertIn("HAVE_UNLINKAT", posix._have_functions)
2442
2443 else:
2444 self.assertNotIn("HAVE_UNLINKAT", posix._have_functions)
2445
2446 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2447 os.unlink("path", dir_fd=0)
2448
2449 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2450 os.rmdir("path", dir_fd=0)
2451
2452 def test_open(self):
2453 self._verify_available("HAVE_OPENAT")
2454 if self.mac_ver >= (10, 10):
2455 self.assertIn("HAVE_OPENAT", posix._have_functions)
2456
2457 else:
2458 self.assertNotIn("HAVE_OPENAT", posix._have_functions)
2459
2460 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2461 os.open("path", os.O_RDONLY, dir_fd=0)
2462
2463 def test_readlink(self):
2464 self._verify_available("HAVE_READLINKAT")
2465 if self.mac_ver >= (10, 10):
2466 self.assertIn("HAVE_READLINKAT", posix._have_functions)
2467
2468 else:
2469 self.assertNotIn("HAVE_READLINKAT", posix._have_functions)
2470
2471 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2472 os.readlink("path", dir_fd=0)
2473
2474 def test_symlink(self):
2475 self._verify_available("HAVE_SYMLINKAT")
2476 if self.mac_ver >= (10, 10):
2477 self.assertIn("HAVE_SYMLINKAT", posix._have_functions)
2478
2479 else:
2480 self.assertNotIn("HAVE_SYMLINKAT", posix._have_functions)
2481
2482 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2483 os.symlink("a", "b", dir_fd=0)
2484
2485 def test_utime(self):
2486 self._verify_available("HAVE_FUTIMENS")
2487 self._verify_available("HAVE_UTIMENSAT")
2488 if self.mac_ver >= (10, 13):
2489 self.assertIn("HAVE_FUTIMENS", posix._have_functions)
2490 self.assertIn("HAVE_UTIMENSAT", posix._have_functions)
2491
2492 else:
2493 self.assertNotIn("HAVE_FUTIMENS", posix._have_functions)
2494 self.assertNotIn("HAVE_UTIMENSAT", posix._have_functions)
2495
2496 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2497 os.utime("path", dir_fd=0)
2498
2499
2500 class NamespacesTests(unittest.TestCase):
2501 """Tests for os.unshare() and os.setns()."""
2502
2503 @unittest.skipUnless(hasattr(os, 'unshare'), 'needs os.unshare()')
2504 @unittest.skipUnless(hasattr(os, 'setns'), 'needs os.setns()')
2505 @unittest.skipUnless(os.path.exists('/proc/self/ns/uts'), 'need /proc/self/ns/uts')
2506 @support.requires_linux_version(3, 0, 0)
2507 def test_unshare_setns(self):
2508 code = """if 1:
2509 import errno
2510 import os
2511 import sys
2512 fd = os.open('/proc/self/ns/uts', os.O_RDONLY)
2513 try:
2514 original = os.readlink('/proc/self/ns/uts')
2515 try:
2516 os.unshare(os.CLONE_NEWUTS)
2517 except OSError as e:
2518 if e.errno == errno.ENOSPC:
2519 # skip test if limit is exceeded
2520 sys.exit()
2521 raise
2522 new = os.readlink('/proc/self/ns/uts')
2523 if original == new:
2524 raise Exception('os.unshare failed')
2525 os.setns(fd, os.CLONE_NEWUTS)
2526 restored = os.readlink('/proc/self/ns/uts')
2527 if original != restored:
2528 raise Exception('os.setns failed')
2529 except PermissionError:
2530 # The calling process did not have the required privileges
2531 # for this operation
2532 pass
2533 except OSError as e:
2534 # Skip the test on these errors:
2535 # - ENOSYS: syscall not available
2536 # - EINVAL: kernel was not configured with the CONFIG_UTS_NS option
2537 # - ENOMEM: not enough memory
2538 if e.errno not in (errno.ENOSYS, errno.EINVAL, errno.ENOMEM):
2539 raise
2540 finally:
2541 os.close(fd)
2542 """
2543
2544 assert_python_ok("-c", code)
2545
2546
2547 def tearDownModule():
2548 support.reap_children()
2549
2550
2551 if __name__ == '__main__':
2552 unittest.main()