Update test_posix.py from Cpython v3.11.2

This commit is contained in:
Andrey Maltsev
2023-04-18 19:15:37 +00:00
parent 5f17d281a6
commit 415fac82b2

View File

@@ -15,7 +15,6 @@ import signal
import time
import os
import platform
import pwd
import stat
import tempfile
import unittest
@@ -23,11 +22,19 @@ import warnings
import textwrap
from contextlib import contextmanager
try:
import pwd
except ImportError:
pwd = None
_DUMMY_SYMLINK = os.path.join(tempfile.gettempdir(),
os_helper.TESTFN + '-dummy-symlink')
requires_32b = unittest.skipUnless(sys.maxsize < 2**32,
'test is only meaningful on 32-bit builds')
requires_32b = unittest.skipUnless(
# Emscripten/WASI have 32 bits pointers, but support 64 bits syscall args.
sys.maxsize < 2**32 and not (support.is_emscripten or support.is_wasi),
'test is only meaningful on 32-bit builds'
)
def _supports_sched():
if not hasattr(posix, 'sched_getscheduler'):
@@ -46,19 +53,13 @@ class PosixTester(unittest.TestCase):
def setUp(self):
# create empty file
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
with open(os_helper.TESTFN, "wb"):
pass
self.teardown_files = [ os_helper.TESTFN ]
self._warnings_manager = warnings_helper.check_warnings()
self._warnings_manager.__enter__()
self.enterContext(warnings_helper.check_warnings())
warnings.filterwarnings('ignore', '.* potential security risk .*',
RuntimeWarning)
def tearDown(self):
for teardown_file in self.teardown_files:
os_helper.unlink(teardown_file)
self._warnings_manager.__exit__(None, None, None)
def testNoArgFunctions(self):
# test posix functions which take no arguments and have
# no side-effects which we need to cleanup (e.g., fork, wait, abort)
@@ -71,8 +72,9 @@ class PosixTester(unittest.TestCase):
for name in NO_ARG_FUNCTIONS:
posix_func = getattr(posix, name, None)
if posix_func is not None:
posix_func()
self.assertRaises(TypeError, posix_func, 1)
with self.subTest(name):
posix_func()
self.assertRaises(TypeError, posix_func, 1)
@unittest.skipUnless(hasattr(posix, 'getresuid'),
'test needs posix.getresuid()')
@@ -126,6 +128,7 @@ class PosixTester(unittest.TestCase):
@unittest.skipUnless(hasattr(posix, 'initgroups'),
"test needs os.initgroups()")
@unittest.skipUnless(hasattr(pwd, 'getpwuid'), "test needs pwd.getpwuid()")
def test_initgroups(self):
# It takes a string and an integer; check that it raises a TypeError
# for other argument lists.
@@ -184,7 +187,7 @@ class PosixTester(unittest.TestCase):
posix.truncate(os_helper.TESTFN, 0)
@unittest.skipUnless(getattr(os, 'execve', None) in os.supports_fd, "test needs execve() to support the fd parameter")
@unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
@support.requires_fork()
def test_fexecve(self):
fp = os.open(sys.executable, os.O_RDONLY)
try:
@@ -199,7 +202,7 @@ class PosixTester(unittest.TestCase):
@unittest.skipUnless(hasattr(posix, 'waitid'), "test needs posix.waitid()")
@unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
@support.requires_fork()
def test_waitid(self):
pid = os.fork()
if pid == 0:
@@ -209,7 +212,7 @@ class PosixTester(unittest.TestCase):
res = posix.waitid(posix.P_PID, pid, posix.WEXITED)
self.assertEqual(pid, res.si_pid)
@unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()")
@support.requires_fork()
def test_register_at_fork(self):
with self.assertRaises(TypeError, msg="Positional args not allowed"):
os.register_at_fork(lambda: None)
@@ -544,6 +547,7 @@ class PosixTester(unittest.TestCase):
@unittest.skipUnless(hasattr(posix, 'dup'),
'test needs posix.dup()')
@unittest.skipIf(support.is_wasi, "WASI does not have dup()")
def test_dup(self):
fp = open(os_helper.TESTFN)
try:
@@ -561,6 +565,7 @@ class PosixTester(unittest.TestCase):
@unittest.skipUnless(hasattr(posix, 'dup2'),
'test needs posix.dup2()')
@unittest.skipIf(support.is_wasi, "WASI does not have dup2()")
def test_dup2(self):
fp1 = open(os_helper.TESTFN)
fp2 = open(os_helper.TESTFN)
@@ -572,6 +577,7 @@ class PosixTester(unittest.TestCase):
@unittest.skipUnless(hasattr(os, 'O_CLOEXEC'), "needs os.O_CLOEXEC")
@support.requires_linux_version(2, 6, 23)
@support.requires_subprocess()
def test_oscloexec(self):
fd = os.open(os_helper.TESTFN, os.O_RDONLY|os.O_CLOEXEC)
self.addCleanup(os.close, fd)
@@ -733,7 +739,11 @@ class PosixTester(unittest.TestCase):
is_root = (uid in (0, 1))
else:
is_root = (uid == 0)
if is_root:
if support.is_emscripten:
# Emscripten getuid() / geteuid() always return 0 (root), but
# cannot chown uid/gid to random value.
pass
elif is_root:
# Try an amusingly large uid/gid to make sure we handle
# large unsigned values. (chown lets you use any
# uid/gid you like, even if they aren't defined.)
@@ -778,7 +788,8 @@ class PosixTester(unittest.TestCase):
self.assertRaises(TypeError, chown_func, first_param, uid, t(gid))
check_stat(uid, gid)
@unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()")
@os_helper.skip_unless_working_chmod
@unittest.skipIf(support.is_emscripten, "getgid() is a stub")
def test_chown(self):
# raise an OSError if the file does not exist
os.unlink(os_helper.TESTFN)
@@ -788,7 +799,9 @@ class PosixTester(unittest.TestCase):
os_helper.create_empty_file(os_helper.TESTFN)
self._test_all_chown_common(posix.chown, os_helper.TESTFN, posix.stat)
@os_helper.skip_unless_working_chmod
@unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()")
@unittest.skipIf(support.is_emscripten, "getgid() is a stub")
def test_fchown(self):
os.unlink(os_helper.TESTFN)
@@ -801,6 +814,7 @@ class PosixTester(unittest.TestCase):
finally:
test_file.close()
@os_helper.skip_unless_working_chmod
@unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()")
def test_lchown(self):
os.unlink(os_helper.TESTFN)
@@ -963,8 +977,8 @@ class PosixTester(unittest.TestCase):
self.assertTrue(hasattr(testfn_st, 'st_flags'))
self.addCleanup(os_helper.unlink, _DUMMY_SYMLINK)
os.symlink(os_helper.TESTFN, _DUMMY_SYMLINK)
self.teardown_files.append(_DUMMY_SYMLINK)
dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
def chflags_nofollow(path, flags):
@@ -1060,6 +1074,7 @@ class PosixTester(unittest.TestCase):
@unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()")
@unittest.skipUnless(hasattr(os, 'popen'), "test needs os.popen()")
@support.requires_subprocess()
def test_getgroups(self):
with os.popen('id -G 2>/dev/null') as idg:
groups = idg.read().strip()
@@ -1207,6 +1222,7 @@ class PosixTester(unittest.TestCase):
# bpo-47205: does not raise OSError on FreeBSD
self.assertRaises(OSError, posix.sched_setaffinity, -1, mask)
@unittest.skipIf(support.is_wasi, "No dynamic linking on WASI")
def test_rtld_constants(self):
# check presence of major RTLD_* constants
posix.RTLD_LAZY
@@ -1346,6 +1362,7 @@ class TestPosixDirFd(unittest.TestCase):
@unittest.skipUnless(hasattr(os, 'chown') and (os.chown in os.supports_dir_fd),
"test needs dir_fd support in os.chown()")
@unittest.skipIf(support.is_emscripten, "getgid() is a stub")
def test_chown_dir_fd(self):
with self.prepare_file() as (dir_fd, name, fullname):
posix.chown(name, os.getuid(), os.getgid(), dir_fd=dir_fd)
@@ -1401,7 +1418,14 @@ class TestPosixDirFd(unittest.TestCase):
# whoops! using both together not supported on this platform.
pass
@unittest.skipUnless(os.link in os.supports_dir_fd, "test needs dir_fd support in os.link()")
@unittest.skipIf(
support.is_wasi,
"WASI: symlink following on path_link is not supported"
)
@unittest.skipUnless(
hasattr(os, "link") and os.link in os.supports_dir_fd,
"test needs dir_fd support in os.link()"
)
def test_link_dir_fd(self):
with self.prepare_file() as (dir_fd, name, fullname), \
self.prepare() as (dir_fd2, linkname, fulllinkname):
@@ -1490,7 +1514,7 @@ class TestPosixDirFd(unittest.TestCase):
raise
@unittest.skip("TODO: RUSTPYTHON; no os.mkfifo")
# @unittest.skipUnless(os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()")
# @unittest.skipUnless(hasattr(os, 'mkfifo') and os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()")
def test_mkfifo_dir_fd(self):
with self.prepare() as (dir_fd, name, fullname):
try:
@@ -2089,6 +2113,28 @@ class TestPosixWeaklinking(unittest.TestCase):
with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
os.mkdir("dir", dir_fd=0)
def test_mkfifo(self):
self._verify_available("HAVE_MKFIFOAT")
if self.mac_ver >= (13, 0):
self.assertIn("HAVE_MKFIFOAT", posix._have_functions)
else:
self.assertNotIn("HAVE_MKFIFOAT", posix._have_functions)
with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
os.mkfifo("path", dir_fd=0)
def test_mknod(self):
self._verify_available("HAVE_MKNODAT")
if self.mac_ver >= (13, 0):
self.assertIn("HAVE_MKNODAT", posix._have_functions)
else:
self.assertNotIn("HAVE_MKNODAT", posix._have_functions)
with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
os.mknod("path", dir_fd=0)
def test_rename_replace(self):
self._verify_available("HAVE_RENAMEAT")
if self.mac_ver >= (10, 10):