Lines Matching full:os
13 import os
22 _DUMMY_SYMLINK = os.path.join(tempfile.gettempdir(),
123 "test needs os.initgroups()")
134 if os.getuid() != 0:
150 self.assertTrue(posix.statvfs(os.curdir))
181 …@unittest.skipUnless(getattr(os, 'execve', None) in os.supports_fd, "test needs execve() to suppor…
182 @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
183 @unittest.skipUnless(hasattr(os, 'waitpid'), "test needs os.waitpid()")
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)
192 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
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":
972 @unittest.skipUnless(hasattr(os, "putenv"), "requires os.putenv()")
975 os.putenv('FRUIT\0VEGETABLE', 'cabbage')
977 os.putenv(b'FRUIT\0VEGETABLE', b'cabbage')
979 os.putenv('FRUIT', 'orange\0VEGETABLE=cabbage')
981 os.putenv(b'FRUIT', b'orange\0VEGETABLE=cabbage')
983 os.putenv('FRUIT=ORANGE', 'lemon')
985 os.putenv(b'FRUIT=ORANGE', b'lemon')
990 curdir = os.getcwd()
991 base_path = os.path.abspath(support.TESTFN) + '.getcwd'
994 os.mkdir(base_path)
995 os.chdir(base_path)
1004 os.mkdir(dirname)
1008 os.chdir(dirname)
1010 os.getcwd()
1014 os.chdir('..')
1015 os.rmdir(dirname)
1020 os.chdir(curdir)
1025 @unittest.skipUnless(hasattr(os, 'getuid'), "test needs os.getuid()")
1027 user = pwd.getpwuid(os.getuid())[0]
1028 group = pwd.getpwuid(os.getuid())[3]
1032 @unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()")
1034 with os.popen('id -G 2>/dev/null') as idg:
1045 # Issues 16698: OS X ABIs prior to 10.6 have limits on getgroups()
1052 # 'id -G' and 'os.getgroups()' should return the same
1061 … @unittest.skipUnless(os.access in os.supports_dir_fd, "test needs dir_fd support for os.access()")
1065 self.assertTrue(posix.access(support.TESTFN, os.R_OK, dir_fd=f))
1069 @unittest.skipUnless(os.chmod in os.supports_dir_fd, "test needs dir_fd support in os.chmod()")
1071 os.chmod(support.TESTFN, stat.S_IRUSR)
1082 @unittest.skipUnless(os.chown in os.supports_dir_fd, "test needs dir_fd support in os.chown()")
1089 posix.chown(support.TESTFN, os.getuid(), os.getgid(), dir_fd=f)
1093 @unittest.skipUnless(os.stat in os.supports_dir_fd, "test needs dir_fd support in os.stat()")
1115 @unittest.skipUnless(os.utime in os.supports_dir_fd, "test needs dir_fd support in os.utime()")
1135 if os.utime in os.supports_follow_symlinks:
1145 @unittest.skipUnless(os.link in os.supports_dir_fd, "test needs dir_fd support in os.link()")
1160 @unittest.skipUnless(os.mkdir in os.supports_dir_fd, "test needs dir_fd support in os.mkdir()")
1170 @unittest.skipUnless((os.mknod in os.supports_dir_fd) and hasattr(stat, 'S_IFIFO'),
1171 "test requires both stat.S_IFIFO and dir_fd support for os.mknod()")
1189 @unittest.skipUnless(os.open in os.supports_dir_fd, "test needs dir_fd support in os.open()")
1203 …@unittest.skipUnless(os.readlink in os.supports_dir_fd, "test needs dir_fd support in os.readlink(…
1205 os.symlink(support.TESTFN, support.TESTFN + 'link')
1214 … @unittest.skipUnless(os.rename in os.supports_dir_fd, "test needs dir_fd support in os.rename()")
1229 …@unittest.skipUnless(os.symlink in os.supports_dir_fd, "test needs dir_fd support in os.symlink()")
1239 … @unittest.skipUnless(os.unlink in os.supports_dir_fd, "test needs dir_fd support in os.unlink()")
1254 … @unittest.skipUnless(os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()")
1301 parent = posix.sched_getscheduler(os.getppid())
1382 @unittest.skipUnless(hasattr(os, 'SEEK_HOLE'),
1383 "test needs an OS that reports file holes")
1386 # if the OS supports it the SEEK_* constants
1389 # os.SEEK_DATA = current position
1390 # os.SEEK_HOLE = end of file position
1398 self.assertEqual(i, os.lseek(fno, i, os.SEEK_DATA))
1399 self.assertLessEqual(size, os.lseek(fno, i, os.SEEK_HOLE))
1400 self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_DATA)
1401 self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_HOLE)
1414 function = getattr(os, name, None)
1425 self.fail("No valid path_error2() test for os." + name)
1435 fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises
1438 os.close(fd)
1439 self.assertFalse(os.path.exists(fn))
1440 self.assertRaises(ValueError, os.mkdir, fn_with_NUL)
1441 self.assertFalse(os.path.exists(fn))
1443 self.assertRaises(ValueError, os.stat, fn_with_NUL)
1446 fn = os.fsencode(support.TESTFN)
1453 fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises
1456 os.close(fd)
1457 self.assertFalse(os.path.exists(fn))
1458 self.assertRaises(ValueError, os.mkdir, fn_with_NUL)
1459 self.assertFalse(os.path.exists(fn))
1461 self.assertRaises(ValueError, os.stat, fn_with_NUL)
1516 import os
1518 pidfile.write(str(os.getpid()))
1521 pid = self.spawn_func(args[0], args, os.environ)
1522 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
1531 os.environ)
1537 pid2, status = os.waitpid(pid, 0)
1545 import os
1547 envfile.write(os.environ['foo'])
1551 {**os.environ, 'foo': 'bar'})
1552 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
1560 os.environ,
1563 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
1569 os.environ,
1572 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
1578 os.environ,
1581 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
1587 os.environ,
1590 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
1596 os.environ, resetids=None)
1602 os.environ,
1603 setpgroup=os.getpgrp()
1605 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
1611 os.environ, setpgroup="023")
1623 os.environ,
1626 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
1632 os.environ, setsigmask=34)
1636 os.environ, setsigmask=["j"])
1640 os.environ, setsigmask=[signal.NSIG,
1644 rfd, wfd = os.pipe()
1645 self.addCleanup(os.close, rfd)
1647 os.set_inheritable(wfd, True)
1650 import os
1652 sid = os.getsid(0)
1653 os.write(fd, str(sid).encode())
1659 os.environ, setsid=True)
1665 os.close(wfd)
1667 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
1668 output = os.read(rfd, 100)
1670 parent_sid = os.getsid(os.getpid())
1684 os.environ,
1690 pid2, status = os.waitpid(pid, 0)
1692 self.assertTrue(os.WIFSIGNALED(status), status)
1693 self.assertEqual(os.WTERMSIG(status), signal.SIGUSR1)
1699 os.environ, setsigdef=34)
1703 os.environ, setsigdef=["j"])
1707 os.environ, setsigdef=[signal.NSIG, signal.NSIG+1])
1713 policy = os.sched_getscheduler(0)
1714 priority = os.sched_get_priority_min(policy)
1716 import os, sys
1717 if os.sched_getscheduler(0) != {policy}:
1719 if os.sched_getparam(0).sched_priority != {priority}:
1724 os.environ,
1725 scheduler=(None, os.sched_param(priority))
1727 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
1733 policy = os.sched_getscheduler(0)
1734 priority = os.sched_get_priority_min(policy)
1736 import os, sys
1737 if os.sched_getscheduler(0) != {policy}:
1739 if os.sched_getparam(0).sched_priority != {priority}:
1744 os.environ,
1745 scheduler=(policy, os.sched_param(priority))
1747 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
1751 (os.POSIX_SPAWN_OPEN, 3, os.path.realpath(__file__), os.O_RDONLY, 0),
1752 (os.POSIX_SPAWN_CLOSE, 0),
1753 (os.POSIX_SPAWN_DUP2, 1, 4),
1757 os.environ,
1759 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
1764 self.spawn_func(args[0], args, os.environ,
1767 self.spawn_func(args[0], args, os.environ,
1770 self.spawn_func(args[0], args, os.environ,
1773 self.spawn_func(args[0], args, os.environ,
1776 self.spawn_func(args[0], args, os.environ,
1777 file_actions=[(os.POSIX_SPAWN_CLOSE,)])
1779 self.spawn_func(args[0], args, os.environ,
1780 file_actions=[(os.POSIX_SPAWN_CLOSE, 1, 2)])
1782 self.spawn_func(args[0], args, os.environ,
1783 file_actions=[(os.POSIX_SPAWN_CLOSE, None)])
1785 self.spawn_func(args[0], args, os.environ,
1786 file_actions=[(os.POSIX_SPAWN_OPEN,
1788 os.O_RDONLY, 0)])
1798 (os.POSIX_SPAWN_OPEN, 1, outfile,
1799 os.O_WRONLY | os.O_CREAT | os.O_TRUNC,
1803 pid = self.spawn_func(args[0], args, os.environ,
1805 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
1813 import os
1815 os.fstat(0)
1821 pid = self.spawn_func(args[0], args, os.environ,
1822 file_actions=[(os.POSIX_SPAWN_CLOSE, 0)])
1823 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
1836 (os.POSIX_SPAWN_DUP2, childfile.fileno(), 1),
1839 pid = self.spawn_func(args[0], args, os.environ,
1841 self.assertEqual(os.waitpid(pid, 0), (pid, 0))
1846 @unittest.skipUnless(hasattr(os, 'posix_spawn'), "test needs os.posix_spawn")
1851 @unittest.skipUnless(hasattr(os, 'posix_spawnp'), "test needs os.posix_spawnp")
1862 program_fullpath = os.path.join(temp_dir, program)
1863 os.symlink(sys.executable, program_fullpath)
1866 path = os.pathsep.join((temp_dir, os.environ['PATH']))
1872 import os
1874 pid = os.posix_spawnp(args[0], args, os.environ)
1875 pid2, status = os.waitpid(pid, 0)
1882 # Use a subprocess to test os.posix_spawnp() with a modified PATH