From 415fac82b23d2501937f3aac8ed61474dcfdf25c Mon Sep 17 00:00:00 2001 From: Andrey Maltsev Date: Tue, 18 Apr 2023 19:15:37 +0000 Subject: [PATCH 1/3] Update test_posix.py from Cpython v3.11.2 --- Lib/test/test_posix.py | 88 ++++++++++++++++++++++++++++++++---------- 1 file changed, 67 insertions(+), 21 deletions(-) diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py index 9a20ba7ab9..c0124904c1 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -15,7 +15,6 @@ import time import os import platform -import pwd import stat import tempfile import unittest @@ -23,11 +22,19 @@ 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 @@ 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()') @@ -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. @@ -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: @@ -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: @@ -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) @@ -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: @@ -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) @@ -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) @@ -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.) @@ -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) @@ -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) @@ -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) @@ -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): @@ -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() @@ -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 @@ -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) @@ -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): @@ -1490,7 +1514,7 @@ def test_unlink_dir_fd(self): 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 @@ 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): From 536da88df7bf1640caf91b868658bc3129f76e83 Mon Sep 17 00:00:00 2001 From: Andrey Maltsev <43664538+Masorubka1@users.noreply.github.com> Date: Tue, 18 Apr 2023 22:25:32 +0300 Subject: [PATCH 2/3] Update Lib/test/test_posix.py Co-authored-by: fanninpm --- Lib/test/test_posix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py index c0124904c1..521c9a1809 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -1514,7 +1514,7 @@ def test_unlink_dir_fd(self): raise @unittest.skip("TODO: RUSTPYTHON; no os.mkfifo") - # @unittest.skipUnless(hasattr(os, 'mkfifo') and 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: From 2d7ec431735aae512cd8961ca873fc8c02f1a177 Mon Sep 17 00:00:00 2001 From: Andrey Maltsev <43664538+Masorubka1@users.noreply.github.com> Date: Tue, 18 Apr 2023 22:36:03 +0300 Subject: [PATCH 3/3] Update Lib/test/test_posix.py Co-authored-by: fanninpm --- Lib/test/test_posix.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py index 521c9a1809..f8e1e7bc20 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -1513,7 +1513,6 @@ def test_unlink_dir_fd(self): self.addCleanup(posix.unlink, fullname) raise - @unittest.skip("TODO: RUSTPYTHON; no 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):