Lines Matching full:os
13 import os
22 _DUMMY_SYMLINK = os.path.join(tempfile.gettempdir(),
124 "test needs os.initgroups()")
135 if os.getuid() != 0:
151 self.assertTrue(posix.statvfs(os.curdir))
182 …@unittest.skipUnless(getattr(os, 'execve', None) in os.supports_fd, "test needs execve() to suppor…
183 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
185 fp = os.open(sys.executable, os.O_RDONLY)
187 pid = os.fork()
189 os.chdir(os.path.split(sys.executable)[0])
190 posix.execve(fp, [sys.executable, '-c', 'pass'], os.environ)
194 os.close(fp)
198 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
200 pid = os.fork()
202 os.chdir(os.path.split(sys.executable)[0])
203 posix.execve(sys.executable, [sys.executable, '-c', 'pass'], os.environ)
208 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
211 os.register_at_fork(lambda: None)
213 os.register_at_fork(before=2)
215 os.register_at_fork(after_in_child="three")
217 os.register_at_fork(after_in_parent=b"Five")
219 os.register_at_fork(before=None)
221 os.register_at_fork(after_in_child=None)
223 os.register_at_fork(after_in_parent=None)
226 os.register_at_fork(before=None, after_in_parent=lambda: 3)
229 os.register_at_fork(before=lambda: None, after_in_child='')
233 import os
235 r, w = os.pipe()
236 fin_r, fin_w = os.pipe()
238 os.register_at_fork(before=lambda: os.write(w, b'A'))
239 os.register_at_fork(after_in_parent=lambda: os.write(w, b'C'))
240 os.register_at_fork(after_in_child=lambda: os.write(w, b'E'))
241 os.register_at_fork(before=lambda: os.write(w, b'B'),
242 after_in_parent=lambda: os.write(w, b'D'),
243 after_in_child=lambda: os.write(w, b'F'))
245 pid = os.fork()
248 os.close(w)
250 os.read(fin_r, 1)
251 os._exit(0)
254 os.close(w)
265 os.write(fin_w, b'!')
271 fd = os.open(support.TESTFN, os.O_WRONLY | os.O_CREAT)
273 os.write(fd, b'test')
274 os.lseek(fd, 0, os.SEEK_SET)
279 os.close(fd)
283 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
285 os.write(fd, b'test')
286 os.lseek(fd, 0, os.SEEK_SET)
291 os.close(fd)
295 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
297 os.write(fd, b'test1tt2t3t5t6t6t8')
302 os.close(fd)
307 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
309 os.write(fd, b'test1tt2t3t5t6t6t8')
311 self.assertEqual(posix.preadv(fd, buf, 3, os.RWF_HIPRI), 10)
324 os.close(fd)
329 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
333 os.preadv(fd, buf, 0)
337 os.close(fd)
341 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
343 os.write(fd, b'test')
344 os.lseek(fd, 0, os.SEEK_SET)
348 os.close(fd)
352 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
354 os.write(fd, b"xx")
355 os.lseek(fd, 0, os.SEEK_SET)
356 n = os.pwritev(fd, [b'test1', b'tt2', b't3'], 2)
359 os.lseek(fd, 0, os.SEEK_SET)
362 os.close(fd)
365 @unittest.skipUnless(hasattr(posix, 'os.RWF_SYNC'), "test needs os.RWF_SYNC")
367 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
369 os.write(fd,b"xx")
370 os.lseek(fd, 0, os.SEEK_SET)
371 n = os.pwritev(fd, [b'test1', b'tt2', b't3'], 2, os.RWF_SYNC)
374 os.lseek(fd, 0, os.SEEK_SET)
377 os.close(fd)
382 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
385 os.pwritev(fd, [b"x" * 2**16] * 2**15, 0)
388 os.close(fd)
393 fd = os.open(support.TESTFN, os.O_WRONLY | os.O_CREAT)
407 os.close(fd)
422 fd = os.open(support.TESTFN, os.O_RDONLY)
426 os.close(fd)
437 @unittest.skipUnless(os.utime in os.supports_fd, "test needs fd support in os.utime")
440 fd = os.open(support.TESTFN, os.O_RDONLY)
456 os.close(fd)
458 …@unittest.skipUnless(os.utime in os.supports_follow_symlinks, "test needs follow_symlinks support …
471 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
473 n = os.writev(fd, (b'test1', b'tt2', b't3'))
476 os.lseek(fd, 0, os.SEEK_SET)
489 os.close(fd)
494 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
497 os.writev(fd, [b"x" * 2**16] * 2**15)
500 os.close(fd)
504 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
506 os.write(fd, b'test1tt2t3')
507 os.lseek(fd, 0, os.SEEK_SET)
522 os.close(fd)
527 fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
531 os.readv(fd, buf)
535 os.close(fd)
544 os.close(fd)
565 @unittest.skipUnless(hasattr(os, 'O_CLOEXEC'), "needs os.O_CLOEXEC")
568 fd = os.open(support.TESTFN, os.O_RDONLY|os.O_CLOEXEC)
569 self.addCleanup(os.close, fd)
570 self.assertFalse(os.get_inheritable(fd))
575 fd = os.open(support.TESTFN,
576 os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
577 self.assertRaises(OSError, os.open, support.TESTFN,
578 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
579 os.close(fd)
582 fd = os.open(support.TESTFN,
583 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
584 self.assertRaises(OSError, os.open, support.TESTFN,
585 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
586 os.close(fd)
591 fd1 = os.open(support.TESTFN,
592 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
593 fd2 = os.open(support.TESTFN,
594 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
595 os.close(fd2)
596 os.close(fd1)
599 fd = os.open(support.TESTFN,
600 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
601 self.assertRaises(OSError, os.open, support.TESTFN,
602 os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
603 os.close(fd)
614 'should be string, bytes, os.PathLike or integer, not',
621 self.assertTrue(posix.stat(os.fsencode(support.TESTFN)))
624 'should be string, bytes, os.PathLike or integer, not',
625 posix.stat, bytearray(os.fsencode(support.TESTFN)))
627 'should be string, bytes, os.PathLike or integer, not',
630 'should be string, bytes, os.PathLike or integer, not',
633 'should be string, bytes, os.PathLike or integer, not',
634 posix.stat, list(os.fsencode(support.TESTFN)))
705 uid = os.getuid()
706 gid = os.getgid()
747 if 0 not in os.getgroups():
757 @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()")
760 os.unlink(support.TESTFN)
767 @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()")
769 os.unlink(support.TESTFN)
780 @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()")
782 os.unlink(support.TESTFN)
784 os.symlink(_DUMMY_SYMLINK, support.TESTFN)
790 posix.chdir(os.curdir)
794 self.assertIn(support.TESTFN, posix.listdir(os.curdir))
798 # it's the same as listdir(os.curdir).
804 self.assertIn(os.fsencode(support.TESTFN), posix.listdir(b'.'))
810 self.assertIn(os.fsencode(support.TESTFN), names)
814 @unittest.skipUnless(posix.listdir in os.supports_fd,
831 self.assertTrue(posix.access(support.TESTFN, os.R_OK))
847 os.close(reader)
848 os.close(writer)
850 @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()")
853 self.assertRaises(TypeError, os.pipe2, 'DEADBEEF')
854 self.assertRaises(TypeError, os.pipe2, 0, 0)
856 # try calling with flags = 0, like os.pipe()
857 r, w = os.pipe2(0)
858 os.close(r)
859 os.close(w)
862 r, w = os.pipe2(os.O_CLOEXEC|os.O_NONBLOCK)
863 self.addCleanup(os.close, r)
864 self.addCleanup(os.close, w)
865 self.assertFalse(os.get_inheritable(r))
866 self.assertFalse(os.get_inheritable(w))
867 self.assertFalse(os.get_blocking(r))
868 self.assertFalse(os.get_blocking(w))
870 self.assertRaises(OSError, os.read, r, 1)
874 os.write(w, b'x' * support.PIPE_MAX_SIZE)
879 @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()")
884 self.assertRaises(OverflowError, os.pipe2, _testcapi.INT_MAX + 1)
885 self.assertRaises(OverflowError, os.pipe2, _testcapi.UINT_MAX + 1)
898 st = os.stat(target_file)
912 new_st = os.stat(target_file)
921 @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()')
925 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
930 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
932 testfn_st = os.stat(support.TESTFN)
936 os.symlink(support.TESTFN, _DUMMY_SYMLINK)
938 dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
954 new_testfn_st = os.stat(support.TESTFN)
955 new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
964 if os.name == "nt":
974 os.putenv('FRUIT\0VEGETABLE', 'cabbage')
976 os.putenv(b'FRUIT\0VEGETABLE', b'cabbage')
978 os.putenv('FRUIT', 'orange\0VEGETABLE=cabbage')
980 os.putenv(b'FRUIT', b'orange\0VEGETABLE=cabbage')
982 os.putenv('FRUIT=ORANGE', 'lemon')
984 os.putenv(b'FRUIT=ORANGE', b'lemon')
989 curdir = os.getcwd()
990 base_path = os.path.abspath(support.TESTFN) + '.getcwd'
993 os.mkdir(base_path)
994 os.chdir(base_path)
1003 os.mkdir(dirname)
1007 os.chdir(dirname)
1009 os.getcwd()
1013 os.chdir('..')
1014 os.rmdir(dirname)
1019 os.chdir(curdir)
1024 @unittest.skipUnless(hasattr(os, 'getuid'), "test needs os.getuid()")
1026 user = pwd.getpwuid(os.getuid())[0]
1027 group = pwd.getpwuid(os.getuid())[3]
1031 @unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()")
1033 with os.popen('id -G 2>/dev/null') as idg:
1044 # Issues 16698: OS X ABIs prior to 10.6 have limits on getgroups()
1051 # 'id -G' and 'os.getgroups()' should return the same
1060 … @unittest.skipUnless(os.access in os.supports_dir_fd, "test needs dir_fd support for os.access()")
1064 self.assertTrue(posix.access(support.TESTFN, os.R_OK, dir_fd=f))
1068 @unittest.skipUnless(os.chmod in os.supports_dir_fd, "test needs dir_fd support in os.chmod()")
1070 os.chmod(support.TESTFN, stat.S_IRUSR)
1081 @unittest.skipUnless(os.chown in os.supports_dir_fd, "test needs dir_fd support in os.chown()")
1088 posix.chown(support.TESTFN, os.getuid(), os.getgid(), dir_fd=f)
1092 @unittest.skipUnless(os.stat in os.supports_dir_fd, "test needs dir_fd support in os.stat()")
1114 @unittest.skipUnless(os.utime in os.supports_dir_fd, "test needs dir_fd support in os.utime()")
1134 if os.utime in os.supports_follow_symlinks:
1144 @unittest.skipUnless(os.link in os.supports_dir_fd, "test needs dir_fd support in os.link()")
1159 @unittest.skipUnless(os.mkdir in os.supports_dir_fd, "test needs dir_fd support in os.mkdir()")
1169 @unittest.skipUnless((os.mknod in os.supports_dir_fd) and hasattr(stat, 'S_IFIFO'),
1170 "test requires both stat.S_IFIFO and dir_fd support for os.mknod()")
1188 @unittest.skipUnless(os.open in os.supports_dir_fd, "test needs dir_fd support in os.open()")
1202 …@unittest.skipUnless(os.readlink in os.supports_dir_fd, "test needs dir_fd support in os.readlink(…
1204 os.symlink(support.TESTFN, support.TESTFN + 'link')
1213 … @unittest.skipUnless(os.rename in os.supports_dir_fd, "test needs dir_fd support in os.rename()")
1229 @unittest.skipUnless(hasattr(os, 'waitid_result'), "test needs os.waitid_result")
1231 os.CLD_EXITED
1232 os.CLD_KILLED
1233 os.CLD_DUMPED
1234 os.CLD_TRAPPED
1235 os.CLD_STOPPED
1236 os.CLD_CONTINUED
1238 …@unittest.skipUnless(os.symlink in os.supports_dir_fd, "test needs dir_fd support in os.symlink()")
1248 … @unittest.skipUnless(os.unlink in os.supports_dir_fd, "test needs dir_fd support in os.unlink()")
1263 … @unittest.skipUnless(os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()")
1310 parent = posix.sched_getscheduler(os.getppid())
1391 @unittest.skipUnless(hasattr(os, 'SEEK_HOLE'),
1392 "test needs an OS that reports file holes")
1395 # if the OS supports it the SEEK_* constants
1398 # os.SEEK_DATA = current position
1399 # os.SEEK_HOLE = end of file position
1407 self.assertEqual(i, os.lseek(fno, i, os.SEEK_DATA))
1408 self.assertLessEqual(size, os.lseek(fno, i, os.SEEK_HOLE))
1409 self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_DATA)
1410 self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_HOLE)
1423 function = getattr(os, name, None)
1434 self.fail("No valid path_error2() test for os." + name)
1444 fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises
1447 os.close(fd)
1448 self.assertFalse(os.path.exists(fn))
1449 self.assertRaises(ValueError, os.mkdir, fn_with_NUL)
1450 self.assertFalse(os.path.exists(fn))
1452 self.assertRaises(ValueError, os.stat, fn_with_NUL)
1455 fn = os.fsencode(support.TESTFN)
1462 fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises
1465 os.close(fd)
1466 self.assertFalse(os.path.exists(fn))
1467 self.assertRaises(ValueError, os.mkdir, fn_with_NUL)
1468 self.assertFalse(os.path.exists(fn))
1470 self.assertRaises(ValueError, os.stat, fn_with_NUL)
1472 @unittest.skipUnless(hasattr(os, "pidfd_open"), "pidfd_open unavailable")
1475 os.pidfd_open(-1)
1481 os.close(os.pidfd_open(os.getpid(), 0))
1536 import os
1538 pidfile.write(str(os.getpid()))
1541 pid = self.spawn_func(args[0], args, os.environ)
1551 os.environ)
1557 pid2, status = os.waitpid(pid, 0)
1565 import os
1567 envfile.write(os.environ['foo'])
1571 {**os.environ, 'foo': 'bar'})
1580 os.environ,
1589 os.environ,
1598 os.environ,
1607 os.environ,
1616 os.environ, resetids=None)
1622 os.environ,
1623 setpgroup=os.getpgrp()
1631 os.environ, setpgroup="023")
1643 os.environ,
1652 os.environ, setsigmask=34)
1656 os.environ, setsigmask=["j"])
1660 os.environ, setsigmask=[signal.NSIG,
1664 rfd, wfd = os.pipe()
1665 self.addCleanup(os.close, rfd)
1667 os.set_inheritable(wfd, True)
1670 import os
1672 sid = os.getsid(0)
1673 os.write(fd, str(sid).encode())
1679 os.environ, setsid=True)
1685 os.close(wfd)
1689 output = os.read(rfd, 100)
1691 parent_sid = os.getsid(os.getpid())
1705 os.environ,
1717 os.environ, setsigdef=34)
1721 os.environ, setsigdef=["j"])
1725 os.environ, setsigdef=[signal.NSIG, signal.NSIG+1])
1731 policy = os.sched_getscheduler(0)
1732 priority = os.sched_get_priority_min(policy)
1734 import os, sys
1735 if os.sched_getscheduler(0) != {policy}:
1737 if os.sched_getparam(0).sched_priority != {priority}:
1742 os.environ,
1743 scheduler=(None, os.sched_param(priority))
1751 policy = os.sched_getscheduler(0)
1752 priority = os.sched_get_priority_min(policy)
1754 import os, sys
1755 if os.sched_getscheduler(0) != {policy}:
1757 if os.sched_getparam(0).sched_priority != {priority}:
1762 os.environ,
1763 scheduler=(policy, os.sched_param(priority))
1769 (os.POSIX_SPAWN_OPEN, 3, os.path.realpath(__file__), os.O_RDONLY, 0),
1770 (os.POSIX_SPAWN_CLOSE, 0),
1771 (os.POSIX_SPAWN_DUP2, 1, 4),
1775 os.environ,
1782 self.spawn_func(args[0], args, os.environ,
1785 self.spawn_func(args[0], args, os.environ,
1788 self.spawn_func(args[0], args, os.environ,
1791 self.spawn_func(args[0], args, os.environ,
1794 self.spawn_func(args[0], args, os.environ,
1795 file_actions=[(os.POSIX_SPAWN_CLOSE,)])
1797 self.spawn_func(args[0], args, os.environ,
1798 file_actions=[(os.POSIX_SPAWN_CLOSE, 1, 2)])
1800 self.spawn_func(args[0], args, os.environ,
1801 file_actions=[(os.POSIX_SPAWN_CLOSE, None)])
1803 self.spawn_func(args[0], args, os.environ,
1804 file_actions=[(os.POSIX_SPAWN_OPEN,
1806 os.O_RDONLY, 0)])
1816 (os.POSIX_SPAWN_OPEN, 1, outfile,
1817 os.O_WRONLY | os.O_CREAT | os.O_TRUNC,
1821 pid = self.spawn_func(args[0], args, os.environ,
1832 import os
1834 os.fstat(0)
1840 pid = self.spawn_func(args[0], args, os.environ,
1841 file_actions=[(os.POSIX_SPAWN_CLOSE, 0)])
1856 (os.POSIX_SPAWN_DUP2, childfile.fileno(), 1),
1859 pid = self.spawn_func(args[0], args, os.environ,
1866 @unittest.skipUnless(hasattr(os, 'posix_spawn'), "test needs os.posix_spawn")
1871 @unittest.skipUnless(hasattr(os, 'posix_spawnp'), "test needs os.posix_spawnp")
1882 program_fullpath = os.path.join(temp_dir, program)
1883 os.symlink(sys.executable, program_fullpath)
1886 path = os.pathsep.join((temp_dir, os.environ['PATH']))
1892 import os
1896 pid = os.posix_spawnp(args[0], args, os.environ)
1901 # Use a subprocess to test os.posix_spawnp() with a modified PATH
1930 self.assertTrue(hasattr(os, "pwritev"), "os.pwritev is not available")
1931 self.assertTrue(hasattr(os, "preadv"), "os.readv is not available")
1934 self.assertFalse(hasattr(os, "pwritev"), "os.pwritev is available")
1935 self.assertFalse(hasattr(os, "preadv"), "os.readv is available")
1946 os.stat("file", dir_fd=0)
1957 os.access("file", os.R_OK, dir_fd=0)
1960 os.access("file", os.R_OK, follow_symlinks=False)
1963 os.access("file", os.R_OK, effective_ids=True)
1975 os.chmod("file", 0o644, dir_fd=0)
1987 os.chown("file", 0, 0, dir_fd=0)
1998 os.link("source", "target", src_dir_fd=0)
2001 os.link("source", "target", dst_dir_fd=0)
2004 os.link("source", "target", src_dir_fd=0, dst_dir_fd=0)
2008 link_path = os.path.join(base_path, "link")
2009 target_path = os.path.join(base_path, "target")
2010 source_path = os.path.join(base_path, "source")
2015 os.symlink("target", link_path)
2017 # Calling os.link should fail in the link(2) call, and
2022 os.link(source_path, link_path, follow_symlinks=True)
2025 os.link(source_path, link_path, follow_symlinks=False)
2036 …with self.assertRaisesRegex(TypeError, "listdir: path should be string, bytes, os.PathLike or None…
2037 os.listdir(0)
2039 …with self.assertRaisesRegex(TypeError, "scandir: path should be string, bytes, os.PathLike or None…
2040 os.scandir(0)
2051 os.mkdir("dir", dir_fd=0)
2062 os.rename("a", "b", src_dir_fd=0)
2065 os.rename("a", "b", dst_dir_fd=0)
2068 os.replace("a", "b", src_dir_fd=0)
2071 os.replace("a", "b", dst_dir_fd=0)
2082 os.unlink("path", dir_fd=0)
2085 os.rmdir("path", dir_fd=0)
2096 os.open("path", os.O_RDONLY, dir_fd=0)
2107 os.readlink("path", dir_fd=0)
2118 os.symlink("a", "b", dir_fd=0)
2132 os.utime("path", dir_fd=0)