From 23fdd0ae618aa9957df215659c44d819a8059a03 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 14 Dec 2023 13:27:43 +0200 Subject: [PATCH] gh-113090: Fix test.support.os_support.can_chmod() on Windows (GH-113091) (cherry picked from commit c6e953be125c491226adc1a5bc9c804ce256aa17) Co-authored-by: Serhiy Storchaka --- Lib/test/support/os_helper.py | 10 +++++++--- Lib/test/test_os.py | 2 +- Lib/test/test_posix.py | 4 +++- Lib/test/test_tarfile.py | 2 +- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Lib/test/support/os_helper.py b/Lib/test/support/os_helper.py index 7a67d87fb9e846..20f38fd36a8876 100644 --- a/Lib/test/support/os_helper.py +++ b/Lib/test/support/os_helper.py @@ -247,15 +247,15 @@ def can_chmod(): global _can_chmod if _can_chmod is not None: return _can_chmod - if not hasattr(os, "chown"): + if not hasattr(os, "chmod"): _can_chmod = False return _can_chmod try: with open(TESTFN, "wb") as f: try: - os.chmod(TESTFN, 0o777) + os.chmod(TESTFN, 0o555) mode1 = os.stat(TESTFN).st_mode - os.chmod(TESTFN, 0o666) + os.chmod(TESTFN, 0o777) mode2 = os.stat(TESTFN).st_mode except OSError as e: can = False @@ -302,6 +302,10 @@ def can_dac_override(): else: _can_dac_override = True finally: + try: + os.chmod(TESTFN, 0o700) + except OSError: + pass unlink(TESTFN) return _can_dac_override diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 1f0836b2476137..2d94951bf27f08 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -1735,7 +1735,7 @@ def tearDown(self): os.removedirs(path) -@os_helper.skip_unless_working_chmod +@unittest.skipUnless(hasattr(os, "chown"), "requires os.chown()") class ChownFileTests(unittest.TestCase): @classmethod diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py index 4eb3cacc23768f..887420f8caccfb 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -791,7 +791,7 @@ def check_stat(uid, gid): self.assertRaises(TypeError, chown_func, first_param, uid, t(gid)) check_stat(uid, gid) - @os_helper.skip_unless_working_chmod + @unittest.skipUnless(hasattr(os, "chown"), "requires os.chown()") @unittest.skipIf(support.is_emscripten, "getgid() is a stub") def test_chown(self): # raise an OSError if the file does not exist @@ -956,6 +956,7 @@ def check_chmod(self, chmod_func, target, **kwargs): finally: posix.chmod(target, mode) + @os_helper.skip_unless_working_chmod def test_chmod_file(self): self.check_chmod(posix.chmod, os_helper.TESTFN) @@ -965,6 +966,7 @@ def tempdir(self): self.addCleanup(posix.rmdir, target) return target + @os_helper.skip_unless_working_chmod def test_chmod_dir(self): target = self.tempdir() self.check_chmod(posix.chmod, target) diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index c5fc76dc0207d4..208ceaf6674c95 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -3429,7 +3429,7 @@ def expect_file(self, name, type=None, symlink_to=None, mode=None, path = pathlib.Path(os.path.normpath(self.destdir / name)) self.assertIn(path, self.expected_paths) self.expected_paths.remove(path) - if mode is not None and os_helper.can_chmod(): + if mode is not None and os_helper.can_chmod() and os.name != 'nt': got = stat.filemode(stat.S_IMODE(path.stat().st_mode)) self.assertEqual(got, mode) if type is None and isinstance(name, str) and name.endswith('/'):