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.assertTrue(hasattr(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.assertTrue(hasattr(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
1545 # tests for the posix *at functions follow
1546 class TestPosixDirFd(unittest.TestCase):
1547 count = 0
1548
1549 @contextmanager
1550 def prepare(self):
1551 TestPosixDirFd.count += 1
1552 name = f'{os_helper.TESTFN}_{self.count}'
1553 base_dir = f'{os_helper.TESTFN}_{self.count}base'
1554 posix.mkdir(base_dir)
1555 self.addCleanup(posix.rmdir, base_dir)
1556 fullname = os.path.join(base_dir, name)
1557 assert not os.path.exists(fullname)
1558 with os_helper.open_dir_fd(base_dir) as dir_fd:
1559 yield (dir_fd, name, fullname)
1560
1561 @contextmanager
1562 def prepare_file(self):
1563 with self.prepare() as (dir_fd, name, fullname):
1564 os_helper.create_empty_file(fullname)
1565 self.addCleanup(posix.unlink, fullname)
1566 yield (dir_fd, name, fullname)
1567
1568 @unittest.skipUnless(os.access in os.supports_dir_fd, "test needs dir_fd support for os.access()")
1569 def test_access_dir_fd(self):
1570 with self.prepare_file() as (dir_fd, name, fullname):
1571 self.assertTrue(posix.access(name, os.R_OK, dir_fd=dir_fd))
1572
1573 @unittest.skipUnless(os.chmod in os.supports_dir_fd, "test needs dir_fd support in os.chmod()")
1574 def test_chmod_dir_fd(self):
1575 with self.prepare_file() as (dir_fd, name, fullname):
1576 posix.chmod(fullname, stat.S_IRUSR)
1577 posix.chmod(name, stat.S_IRUSR | stat.S_IWUSR, dir_fd=dir_fd)
1578 s = posix.stat(fullname)
1579 self.assertEqual(s.st_mode & stat.S_IRWXU,
1580 stat.S_IRUSR | stat.S_IWUSR)
1581
1582 @unittest.skipUnless(hasattr(os, 'chown') and (os.chown in os.supports_dir_fd),
1583 "test needs dir_fd support in os.chown()")
1584 @unittest.skipIf(support.is_emscripten, "getgid() is a stub")
1585 def test_chown_dir_fd(self):
1586 with self.prepare_file() as (dir_fd, name, fullname):
1587 posix.chown(name, os.getuid(), os.getgid(), dir_fd=dir_fd)
1588
1589 @unittest.skipUnless(os.stat in os.supports_dir_fd, "test needs dir_fd support in os.stat()")
1590 def test_stat_dir_fd(self):
1591 with self.prepare() as (dir_fd, name, fullname):
1592 with open(fullname, 'w') as outfile:
1593 outfile.write("testline\n")
1594 self.addCleanup(posix.unlink, fullname)
1595
1596 s1 = posix.stat(fullname)
1597 s2 = posix.stat(name, dir_fd=dir_fd)
1598 self.assertEqual(s1, s2)
1599 s2 = posix.stat(fullname, dir_fd=None)
1600 self.assertEqual(s1, s2)
1601
1602 self.assertRaisesRegex(TypeError, 'should be integer or None, not',
1603 posix.stat, name, dir_fd=posix.getcwd())
1604 self.assertRaisesRegex(TypeError, 'should be integer or None, not',
1605 posix.stat, name, dir_fd=float(dir_fd))
1606 self.assertRaises(OverflowError,
1607 posix.stat, name, dir_fd=10**20)
1608
1609 for fd in False, True:
1610 with self.assertWarnsRegex(RuntimeWarning,
1611 'bool is used as a file descriptor') as cm:
1612 with self.assertRaises(OSError):
1613 posix.stat('nonexisting', dir_fd=fd)
1614 self.assertEqual(cm.filename, __file__)
1615
1616 @unittest.skipUnless(os.utime in os.supports_dir_fd, "test needs dir_fd support in os.utime()")
1617 def test_utime_dir_fd(self):
1618 with self.prepare_file() as (dir_fd, name, fullname):
1619 now = time.time()
1620 posix.utime(name, None, dir_fd=dir_fd)
1621 posix.utime(name, dir_fd=dir_fd)
1622 self.assertRaises(TypeError, posix.utime, name,
1623 now, dir_fd=dir_fd)
1624 self.assertRaises(TypeError, posix.utime, name,
1625 (None, None), dir_fd=dir_fd)
1626 self.assertRaises(TypeError, posix.utime, name,
1627 (now, None), dir_fd=dir_fd)
1628 self.assertRaises(TypeError, posix.utime, name,
1629 (None, now), dir_fd=dir_fd)
1630 self.assertRaises(TypeError, posix.utime, name,
1631 (now, "x"), dir_fd=dir_fd)
1632 posix.utime(name, (int(now), int(now)), dir_fd=dir_fd)
1633 posix.utime(name, (now, now), dir_fd=dir_fd)
1634 posix.utime(name,
1635 (int(now), int((now - int(now)) * 1e9)), dir_fd=dir_fd)
1636 posix.utime(name, dir_fd=dir_fd,
1637 times=(int(now), int((now - int(now)) * 1e9)))
1638
1639 # try dir_fd and follow_symlinks together
1640 if os.utime in os.supports_follow_symlinks:
1641 try:
1642 posix.utime(name, follow_symlinks=False, dir_fd=dir_fd)
1643 except ValueError:
1644 # whoops! using both together not supported on this platform.
1645 pass
1646
1647 @unittest.skipIf(
1648 support.is_wasi,
1649 "WASI: symlink following on path_link is not supported"
1650 )
1651 @unittest.skipUnless(
1652 hasattr(os, "link") and os.link in os.supports_dir_fd,
1653 "test needs dir_fd support in os.link()"
1654 )
1655 def test_link_dir_fd(self):
1656 with self.prepare_file() as (dir_fd, name, fullname), \
1657 self.prepare() as (dir_fd2, linkname, fulllinkname):
1658 try:
1659 posix.link(name, linkname, src_dir_fd=dir_fd, dst_dir_fd=dir_fd2)
1660 except PermissionError as e:
1661 self.skipTest('posix.link(): %s' % e)
1662 self.addCleanup(posix.unlink, fulllinkname)
1663 # should have same inodes
1664 self.assertEqual(posix.stat(fullname)[1],
1665 posix.stat(fulllinkname)[1])
1666
1667 @unittest.skipUnless(os.mkdir in os.supports_dir_fd, "test needs dir_fd support in os.mkdir()")
1668 def test_mkdir_dir_fd(self):
1669 with self.prepare() as (dir_fd, name, fullname):
1670 posix.mkdir(name, dir_fd=dir_fd)
1671 self.addCleanup(posix.rmdir, fullname)
1672 posix.stat(fullname) # should not raise exception
1673
1674 @unittest.skipUnless(hasattr(os, 'mknod')
1675 and (os.mknod in os.supports_dir_fd)
1676 and hasattr(stat, 'S_IFIFO'),
1677 "test requires both stat.S_IFIFO and dir_fd support for os.mknod()")
1678 def test_mknod_dir_fd(self):
1679 # Test using mknodat() to create a FIFO (the only use specified
1680 # by POSIX).
1681 with self.prepare() as (dir_fd, name, fullname):
1682 mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR
1683 try:
1684 posix.mknod(name, mode, 0, dir_fd=dir_fd)
1685 except OSError as e:
1686 # Some old systems don't allow unprivileged users to use
1687 # mknod(), or only support creating device nodes.
1688 self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES))
1689 else:
1690 self.addCleanup(posix.unlink, fullname)
1691 self.assertTrue(stat.S_ISFIFO(posix.stat(fullname).st_mode))
1692
1693 @unittest.skipUnless(os.open in os.supports_dir_fd, "test needs dir_fd support in os.open()")
1694 def test_open_dir_fd(self):
1695 with self.prepare() as (dir_fd, name, fullname):
1696 with open(fullname, 'wb') as outfile:
1697 outfile.write(b"testline\n")
1698 self.addCleanup(posix.unlink, fullname)
1699 fd = posix.open(name, posix.O_RDONLY, dir_fd=dir_fd)
1700 try:
1701 res = posix.read(fd, 9)
1702 self.assertEqual(b"testline\n", res)
1703 finally:
1704 posix.close(fd)
1705
1706 @unittest.skipUnless(hasattr(os, 'readlink') and (os.readlink in os.supports_dir_fd),
1707 "test needs dir_fd support in os.readlink()")
1708 def test_readlink_dir_fd(self):
1709 with self.prepare() as (dir_fd, name, fullname):
1710 os.symlink('symlink', fullname)
1711 self.addCleanup(posix.unlink, fullname)
1712 self.assertEqual(posix.readlink(name, dir_fd=dir_fd), 'symlink')
1713
1714 @unittest.skipUnless(os.rename in os.supports_dir_fd, "test needs dir_fd support in os.rename()")
1715 def test_rename_dir_fd(self):
1716 with self.prepare_file() as (dir_fd, name, fullname), \
1717 self.prepare() as (dir_fd2, name2, fullname2):
1718 posix.rename(name, name2,
1719 src_dir_fd=dir_fd, dst_dir_fd=dir_fd2)
1720 posix.stat(fullname2) # should not raise exception
1721 posix.rename(fullname2, fullname)
1722
1723 @unittest.skipUnless(os.symlink in os.supports_dir_fd, "test needs dir_fd support in os.symlink()")
1724 def test_symlink_dir_fd(self):
1725 with self.prepare() as (dir_fd, name, fullname):
1726 posix.symlink('symlink', name, dir_fd=dir_fd)
1727 self.addCleanup(posix.unlink, fullname)
1728 self.assertEqual(posix.readlink(fullname), 'symlink')
1729
1730 @unittest.skipUnless(os.unlink in os.supports_dir_fd, "test needs dir_fd support in os.unlink()")
1731 def test_unlink_dir_fd(self):
1732 with self.prepare() as (dir_fd, name, fullname):
1733 os_helper.create_empty_file(fullname)
1734 posix.stat(fullname) # should not raise exception
1735 try:
1736 posix.unlink(name, dir_fd=dir_fd)
1737 self.assertRaises(OSError, posix.stat, fullname)
1738 except:
1739 self.addCleanup(posix.unlink, fullname)
1740 raise
1741
1742 @unittest.skipUnless(hasattr(os, 'mkfifo') and os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()")
1743 def test_mkfifo_dir_fd(self):
1744 with self.prepare() as (dir_fd, name, fullname):
1745 try:
1746 posix.mkfifo(name, stat.S_IRUSR | stat.S_IWUSR, dir_fd=dir_fd)
1747 except PermissionError as e:
1748 self.skipTest('posix.mkfifo(): %s' % e)
1749 self.addCleanup(posix.unlink, fullname)
1750 self.assertTrue(stat.S_ISFIFO(posix.stat(fullname).st_mode))
1751
1752
1753 class PosixGroupsTester(unittest.TestCase):
1754
1755 def setUp(self):
1756 if posix.getuid() != 0:
1757 raise unittest.SkipTest("not enough privileges")
1758 if not hasattr(posix, 'getgroups'):
1759 raise unittest.SkipTest("need posix.getgroups")
1760 if sys.platform == 'darwin':
1761 raise unittest.SkipTest("getgroups(2) is broken on OSX")
1762 self.saved_groups = posix.getgroups()
1763
1764 def tearDown(self):
1765 if hasattr(posix, 'setgroups'):
1766 posix.setgroups(self.saved_groups)
1767 elif hasattr(posix, 'initgroups'):
1768 name = pwd.getpwuid(posix.getuid()).pw_name
1769 posix.initgroups(name, self.saved_groups[0])
1770
1771 @unittest.skipUnless(hasattr(posix, 'initgroups'),
1772 "test needs posix.initgroups()")
1773 def test_initgroups(self):
1774 # find missing group
1775
1776 g = max(self.saved_groups or [0]) + 1
1777 name = pwd.getpwuid(posix.getuid()).pw_name
1778 posix.initgroups(name, g)
1779 self.assertIn(g, posix.getgroups())
1780
1781 @unittest.skipUnless(hasattr(posix, 'setgroups'),
1782 "test needs posix.setgroups()")
1783 def test_setgroups(self):
1784 for groups in [[0], list(range(16))]:
1785 posix.setgroups(groups)
1786 self.assertListEqual(groups, posix.getgroups())
1787
1788
1789 class _PosixSpawnMixin:
1790 # Program which does nothing and exits with status 0 (success)
1791 NOOP_PROGRAM = (sys.executable, '-I', '-S', '-c', 'pass')
1792 spawn_func = None
1793
1794 def python_args(self, *args):
1795 # Disable site module to avoid side effects. For example,
1796 # on Fedora 28, if the HOME environment variable is not set,
1797 # site._getuserbase() calls pwd.getpwuid() which opens
1798 # /var/lib/sss/mc/passwd but then leaves the file open which makes
1799 # test_close_file() to fail.
1800 return (sys.executable, '-I', '-S', *args)
1801
1802 def test_returns_pid(self):
1803 pidfile = os_helper.TESTFN
1804 self.addCleanup(os_helper.unlink, pidfile)
1805 script = f"""if 1:
1806 import os
1807 with open({pidfile!r}, "w") as pidfile:
1808 pidfile.write(str(os.getpid()))
1809 """
1810 args = self.python_args('-c', script)
1811 pid = self.spawn_func(args[0], args, os.environ)
1812 support.wait_process(pid, exitcode=0)
1813 with open(pidfile, encoding="utf-8") as f:
1814 self.assertEqual(f.read(), str(pid))
1815
1816 def test_no_such_executable(self):
1817 no_such_executable = 'no_such_executable'
1818 try:
1819 pid = self.spawn_func(no_such_executable,
1820 [no_such_executable],
1821 os.environ)
1822 # bpo-35794: PermissionError can be raised if there are
1823 # directories in the $PATH that are not accessible.
1824 except (FileNotFoundError, PermissionError) as exc:
1825 self.assertEqual(exc.filename, no_such_executable)
1826 else:
1827 pid2, status = os.waitpid(pid, 0)
1828 self.assertEqual(pid2, pid)
1829 self.assertNotEqual(status, 0)
1830
1831 def test_specify_environment(self):
1832 envfile = os_helper.TESTFN
1833 self.addCleanup(os_helper.unlink, envfile)
1834 script = f"""if 1:
1835 import os
1836 with open({envfile!r}, "w", encoding="utf-8") as envfile:
1837 envfile.write(os.environ['foo'])
1838 """
1839 args = self.python_args('-c', script)
1840 pid = self.spawn_func(args[0], args,
1841 {**os.environ, 'foo': 'bar'})
1842 support.wait_process(pid, exitcode=0)
1843 with open(envfile, encoding="utf-8") as f:
1844 self.assertEqual(f.read(), 'bar')
1845
1846 def test_none_file_actions(self):
1847 pid = self.spawn_func(
1848 self.NOOP_PROGRAM[0],
1849 self.NOOP_PROGRAM,
1850 os.environ,
1851 file_actions=None
1852 )
1853 support.wait_process(pid, exitcode=0)
1854
1855 def test_empty_file_actions(self):
1856 pid = self.spawn_func(
1857 self.NOOP_PROGRAM[0],
1858 self.NOOP_PROGRAM,
1859 os.environ,
1860 file_actions=[]
1861 )
1862 support.wait_process(pid, exitcode=0)
1863
1864 def test_resetids_explicit_default(self):
1865 pid = self.spawn_func(
1866 sys.executable,
1867 [sys.executable, '-c', 'pass'],
1868 os.environ,
1869 resetids=False
1870 )
1871 support.wait_process(pid, exitcode=0)
1872
1873 def test_resetids(self):
1874 pid = self.spawn_func(
1875 sys.executable,
1876 [sys.executable, '-c', 'pass'],
1877 os.environ,
1878 resetids=True
1879 )
1880 support.wait_process(pid, exitcode=0)
1881
1882 def test_setpgroup(self):
1883 pid = self.spawn_func(
1884 sys.executable,
1885 [sys.executable, '-c', 'pass'],
1886 os.environ,
1887 setpgroup=os.getpgrp()
1888 )
1889 support.wait_process(pid, exitcode=0)
1890
1891 def test_setpgroup_wrong_type(self):
1892 with self.assertRaises(TypeError):
1893 self.spawn_func(sys.executable,
1894 [sys.executable, "-c", "pass"],
1895 os.environ, setpgroup="023")
1896
1897 @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'),
1898 'need signal.pthread_sigmask()')
1899 def test_setsigmask(self):
1900 code = textwrap.dedent("""\
1901 import signal
1902 signal.raise_signal(signal.SIGUSR1)""")
1903
1904 pid = self.spawn_func(
1905 sys.executable,
1906 [sys.executable, '-c', code],
1907 os.environ,
1908 setsigmask=[signal.SIGUSR1]
1909 )
1910 support.wait_process(pid, exitcode=0)
1911
1912 def test_setsigmask_wrong_type(self):
1913 with self.assertRaises(TypeError):
1914 self.spawn_func(sys.executable,
1915 [sys.executable, "-c", "pass"],
1916 os.environ, setsigmask=34)
1917 with self.assertRaises(TypeError):
1918 self.spawn_func(sys.executable,
1919 [sys.executable, "-c", "pass"],
1920 os.environ, setsigmask=["j"])
1921 with self.assertRaises(ValueError):
1922 self.spawn_func(sys.executable,
1923 [sys.executable, "-c", "pass"],
1924 os.environ, setsigmask=[signal.NSIG,
1925 signal.NSIG+1])
1926
1927 def test_setsid(self):
1928 rfd, wfd = os.pipe()
1929 self.addCleanup(os.close, rfd)
1930 try:
1931 os.set_inheritable(wfd, True)
1932
1933 code = textwrap.dedent(f"""
1934 import os
1935 fd = {wfd}
1936 sid = os.getsid(0)
1937 os.write(fd, str(sid).encode())
1938 """)
1939
1940 try:
1941 pid = self.spawn_func(sys.executable,
1942 [sys.executable, "-c", code],
1943 os.environ, setsid=True)
1944 except NotImplementedError as exc:
1945 self.skipTest(f"setsid is not supported: {exc!r}")
1946 except PermissionError as exc:
1947 self.skipTest(f"setsid failed with: {exc!r}")
1948 finally:
1949 os.close(wfd)
1950
1951 support.wait_process(pid, exitcode=0)
1952
1953 output = os.read(rfd, 100)
1954 child_sid = int(output)
1955 parent_sid = os.getsid(os.getpid())
1956 self.assertNotEqual(parent_sid, child_sid)
1957
1958 @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'),
1959 'need signal.pthread_sigmask()')
1960 def test_setsigdef(self):
1961 original_handler = signal.signal(signal.SIGUSR1, signal.SIG_IGN)
1962 code = textwrap.dedent("""\
1963 import signal
1964 signal.raise_signal(signal.SIGUSR1)""")
1965 try:
1966 pid = self.spawn_func(
1967 sys.executable,
1968 [sys.executable, '-c', code],
1969 os.environ,
1970 setsigdef=[signal.SIGUSR1]
1971 )
1972 finally:
1973 signal.signal(signal.SIGUSR1, original_handler)
1974
1975 support.wait_process(pid, exitcode=-signal.SIGUSR1)
1976
1977 def test_setsigdef_wrong_type(self):
1978 with self.assertRaises(TypeError):
1979 self.spawn_func(sys.executable,
1980 [sys.executable, "-c", "pass"],
1981 os.environ, setsigdef=34)
1982 with self.assertRaises(TypeError):
1983 self.spawn_func(sys.executable,
1984 [sys.executable, "-c", "pass"],
1985 os.environ, setsigdef=["j"])
1986 with self.assertRaises(ValueError):
1987 self.spawn_func(sys.executable,
1988 [sys.executable, "-c", "pass"],
1989 os.environ, setsigdef=[signal.NSIG, signal.NSIG+1])
1990
1991 @requires_sched
1992 @unittest.skipIf(sys.platform.startswith(('freebsd', 'netbsd')),
1993 "bpo-34685: test can fail on BSD")
1994 def test_setscheduler_only_param(self):
1995 policy = os.sched_getscheduler(0)
1996 priority = os.sched_get_priority_min(policy)
1997 code = textwrap.dedent(f"""\
1998 import os, sys
1999 if os.sched_getscheduler(0) != {policy}:
2000 sys.exit(101)
2001 if os.sched_getparam(0).sched_priority != {priority}:
2002 sys.exit(102)""")
2003 pid = self.spawn_func(
2004 sys.executable,
2005 [sys.executable, '-c', code],
2006 os.environ,
2007 scheduler=(None, os.sched_param(priority))
2008 )
2009 support.wait_process(pid, exitcode=0)
2010
2011 @requires_sched
2012 @unittest.skipIf(sys.platform.startswith(('freebsd', 'netbsd')),
2013 "bpo-34685: test can fail on BSD")
2014 @unittest.skipIf(platform.libc_ver()[0] == 'glibc' and
2015 os.sched_getscheduler(0) in [
2016 os.SCHED_BATCH,
2017 os.SCHED_IDLE],
2018 "Skip test due to glibc posix_spawn policy")
2019 def test_setscheduler_with_policy(self):
2020 policy = os.sched_getscheduler(0)
2021 priority = os.sched_get_priority_min(policy)
2022 code = textwrap.dedent(f"""\
2023 import os, sys
2024 if os.sched_getscheduler(0) != {policy}:
2025 sys.exit(101)
2026 if os.sched_getparam(0).sched_priority != {priority}:
2027 sys.exit(102)""")
2028 pid = self.spawn_func(
2029 sys.executable,
2030 [sys.executable, '-c', code],
2031 os.environ,
2032 scheduler=(policy, os.sched_param(priority))
2033 )
2034 support.wait_process(pid, exitcode=0)
2035
2036 def test_multiple_file_actions(self):
2037 file_actions = [
2038 (os.POSIX_SPAWN_OPEN, 3, os.path.realpath(__file__), os.O_RDONLY, 0),
2039 (os.POSIX_SPAWN_CLOSE, 0),
2040 (os.POSIX_SPAWN_DUP2, 1, 4),
2041 ]
2042 pid = self.spawn_func(self.NOOP_PROGRAM[0],
2043 self.NOOP_PROGRAM,
2044 os.environ,
2045 file_actions=file_actions)
2046 support.wait_process(pid, exitcode=0)
2047
2048 def test_bad_file_actions(self):
2049 args = self.NOOP_PROGRAM
2050 with self.assertRaises(TypeError):
2051 self.spawn_func(args[0], args, os.environ,
2052 file_actions=[None])
2053 with self.assertRaises(TypeError):
2054 self.spawn_func(args[0], args, os.environ,
2055 file_actions=[()])
2056 with self.assertRaises(TypeError):
2057 self.spawn_func(args[0], args, os.environ,
2058 file_actions=[(None,)])
2059 with self.assertRaises(TypeError):
2060 self.spawn_func(args[0], args, os.environ,
2061 file_actions=[(12345,)])
2062 with self.assertRaises(TypeError):
2063 self.spawn_func(args[0], args, os.environ,
2064 file_actions=[(os.POSIX_SPAWN_CLOSE,)])
2065 with self.assertRaises(TypeError):
2066 self.spawn_func(args[0], args, os.environ,
2067 file_actions=[(os.POSIX_SPAWN_CLOSE, 1, 2)])
2068 with self.assertRaises(TypeError):
2069 self.spawn_func(args[0], args, os.environ,
2070 file_actions=[(os.POSIX_SPAWN_CLOSE, None)])
2071 with self.assertRaises(ValueError):
2072 self.spawn_func(args[0], args, os.environ,
2073 file_actions=[(os.POSIX_SPAWN_OPEN,
2074 3, __file__ + '\0',
2075 os.O_RDONLY, 0)])
2076
2077 def test_open_file(self):
2078 outfile = os_helper.TESTFN
2079 self.addCleanup(os_helper.unlink, outfile)
2080 script = """if 1:
2081 import sys
2082 sys.stdout.write("hello")
2083 """
2084 file_actions = [
2085 (os.POSIX_SPAWN_OPEN, 1, outfile,
2086 os.O_WRONLY | os.O_CREAT | os.O_TRUNC,
2087 stat.S_IRUSR | stat.S_IWUSR),
2088 ]
2089 args = self.python_args('-c', script)
2090 pid = self.spawn_func(args[0], args, os.environ,
2091 file_actions=file_actions)
2092
2093 support.wait_process(pid, exitcode=0)
2094 with open(outfile, encoding="utf-8") as f:
2095 self.assertEqual(f.read(), 'hello')
2096
2097 def test_close_file(self):
2098 closefile = os_helper.TESTFN
2099 self.addCleanup(os_helper.unlink, closefile)
2100 script = f"""if 1:
2101 import os
2102 try:
2103 os.fstat(0)
2104 except OSError as e:
2105 with open({closefile!r}, 'w', encoding='utf-8') as closefile:
2106 closefile.write('is closed %d' % e.errno)
2107 """
2108 args = self.python_args('-c', script)
2109 pid = self.spawn_func(args[0], args, os.environ,
2110 file_actions=[(os.POSIX_SPAWN_CLOSE, 0)])
2111
2112 support.wait_process(pid, exitcode=0)
2113 with open(closefile, encoding="utf-8") as f:
2114 self.assertEqual(f.read(), 'is closed %d' % errno.EBADF)
2115
2116 def test_dup2(self):
2117 dupfile = os_helper.TESTFN
2118 self.addCleanup(os_helper.unlink, dupfile)
2119 script = """if 1:
2120 import sys
2121 sys.stdout.write("hello")
2122 """
2123 with open(dupfile, "wb") as childfile:
2124 file_actions = [
2125 (os.POSIX_SPAWN_DUP2, childfile.fileno(), 1),
2126 ]
2127 args = self.python_args('-c', script)
2128 pid = self.spawn_func(args[0], args, os.environ,
2129 file_actions=file_actions)
2130 support.wait_process(pid, exitcode=0)
2131 with open(dupfile, encoding="utf-8") as f:
2132 self.assertEqual(f.read(), 'hello')
2133
2134
2135 @unittest.skipUnless(hasattr(os, 'posix_spawn'), "test needs os.posix_spawn")
2136 @support.requires_subprocess()
2137 class TestPosixSpawn(unittest.TestCase, _PosixSpawnMixin):
2138 spawn_func = getattr(posix, 'posix_spawn', None)
2139
2140
2141 @unittest.skipUnless(hasattr(os, 'posix_spawnp'), "test needs os.posix_spawnp")
2142 @support.requires_subprocess()
2143 class TestPosixSpawnP(unittest.TestCase, _PosixSpawnMixin):
2144 spawn_func = getattr(posix, 'posix_spawnp', None)
2145
2146 @os_helper.skip_unless_symlink
2147 def test_posix_spawnp(self):
2148 # Use a symlink to create a program in its own temporary directory
2149 temp_dir = tempfile.mkdtemp()
2150 self.addCleanup(os_helper.rmtree, temp_dir)
2151
2152 program = 'posix_spawnp_test_program.exe'
2153 program_fullpath = os.path.join(temp_dir, program)
2154 os.symlink(sys.executable, program_fullpath)
2155
2156 try:
2157 path = os.pathsep.join((temp_dir, os.environ['PATH']))
2158 except KeyError:
2159 path = temp_dir # PATH is not set
2160
2161 spawn_args = (program, '-I', '-S', '-c', 'pass')
2162 code = textwrap.dedent("""
2163 import os
2164 from test import support
2165
2166 args = %a
2167 pid = os.posix_spawnp(args[0], args, os.environ)
2168
2169 support.wait_process(pid, exitcode=0)
2170 """ % (spawn_args,))
2171
2172 # Use a subprocess to test os.posix_spawnp() with a modified PATH
2173 # environment variable: posix_spawnp() uses the current environment
2174 # to locate the program, not its environment argument.
2175 args = ('-c', code)
2176 assert_python_ok(*args, PATH=path)
2177
2178
2179 @unittest.skipUnless(sys.platform == "darwin", "test weak linking on macOS")
2180 class TestPosixWeaklinking(unittest.TestCase):
2181 # These test cases verify that weak linking support on macOS works
2182 # as expected. These cases only test new behaviour introduced by weak linking,
2183 # regular behaviour is tested by the normal test cases.
2184 #
2185 # See the section on Weak Linking in Mac/README.txt for more information.
2186 def setUp(self):
2187 import sysconfig
2188 import platform
2189
2190 config_vars = sysconfig.get_config_vars()
2191 self.available = { nm for nm in config_vars if nm.startswith("HAVE_") and config_vars[nm] }
2192 self.mac_ver = tuple(int(part) for part in platform.mac_ver()[0].split("."))
2193
2194 def _verify_available(self, name):
2195 if name not in self.available:
2196 raise unittest.SkipTest(f"{name} not weak-linked")
2197
2198 def test_pwritev(self):
2199 self._verify_available("HAVE_PWRITEV")
2200 if self.mac_ver >= (10, 16):
2201 self.assertTrue(hasattr(os, "pwritev"), "os.pwritev is not available")
2202 self.assertTrue(hasattr(os, "preadv"), "os.readv is not available")
2203
2204 else:
2205 self.assertFalse(hasattr(os, "pwritev"), "os.pwritev is available")
2206 self.assertFalse(hasattr(os, "preadv"), "os.readv is available")
2207
2208 def test_stat(self):
2209 self._verify_available("HAVE_FSTATAT")
2210 if self.mac_ver >= (10, 10):
2211 self.assertIn("HAVE_FSTATAT", posix._have_functions)
2212
2213 else:
2214 self.assertNotIn("HAVE_FSTATAT", posix._have_functions)
2215
2216 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2217 os.stat("file", dir_fd=0)
2218
2219 def test_ptsname_r(self):
2220 self._verify_available("HAVE_PTSNAME_R")
2221 if self.mac_ver >= (10, 13, 4):
2222 self.assertIn("HAVE_PTSNAME_R", posix._have_functions)
2223 else:
2224 self.assertNotIn("HAVE_PTSNAME_R", posix._have_functions)
2225
2226 def test_access(self):
2227 self._verify_available("HAVE_FACCESSAT")
2228 if self.mac_ver >= (10, 10):
2229 self.assertIn("HAVE_FACCESSAT", posix._have_functions)
2230
2231 else:
2232 self.assertNotIn("HAVE_FACCESSAT", posix._have_functions)
2233
2234 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2235 os.access("file", os.R_OK, dir_fd=0)
2236
2237 with self.assertRaisesRegex(NotImplementedError, "follow_symlinks unavailable"):
2238 os.access("file", os.R_OK, follow_symlinks=False)
2239
2240 with self.assertRaisesRegex(NotImplementedError, "effective_ids unavailable"):
2241 os.access("file", os.R_OK, effective_ids=True)
2242
2243 def test_chmod(self):
2244 self._verify_available("HAVE_FCHMODAT")
2245 if self.mac_ver >= (10, 10):
2246 self.assertIn("HAVE_FCHMODAT", posix._have_functions)
2247
2248 else:
2249 self.assertNotIn("HAVE_FCHMODAT", posix._have_functions)
2250 self.assertIn("HAVE_LCHMOD", posix._have_functions)
2251
2252 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2253 os.chmod("file", 0o644, dir_fd=0)
2254
2255 def test_chown(self):
2256 self._verify_available("HAVE_FCHOWNAT")
2257 if self.mac_ver >= (10, 10):
2258 self.assertIn("HAVE_FCHOWNAT", posix._have_functions)
2259
2260 else:
2261 self.assertNotIn("HAVE_FCHOWNAT", posix._have_functions)
2262 self.assertIn("HAVE_LCHOWN", posix._have_functions)
2263
2264 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2265 os.chown("file", 0, 0, dir_fd=0)
2266
2267 def test_link(self):
2268 self._verify_available("HAVE_LINKAT")
2269 if self.mac_ver >= (10, 10):
2270 self.assertIn("HAVE_LINKAT", posix._have_functions)
2271
2272 else:
2273 self.assertNotIn("HAVE_LINKAT", posix._have_functions)
2274
2275 with self.assertRaisesRegex(NotImplementedError, "src_dir_fd unavailable"):
2276 os.link("source", "target", src_dir_fd=0)
2277
2278 with self.assertRaisesRegex(NotImplementedError, "dst_dir_fd unavailable"):
2279 os.link("source", "target", dst_dir_fd=0)
2280
2281 with self.assertRaisesRegex(NotImplementedError, "src_dir_fd unavailable"):
2282 os.link("source", "target", src_dir_fd=0, dst_dir_fd=0)
2283
2284 # issue 41355: !HAVE_LINKAT code path ignores the follow_symlinks flag
2285 with os_helper.temp_dir() as base_path:
2286 link_path = os.path.join(base_path, "link")
2287 target_path = os.path.join(base_path, "target")
2288 source_path = os.path.join(base_path, "source")
2289
2290 with open(source_path, "w") as fp:
2291 fp.write("data")
2292
2293 os.symlink("target", link_path)
2294
2295 # Calling os.link should fail in the link(2) call, and
2296 # should not reject *follow_symlinks* (to match the
2297 # behaviour you'd get when building on a platform without
2298 # linkat)
2299 with self.assertRaises(FileExistsError):
2300 os.link(source_path, link_path, follow_symlinks=True)
2301
2302 with self.assertRaises(FileExistsError):
2303 os.link(source_path, link_path, follow_symlinks=False)
2304
2305
2306 def test_listdir_scandir(self):
2307 self._verify_available("HAVE_FDOPENDIR")
2308 if self.mac_ver >= (10, 10):
2309 self.assertIn("HAVE_FDOPENDIR", posix._have_functions)
2310
2311 else:
2312 self.assertNotIn("HAVE_FDOPENDIR", posix._have_functions)
2313
2314 with self.assertRaisesRegex(TypeError, "listdir: path should be string, bytes, os.PathLike or None, not int"):
2315 os.listdir(0)
2316
2317 with self.assertRaisesRegex(TypeError, "scandir: path should be string, bytes, os.PathLike or None, not int"):
2318 os.scandir(0)
2319
2320 def test_mkdir(self):
2321 self._verify_available("HAVE_MKDIRAT")
2322 if self.mac_ver >= (10, 10):
2323 self.assertIn("HAVE_MKDIRAT", posix._have_functions)
2324
2325 else:
2326 self.assertNotIn("HAVE_MKDIRAT", posix._have_functions)
2327
2328 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2329 os.mkdir("dir", dir_fd=0)
2330
2331 def test_mkfifo(self):
2332 self._verify_available("HAVE_MKFIFOAT")
2333 if self.mac_ver >= (13, 0):
2334 self.assertIn("HAVE_MKFIFOAT", posix._have_functions)
2335
2336 else:
2337 self.assertNotIn("HAVE_MKFIFOAT", posix._have_functions)
2338
2339 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2340 os.mkfifo("path", dir_fd=0)
2341
2342 def test_mknod(self):
2343 self._verify_available("HAVE_MKNODAT")
2344 if self.mac_ver >= (13, 0):
2345 self.assertIn("HAVE_MKNODAT", posix._have_functions)
2346
2347 else:
2348 self.assertNotIn("HAVE_MKNODAT", posix._have_functions)
2349
2350 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2351 os.mknod("path", dir_fd=0)
2352
2353 def test_rename_replace(self):
2354 self._verify_available("HAVE_RENAMEAT")
2355 if self.mac_ver >= (10, 10):
2356 self.assertIn("HAVE_RENAMEAT", posix._have_functions)
2357
2358 else:
2359 self.assertNotIn("HAVE_RENAMEAT", posix._have_functions)
2360
2361 with self.assertRaisesRegex(NotImplementedError, "src_dir_fd and dst_dir_fd unavailable"):
2362 os.rename("a", "b", src_dir_fd=0)
2363
2364 with self.assertRaisesRegex(NotImplementedError, "src_dir_fd and dst_dir_fd unavailable"):
2365 os.rename("a", "b", dst_dir_fd=0)
2366
2367 with self.assertRaisesRegex(NotImplementedError, "src_dir_fd and dst_dir_fd unavailable"):
2368 os.replace("a", "b", src_dir_fd=0)
2369
2370 with self.assertRaisesRegex(NotImplementedError, "src_dir_fd and dst_dir_fd unavailable"):
2371 os.replace("a", "b", dst_dir_fd=0)
2372
2373 def test_unlink_rmdir(self):
2374 self._verify_available("HAVE_UNLINKAT")
2375 if self.mac_ver >= (10, 10):
2376 self.assertIn("HAVE_UNLINKAT", posix._have_functions)
2377
2378 else:
2379 self.assertNotIn("HAVE_UNLINKAT", posix._have_functions)
2380
2381 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2382 os.unlink("path", dir_fd=0)
2383
2384 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2385 os.rmdir("path", dir_fd=0)
2386
2387 def test_open(self):
2388 self._verify_available("HAVE_OPENAT")
2389 if self.mac_ver >= (10, 10):
2390 self.assertIn("HAVE_OPENAT", posix._have_functions)
2391
2392 else:
2393 self.assertNotIn("HAVE_OPENAT", posix._have_functions)
2394
2395 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2396 os.open("path", os.O_RDONLY, dir_fd=0)
2397
2398 def test_readlink(self):
2399 self._verify_available("HAVE_READLINKAT")
2400 if self.mac_ver >= (10, 10):
2401 self.assertIn("HAVE_READLINKAT", posix._have_functions)
2402
2403 else:
2404 self.assertNotIn("HAVE_READLINKAT", posix._have_functions)
2405
2406 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2407 os.readlink("path", dir_fd=0)
2408
2409 def test_symlink(self):
2410 self._verify_available("HAVE_SYMLINKAT")
2411 if self.mac_ver >= (10, 10):
2412 self.assertIn("HAVE_SYMLINKAT", posix._have_functions)
2413
2414 else:
2415 self.assertNotIn("HAVE_SYMLINKAT", posix._have_functions)
2416
2417 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2418 os.symlink("a", "b", dir_fd=0)
2419
2420 def test_utime(self):
2421 self._verify_available("HAVE_FUTIMENS")
2422 self._verify_available("HAVE_UTIMENSAT")
2423 if self.mac_ver >= (10, 13):
2424 self.assertIn("HAVE_FUTIMENS", posix._have_functions)
2425 self.assertIn("HAVE_UTIMENSAT", posix._have_functions)
2426
2427 else:
2428 self.assertNotIn("HAVE_FUTIMENS", posix._have_functions)
2429 self.assertNotIn("HAVE_UTIMENSAT", posix._have_functions)
2430
2431 with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
2432 os.utime("path", dir_fd=0)
2433
2434
2435 class NamespacesTests(unittest.TestCase):
2436 """Tests for os.unshare() and os.setns()."""
2437
2438 @unittest.skipUnless(hasattr(os, 'unshare'), 'needs os.unshare()')
2439 @unittest.skipUnless(hasattr(os, 'setns'), 'needs os.setns()')
2440 @unittest.skipUnless(os.path.exists('/proc/self/ns/uts'), 'need /proc/self/ns/uts')
2441 @support.requires_linux_version(3, 0, 0)
2442 def test_unshare_setns(self):
2443 code = """if 1:
2444 import errno
2445 import os
2446 import sys
2447 fd = os.open('/proc/self/ns/uts', os.O_RDONLY)
2448 try:
2449 original = os.readlink('/proc/self/ns/uts')
2450 try:
2451 os.unshare(os.CLONE_NEWUTS)
2452 except OSError as e:
2453 if e.errno == errno.ENOSPC:
2454 # skip test if limit is exceeded
2455 sys.exit()
2456 raise
2457 new = os.readlink('/proc/self/ns/uts')
2458 if original == new:
2459 raise Exception('os.unshare failed')
2460 os.setns(fd, os.CLONE_NEWUTS)
2461 restored = os.readlink('/proc/self/ns/uts')
2462 if original != restored:
2463 raise Exception('os.setns failed')
2464 except PermissionError:
2465 # The calling process did not have the required privileges
2466 # for this operation
2467 pass
2468 except OSError as e:
2469 # Skip the test on these errors:
2470 # - ENOSYS: syscall not available
2471 # - EINVAL: kernel was not configured with the CONFIG_UTS_NS option
2472 # - ENOMEM: not enough memory
2473 if e.errno not in (errno.ENOSYS, errno.EINVAL, errno.ENOMEM):
2474 raise
2475 finally:
2476 os.close(fd)
2477 """
2478
2479 assert_python_ok("-c", code)
2480
2481
2482 def tearDownModule():
2483 support.reap_children()
2484
2485
2486 if __name__ == '__main__':
2487 unittest.main()