Skip to content

Update test_posix.py from Cpython v3.11.2 #4890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 67 additions & 22 deletions Lib/test/test_posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,26 @@
import time
import os
import platform
import pwd
import stat
import tempfile
import unittest
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'):
Expand All @@ -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)
Expand All @@ -71,8 +72,9 @@ def testNoArgFunctions(self):
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()')
Expand Down Expand Up @@ -126,6 +128,7 @@ def test_setresgid_exception(self):

@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.
Expand Down Expand Up @@ -184,7 +187,7 @@ def test_truncate(self):
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:
Expand All @@ -199,7 +202,7 @@ def test_fexecve(self):


@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:
Expand All @@ -209,7 +212,7 @@ def test_waitid(self):
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)
Expand Down Expand Up @@ -544,6 +547,7 @@ def test_readv_overflow_32bits(self):

@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:
Expand All @@ -561,6 +565,7 @@ def test_confstr(self):

@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)
Expand All @@ -572,6 +577,7 @@ def test_dup2(self):

@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)
Expand Down Expand Up @@ -733,7 +739,11 @@ def check_stat(uid, gid):
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.)
Expand Down Expand Up @@ -778,7 +788,8 @@ def check_stat(uid, gid):
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)
Expand All @@ -788,7 +799,9 @@ def test_chown(self):
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)

Expand All @@ -801,6 +814,7 @@ def test_fchown(self):
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)
Expand Down Expand Up @@ -963,8 +977,8 @@ def test_lchflags_symlink(self):

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):
Expand Down Expand Up @@ -1060,6 +1074,7 @@ def test_getgrouplist(self):

@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()
Expand Down Expand Up @@ -1207,6 +1222,7 @@ def test_sched_setaffinity(self):
# 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
Expand Down Expand Up @@ -1346,6 +1362,7 @@ def test_chmod_dir_fd(self):

@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)
Expand Down Expand Up @@ -1401,7 +1418,14 @@ def test_utime_dir_fd(self):
# 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):
Expand Down Expand Up @@ -1489,8 +1513,7 @@ def test_unlink_dir_fd(self):
self.addCleanup(posix.unlink, fullname)
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:
Expand Down Expand Up @@ -2089,6 +2112,28 @@ def test_mkdir(self):
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):
Expand Down