Skip to content

Commit 913051d

Browse files
[3.12] gh-113090: Fix test.support.os_support.can_chmod() on Windows (GH-113091) (GH-113099)
(cherry picked from commit c6e953b) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 9f5209f commit 913051d

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
@@ -247,15 +247,15 @@ def can_chmod():
247247
global _can_chmod
248248
if _can_chmod is not None:
249249
return _can_chmod
250-
if not hasattr(os, "chown"):
250+
if not hasattr(os, "chmod"):
251251
_can_chmod = False
252252
return _can_chmod
253253
try:
254254
with open(TESTFN, "wb") as f:
255255
try:
256-
os.chmod(TESTFN, 0o777)
256+
os.chmod(TESTFN, 0o555)
257257
mode1 = os.stat(TESTFN).st_mode
258-
os.chmod(TESTFN, 0o666)
258+
os.chmod(TESTFN, 0o777)
259259
mode2 = os.stat(TESTFN).st_mode
260260
except OSError as e:
261261
can = False
@@ -302,6 +302,10 @@ def can_dac_override():
302302
else:
303303
_can_dac_override = True
304304
finally:
305+
try:
306+
os.chmod(TESTFN, 0o700)
307+
except OSError:
308+
pass
305309
unlink(TESTFN)
306310

307311
return _can_dac_override

Lib/test/test_os.py

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

17371737

1738-
@os_helper.skip_unless_working_chmod
1738+
@unittest.skipUnless(hasattr(os, "chown"), "requires os.chown()")
17391739
class ChownFileTests(unittest.TestCase):
17401740

17411741
@classmethod

Lib/test/test_posix.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ def check_stat(uid, gid):
791791
self.assertRaises(TypeError, chown_func, first_param, uid, t(gid))
792792
check_stat(uid, gid)
793793

794-
@os_helper.skip_unless_working_chmod
794+
@unittest.skipUnless(hasattr(os, "chown"), "requires os.chown()")
795795
@unittest.skipIf(support.is_emscripten, "getgid() is a stub")
796796
def test_chown(self):
797797
# 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
@@ -3429,7 +3429,7 @@ def expect_file(self, name, type=None, symlink_to=None, mode=None,
34293429
path = pathlib.Path(os.path.normpath(self.destdir / name))
34303430
self.assertIn(path, self.expected_paths)
34313431
self.expected_paths.remove(path)
3432-
if mode is not None and os_helper.can_chmod():
3432+
if mode is not None and os_helper.can_chmod() and os.name != 'nt':
34333433
got = stat.filemode(stat.S_IMODE(path.stat().st_mode))
34343434
self.assertEqual(got, mode)
34353435
if type is None and isinstance(name, str) and name.endswith('/'):

0 commit comments

Comments
 (0)