gh-113191: Add support of os.fchmod() on Windows (GH-113192)

Also support a file descriptor in os.chmod().
This commit is contained in:
Serhiy Storchaka 2023-12-24 12:57:11 +02:00 committed by GitHub
parent 53330f1677
commit 1f06baeabd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 82 additions and 32 deletions

View file

@ -936,6 +936,7 @@ class PosixTester(unittest.TestCase):
posix.utime(os_helper.TESTFN, (now, now))
def check_chmod(self, chmod_func, target, **kwargs):
closefd = not isinstance(target, int)
mode = os.stat(target).st_mode
try:
new_mode = mode & ~(stat.S_IWOTH | stat.S_IWGRP | stat.S_IWUSR)
@ -943,7 +944,7 @@ class PosixTester(unittest.TestCase):
self.assertEqual(os.stat(target).st_mode, new_mode)
if stat.S_ISREG(mode):
try:
with open(target, 'wb+'):
with open(target, 'wb+', closefd=closefd):
pass
except PermissionError:
pass
@ -951,10 +952,10 @@ class PosixTester(unittest.TestCase):
chmod_func(target, new_mode, **kwargs)
self.assertEqual(os.stat(target).st_mode, new_mode)
if stat.S_ISREG(mode):
with open(target, 'wb+'):
with open(target, 'wb+', closefd=closefd):
pass
finally:
posix.chmod(target, mode)
chmod_func(target, mode)
@os_helper.skip_unless_working_chmod
def test_chmod_file(self):
@ -971,6 +972,12 @@ class PosixTester(unittest.TestCase):
target = self.tempdir()
self.check_chmod(posix.chmod, target)
@os_helper.skip_unless_working_chmod
def test_fchmod_file(self):
with open(os_helper.TESTFN, 'wb+') as f:
self.check_chmod(posix.fchmod, f.fileno())
self.check_chmod(posix.chmod, f.fileno())
@unittest.skipUnless(hasattr(posix, 'lchmod'), 'test needs os.lchmod()')
def test_lchmod_file(self):
self.check_chmod(posix.lchmod, os_helper.TESTFN)