Skip to content

Commit 08ff6fa

Browse files
[3.11] gh-113090: Fix test.support.os_support.can_chmod() on Windows (GH-113091) (GH-113100)
(cherry picked from commit c6e953b) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent e0c6995 commit 08ff6fa

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

Lib/test/support/os_helper.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,15 @@ def can_chmod():
243243
global _can_chmod
244244
if _can_chmod is not None:
245245
return _can_chmod
246-
if not hasattr(os, "chown"):
246+
if not hasattr(os, "chmod"):
247247
_can_chmod = False
248248
return _can_chmod
249249
try:
250250
with open(TESTFN, "wb") as f:
251251
try:
252-
os.chmod(TESTFN, 0o777)
252+
os.chmod(TESTFN, 0o555)
253253
mode1 = os.stat(TESTFN).st_mode
254-
os.chmod(TESTFN, 0o666)
254+
os.chmod(TESTFN, 0o777)
255255
mode2 = os.stat(TESTFN).st_mode
256256
except OSError as e:
257257
can = False
@@ -298,6 +298,10 @@ def can_dac_override():
298298
else:
299299
_can_dac_override = True
300300
finally:
301+
try:
302+
os.chmod(TESTFN, 0o700)
303+
except OSError:
304+
pass
301305
unlink(TESTFN)
302306

303307
return _can_dac_override

Lib/test/test_os.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,7 @@ def tearDown(self):
16811681
os.removedirs(path)
16821682

16831683

1684-
@os_helper.skip_unless_working_chmod
1684+
@unittest.skipUnless(hasattr(os, "chown"), "requires os.chown()")
16851685
class ChownFileTests(unittest.TestCase):
16861686

16871687
@classmethod

Lib/test/test_posix.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ def check_stat(uid, gid):
788788
self.assertRaises(TypeError, chown_func, first_param, uid, t(gid))
789789
check_stat(uid, gid)
790790

791-
@os_helper.skip_unless_working_chmod
791+
@unittest.skipUnless(hasattr(os, "chown"), "requires os.chown()")
792792
@unittest.skipIf(support.is_emscripten, "getgid() is a stub")
793793
def test_chown(self):
794794
# raise an OSError if the file does not exist
@@ -956,6 +956,7 @@ def check_chmod(self, chmod_func, target, **kwargs):
956956
finally:
957957
posix.chmod(target, mode)
958958

959+
@os_helper.skip_unless_working_chmod
959960
def test_chmod_file(self):
960961
self.check_chmod(posix.chmod, os_helper.TESTFN)
961962

@@ -965,6 +966,7 @@ def tempdir(self):
965966
self.addCleanup(posix.rmdir, target)
966967
return target
967968

969+
@os_helper.skip_unless_working_chmod
968970
def test_chmod_dir(self):
969971
target = self.tempdir()
970972
self.check_chmod(posix.chmod, target)

Lib/test/test_tarfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3354,7 +3354,7 @@ def expect_file(self, name, type=None, symlink_to=None, mode=None,
33543354
path = pathlib.Path(os.path.normpath(self.destdir / name))
33553355
self.assertIn(path, self.expected_paths)
33563356
self.expected_paths.remove(path)
3357-
if mode is not None and os_helper.can_chmod():
3357+
if mode is not None and os_helper.can_chmod() and os.name != 'nt':
33583358
got = stat.filemode(stat.S_IMODE(path.stat().st_mode))
33593359
self.assertEqual(got, mode)
33603360
if type is None and isinstance(name, str) and name.endswith('/'):

0 commit comments

Comments
 (0)