Skip to content

[3.11] gh-113090: Fix test.support.os_support.can_chmod() on Windows (GH-113091) #113100

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 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions Lib/test/support/os_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,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
Expand Down Expand Up @@ -298,6 +298,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
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -1681,7 +1681,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
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,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
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3354,7 +3354,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('/'):
Expand Down