Lines Matching full:os
16 import os
25 _DUMMY_SYMLINK = os.path.join(tempfile.gettempdir(),
127 "test needs os.initgroups()")
138 if os.getuid() != 0:
154 self.assertTrue(posix.statvfs(os.curdir))
185 …@unittest.skipUnless(getattr(os, 'execve', None) in os.supports_fd, "test needs execve() to suppor…
186 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
188 fp = os.open(sys.executable, os.O_RDONLY)
190 pid = os.fork()
192 os.chdir(os.path.split(sys.executable)[0])
193 posix.execve(fp, [sys.executable, '-c', 'pass'], os.environ)
197 os.close(fp)
201 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
203 pid = os.fork()
205 os.chdir(os.path.split(sys.executable)[0])
206 posix.execve(sys.executable, [sys.executable, '-c', 'pass'], os.environ)
211 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
214 os.register_at_fork(lambda: None)
216 os.register_at_fork(before=2)
218 os.register_at_fork(after_in_child="three")
220 os.register_at_fork(after_in_parent=b"Five")
222 os.register_at_fork(before=None)
224 os.register_at_fork(after_in_child=None)
226 os.register_at_fork(after_in_parent=None)
229 os.register_at_fork(before=None, after_in_parent=lambda: 3)
232 os.register_at_fork(before=lambda: None, after_in_child='')
236 import os
238 r, w = os.pipe()
239 fin_r, fin_w = os.pipe()
241 os.register_at_fork(before=lambda: os.write(w, b'A'))
242 os.register_at_fork(after_in_parent=lambda: os.write(w, b'C'))
243 os.register_at_fork(after_in_child=lambda: os.write(w, b'E'))
244 os.register_at_fork(before=lambda: os.write(w, b'B'),
245 after_in_parent=lambda: os.write(w, b'D'),
246 after_in_child=lambda: os.write(w, b'F'))
248 pid = os.fork()
251 os.close(w)
253 os.read(fin_r, 1)
254 os._exit(0)
257 os.close(w)
268 os.write(fin_w, b'!')
274 fd = os.open(os_helper.TESTFN, os.O_WRONLY | os.O_CREAT)
276 os.write(fd, b'test')
277 os.lseek(fd, 0, os.SEEK_SET)
282 os.close(fd)
286 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
288 os.write(fd, b'test')
289 os.lseek(fd, 0, os.SEEK_SET)
294 os.close(fd)
298 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
300 os.write(fd, b'test1tt2t3t5t6t6t8')
305 os.close(fd)
310 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
312 os.write(fd, b'test1tt2t3t5t6t6t8')
314 self.assertEqual(posix.preadv(fd, buf, 3, os.RWF_HIPRI), 10)
327 os.close(fd)
332 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
336 os.preadv(fd, buf, 0)
340 os.close(fd)
344 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
346 os.write(fd, b'test')
347 os.lseek(fd, 0, os.SEEK_SET)
351 os.close(fd)
355 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
357 os.write(fd, b"xx")
358 os.lseek(fd, 0, os.SEEK_SET)
359 n = os.pwritev(fd, [b'test1', b'tt2', b't3'], 2)
362 os.lseek(fd, 0, os.SEEK_SET)
365 os.close(fd)
368 @unittest.skipUnless(hasattr(posix, 'os.RWF_SYNC'), "test needs os.RWF_SYNC")
370 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
372 os.write(fd,b"xx")
373 os.lseek(fd, 0, os.SEEK_SET)
374 n = os.pwritev(fd, [b'test1', b'tt2', b't3'], 2, os.RWF_SYNC)
377 os.lseek(fd, 0, os.SEEK_SET)
380 os.close(fd)
385 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
388 os.pwritev(fd, [b"x" * 2**16] * 2**15, 0)
391 os.close(fd)
396 fd = os.open(os_helper.TESTFN, os.O_WRONLY | os.O_CREAT)
410 os.close(fd)
425 fd = os.open(os_helper.TESTFN, os.O_RDONLY)
429 os.close(fd)
440 @unittest.skipUnless(os.utime in os.supports_fd, "test needs fd support in os.utime")
443 fd = os.open(os_helper.TESTFN, os.O_RDONLY)
459 os.close(fd)
461 …@unittest.skipUnless(os.utime in os.supports_follow_symlinks, "test needs follow_symlinks support …
478 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
480 n = os.writev(fd, (b'test1', b'tt2', b't3'))
483 os.lseek(fd, 0, os.SEEK_SET)
496 os.close(fd)
501 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
504 os.writev(fd, [b"x" * 2**16] * 2**15)
507 os.close(fd)
511 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
513 os.write(fd, b'test1tt2t3')
514 os.lseek(fd, 0, os.SEEK_SET)
529 os.close(fd)
534 fd = os.open(os_helper.TESTFN, os.O_RDWR | os.O_CREAT)
538 os.readv(fd, buf)
542 os.close(fd)
551 os.close(fd)
572 @unittest.skipUnless(hasattr(os, 'O_CLOEXEC'), "needs os.O_CLOEXEC")
575 fd = os.open(os_helper.TESTFN, os.O_RDONLY|os.O_CLOEXEC)
576 self.addCleanup(os.close, fd)
577 self.assertFalse(os.get_inheritable(fd))
582 fd = os.open(os_helper.TESTFN,
583 os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
584 self.assertRaises(OSError, os.open, os_helper.TESTFN,
585 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
586 os.close(fd)
589 fd = os.open(os_helper.TESTFN,
590 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
591 self.assertRaises(OSError, os.open, os_helper.TESTFN,
592 os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
593 os.close(fd)
598 fd1 = os.open(os_helper.TESTFN,
599 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
600 fd2 = os.open(os_helper.TESTFN,
601 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
602 os.close(fd2)
603 os.close(fd1)
606 fd = os.open(os_helper.TESTFN,
607 os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
608 self.assertRaises(OSError, os.open, os_helper.TESTFN,
609 os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
610 os.close(fd)
621 'should be string, bytes, os.PathLike or integer, not',
628 self.assertTrue(posix.stat(os.fsencode(os_helper.TESTFN)))
631 'should be string, bytes, os.PathLike or integer, not',
632 posix.stat, bytearray(os.fsencode(os_helper.TESTFN)))
634 'should be string, bytes, os.PathLike or integer, not',
637 'should be string, bytes, os.PathLike or integer, not',
640 'should be string, bytes, os.PathLike or integer, not',
641 posix.stat, list(os.fsencode(os_helper.TESTFN)))
646 fifo_path = os.path.join("/fifos/", os_helper.TESTFN)
717 uid = os.getuid()
718 gid = os.getgid()
768 if 0 not in os.getgroups():
778 @unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()")
781 os.unlink(os_helper.TESTFN)
788 @unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()")
790 os.unlink(os_helper.TESTFN)
801 @unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()")
803 os.unlink(os_helper.TESTFN)
805 os.symlink(_DUMMY_SYMLINK, os_helper.TESTFN)
811 posix.chdir(os.curdir)
815 self.assertIn(os_helper.TESTFN, posix.listdir(os.curdir))
819 # it's the same as listdir(os.curdir).
825 self.assertIn(os.fsencode(os_helper.TESTFN), posix.listdir(b'.'))
831 self.assertIn(os.fsencode(os_helper.TESTFN), names)
835 @unittest.skipUnless(posix.listdir in os.supports_fd,
852 self.assertTrue(posix.access(os_helper.TESTFN, os.R_OK))
868 os.close(reader)
869 os.close(writer)
871 @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()")
874 self.assertRaises(TypeError, os.pipe2, 'DEADBEEF')
875 self.assertRaises(TypeError, os.pipe2, 0, 0)
877 # try calling with flags = 0, like os.pipe()
878 r, w = os.pipe2(0)
879 os.close(r)
880 os.close(w)
883 r, w = os.pipe2(os.O_CLOEXEC|os.O_NONBLOCK)
884 self.addCleanup(os.close, r)
885 self.addCleanup(os.close, w)
886 self.assertFalse(os.get_inheritable(r))
887 self.assertFalse(os.get_inheritable(w))
888 self.assertFalse(os.get_blocking(r))
889 self.assertFalse(os.get_blocking(w))
891 self.assertRaises(OSError, os.read, r, 1)
895 os.write(w, b'x' * support.PIPE_MAX_SIZE)
900 @unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()")
905 self.assertRaises(OverflowError, os.pipe2, _testcapi.INT_MAX + 1)
906 self.assertRaises(OverflowError, os.pipe2, _testcapi.UINT_MAX + 1)
922 st = os.stat(target_file)
936 new_st = os.stat(target_file)
945 @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()')
949 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
955 @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()')
957 testfn_st = os.stat(os_helper.TESTFN)
961 os.symlink(os_helper.TESTFN, _DUMMY_SYMLINK)
963 dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
979 new_testfn_st = os.stat(os_helper.TESTFN)
980 new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
989 if os.name == "nt":
999 os.putenv('FRUIT\0VEGETABLE', 'cabbage')
1001 os.putenv(b'FRUIT\0VEGETABLE', b'cabbage')
1003 os.putenv('FRUIT', 'orange\0VEGETABLE=cabbage')
1005 os.putenv(b'FRUIT', b'orange\0VEGETABLE=cabbage')
1007 os.putenv('FRUIT=ORANGE', 'lemon')
1009 os.putenv(b'FRUIT=ORANGE', b'lemon')
1014 curdir = os.getcwd()
1015 base_path = os.path.abspath(os_helper.TESTFN) + '.getcwd'
1018 os.mkdir(base_path)
1019 os.chdir(base_path)
1028 os.mkdir(dirname)
1032 os.chdir(dirname)
1034 os.getcwd()
1038 os.chdir('..')
1039 os.rmdir(dirname)
1044 os.chdir(curdir)
1049 @unittest.skipUnless(hasattr(os, 'getuid'), "test needs os.getuid()")
1051 user = pwd.getpwuid(os.getuid())[0]
1052 group = pwd.getpwuid(os.getuid())[3]
1056 @unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()")
1057 @unittest.skipUnless(hasattr(os, 'popen'), "test needs os.popen()")
1059 with os.popen('id -G 2>/dev/null') as idg:
1070 # Issues 16698: OS X ABIs prior to 10.6 have limits on getgroups()
1077 # 'id -G' and 'os.getgroups()' should return the same
1086 … @unittest.skipUnless(os.access in os.supports_dir_fd, "test needs dir_fd support for os.access()")
1090 self.assertTrue(posix.access(os_helper.TESTFN, os.R_OK, dir_fd=f))
1094 @unittest.skipUnless(os.chmod in os.supports_dir_fd, "test needs dir_fd support in os.chmod()")
1096 os.chmod(os_helper.TESTFN, stat.S_IRUSR)
1107 @unittest.skipUnless(hasattr(os, 'chown') and (os.chown in os.supports_dir_fd),
1108 "test needs dir_fd support in os.chown()")
1115 posix.chown(os_helper.TESTFN, os.getuid(), os.getgid(), dir_fd=f)
1119 @unittest.skipUnless(os.stat in os.supports_dir_fd, "test needs dir_fd support in os.stat()")
1141 @unittest.skipUnless(os.utime in os.supports_dir_fd, "test needs dir_fd support in os.utime()")
1166 if os.utime in os.supports_follow_symlinks:
1177 @unittest.skipUnless(os.link in os.supports_dir_fd, "test needs dir_fd support in os.link()")
1193 @unittest.skipUnless(os.mkdir in os.supports_dir_fd, "test needs dir_fd support in os.mkdir()")
1203 @unittest.skipUnless(hasattr(os, 'mknod')
1204 and (os.mknod in os.supports_dir_fd)
1206 "test requires both stat.S_IFIFO and dir_fd support for os.mknod()")
1224 @unittest.skipUnless(os.open in os.supports_dir_fd, "test needs dir_fd support in os.open()")
1238 @unittest.skipUnless(hasattr(os, 'readlink') and (os.readlink in os.supports_dir_fd),
1239 "test needs dir_fd support in os.readlink()")
1241 os.symlink(os_helper.TESTFN, os_helper.TESTFN + 'link')
1250 … @unittest.skipUnless(os.rename in os.supports_dir_fd, "test needs dir_fd support in os.rename()")
1266 @unittest.skipUnless(hasattr(os, 'waitid_result'), "test needs os.waitid_result")
1268 os.CLD_EXITED
1269 os.CLD_KILLED
1270 os.CLD_DUMPED
1271 os.CLD_TRAPPED
1272 os.CLD_STOPPED
1273 os.CLD_CONTINUED
1275 …@unittest.skipUnless(os.symlink in os.supports_dir_fd, "test needs dir_fd support in os.symlink()")
1287 … @unittest.skipUnless(os.unlink in os.supports_dir_fd, "test needs dir_fd support in os.unlink()")
1302 … @unittest.skipUnless(os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()")
1349 parent = posix.sched_getscheduler(os.getppid())
1430 @unittest.skipUnless(hasattr(os, 'SEEK_HOLE'),
1431 "test needs an OS that reports file holes")
1434 # if the OS supports it the SEEK_* constants
1437 # os.SEEK_DATA = current position
1438 # os.SEEK_HOLE = end of file position
1446 self.assertEqual(i, os.lseek(fno, i, os.SEEK_DATA))
1447 self.assertLessEqual(size, os.lseek(fno, i, os.SEEK_HOLE))
1448 self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_DATA)
1449 self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_HOLE)
1462 function = getattr(os, name, None)
1473 self.fail("No valid path_error2() test for os." + name)
1483 fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises
1486 os.close(fd)
1487 self.assertFalse(os.path.exists(fn))
1488 self.assertRaises(ValueError, os.mkdir, fn_with_NUL)
1489 self.assertFalse(os.path.exists(fn))
1491 self.assertRaises(ValueError, os.stat, fn_with_NUL)
1494 fn = os.fsencode(os_helper.TESTFN)
1501 fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises
1504 os.close(fd)
1505 self.assertFalse(os.path.exists(fn))
1506 self.assertRaises(ValueError, os.mkdir, fn_with_NUL)
1507 self.assertFalse(os.path.exists(fn))
1509 self.assertRaises(ValueError, os.stat, fn_with_NUL)
1511 @unittest.skipUnless(hasattr(os, "pidfd_open"), "pidfd_open unavailable")
1514 os.pidfd_open(-1)
1520 os.close(os.pidfd_open(os.getpid(), 0))
1575 import os
1577 pidfile.write(str(os.getpid()))
1580 pid = self.spawn_func(args[0], args, os.environ)
1590 os.environ)
1596 pid2, status = os.waitpid(pid, 0)
1604 import os
1606 envfile.write(os.environ['foo'])
1610 {**os.environ, 'foo': 'bar'})
1619 os.environ,
1628 os.environ,
1637 os.environ,
1646 os.environ,
1655 os.environ, resetids=None)
1661 os.environ,
1662 setpgroup=os.getpgrp()
1670 os.environ, setpgroup="023")
1682 os.environ,
1691 os.environ, setsigmask=34)
1695 os.environ, setsigmask=["j"])
1699 os.environ, setsigmask=[signal.NSIG,
1703 rfd, wfd = os.pipe()
1704 self.addCleanup(os.close, rfd)
1706 os.set_inheritable(wfd, True)
1709 import os
1711 sid = os.getsid(0)
1712 os.write(fd, str(sid).encode())
1718 os.environ, setsid=True)
1724 os.close(wfd)
1728 output = os.read(rfd, 100)
1730 parent_sid = os.getsid(os.getpid())
1744 os.environ,
1756 os.environ, setsigdef=34)
1760 os.environ, setsigdef=["j"])
1764 os.environ, setsigdef=[signal.NSIG, signal.NSIG+1])
1770 policy = os.sched_getscheduler(0)
1771 priority = os.sched_get_priority_min(policy)
1773 import os, sys
1774 if os.sched_getscheduler(0) != {policy}:
1776 if os.sched_getparam(0).sched_priority != {priority}:
1781 os.environ,
1782 scheduler=(None, os.sched_param(priority))
1790 policy = os.sched_getscheduler(0)
1791 priority = os.sched_get_priority_min(policy)
1793 import os, sys
1794 if os.sched_getscheduler(0) != {policy}:
1796 if os.sched_getparam(0).sched_priority != {priority}:
1801 os.environ,
1802 scheduler=(policy, os.sched_param(priority))
1808 (os.POSIX_SPAWN_OPEN, 3, os.path.realpath(__file__), os.O_RDONLY, 0),
1809 (os.POSIX_SPAWN_CLOSE, 0),
1810 (os.POSIX_SPAWN_DUP2, 1, 4),
1814 os.environ,
1821 self.spawn_func(args[0], args, os.environ,
1824 self.spawn_func(args[0], args, os.environ,
1827 self.spawn_func(args[0], args, os.environ,
1830 self.spawn_func(args[0], args, os.environ,
1833 self.spawn_func(args[0], args, os.environ,
1834 file_actions=[(os.POSIX_SPAWN_CLOSE,)])
1836 self.spawn_func(args[0], args, os.environ,
1837 file_actions=[(os.POSIX_SPAWN_CLOSE, 1, 2)])
1839 self.spawn_func(args[0], args, os.environ,
1840 file_actions=[(os.POSIX_SPAWN_CLOSE, None)])
1842 self.spawn_func(args[0], args, os.environ,
1843 file_actions=[(os.POSIX_SPAWN_OPEN,
1845 os.O_RDONLY, 0)])
1855 (os.POSIX_SPAWN_OPEN, 1, outfile,
1856 os.O_WRONLY | os.O_CREAT | os.O_TRUNC,
1860 pid = self.spawn_func(args[0], args, os.environ,
1871 import os
1873 os.fstat(0)
1879 pid = self.spawn_func(args[0], args, os.environ,
1880 file_actions=[(os.POSIX_SPAWN_CLOSE, 0)])
1895 (os.POSIX_SPAWN_DUP2, childfile.fileno(), 1),
1898 pid = self.spawn_func(args[0], args, os.environ,
1905 @unittest.skipUnless(hasattr(os, 'posix_spawn'), "test needs os.posix_spawn")
1910 @unittest.skipUnless(hasattr(os, 'posix_spawnp'), "test needs os.posix_spawnp")
1921 program_fullpath = os.path.join(temp_dir, program)
1922 os.symlink(sys.executable, program_fullpath)
1925 path = os.pathsep.join((temp_dir, os.environ['PATH']))
1931 import os
1935 pid = os.posix_spawnp(args[0], args, os.environ)
1940 # Use a subprocess to test os.posix_spawnp() with a modified PATH
1969 self.assertTrue(hasattr(os, "pwritev"), "os.pwritev is not available")
1970 self.assertTrue(hasattr(os, "preadv"), "os.readv is not available")
1973 self.assertFalse(hasattr(os, "pwritev"), "os.pwritev is available")
1974 self.assertFalse(hasattr(os, "preadv"), "os.readv is available")
1985 os.stat("file", dir_fd=0)
1996 os.access("file", os.R_OK, dir_fd=0)
1999 os.access("file", os.R_OK, follow_symlinks=False)
2002 os.access("file", os.R_OK, effective_ids=True)
2014 os.chmod("file", 0o644, dir_fd=0)
2026 os.chown("file", 0, 0, dir_fd=0)
2037 os.link("source", "target", src_dir_fd=0)
2040 os.link("source", "target", dst_dir_fd=0)
2043 os.link("source", "target", src_dir_fd=0, dst_dir_fd=0)
2047 link_path = os.path.join(base_path, "link")
2048 target_path = os.path.join(base_path, "target")
2049 source_path = os.path.join(base_path, "source")
2054 os.symlink("target", link_path)
2056 # Calling os.link should fail in the link(2) call, and
2061 os.link(source_path, link_path, follow_symlinks=True)
2064 os.link(source_path, link_path, follow_symlinks=False)
2075 …with self.assertRaisesRegex(TypeError, "listdir: path should be string, bytes, os.PathLike or None…
2076 os.listdir(0)
2078 …with self.assertRaisesRegex(TypeError, "scandir: path should be string, bytes, os.PathLike or None…
2079 os.scandir(0)
2090 os.mkdir("dir", dir_fd=0)
2101 os.rename("a", "b", src_dir_fd=0)
2104 os.rename("a", "b", dst_dir_fd=0)
2107 os.replace("a", "b", src_dir_fd=0)
2110 os.replace("a", "b", dst_dir_fd=0)
2121 os.unlink("path", dir_fd=0)
2124 os.rmdir("path", dir_fd=0)
2135 os.open("path", os.O_RDONLY, dir_fd=0)
2146 os.readlink("path", dir_fd=0)
2157 os.symlink("a", "b", dir_fd=0)
2171 os.utime("path", dir_fd=0)