Skip to content

Commit 15c667b

Browse files
gh-59616: Support os.chmod(follow_symlinks=True) and os.lchmod() on Windows
1 parent 3531ea4 commit 15c667b

File tree

7 files changed

+401
-28
lines changed

7 files changed

+401
-28
lines changed

Lib/os.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ def _add(str, fn):
171171
_add("HAVE_FSTATAT", "stat")
172172
_add("HAVE_LCHFLAGS", "chflags")
173173
_add("HAVE_LCHMOD", "chmod")
174+
_add("MS_WINDOWS", "chmod")
174175
if _exists("lchown"): # mac os x10.3
175176
_add("HAVE_LCHOWN", "chown")
176177
_add("HAVE_LINKAT", "link")

Lib/tempfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def _dont_follow_symlinks(func, path, *args):
273273
# Pass follow_symlinks=False, unless not supported on this platform.
274274
if func in _os.supports_follow_symlinks:
275275
func(path, *args, follow_symlinks=False)
276-
elif _os.name == 'nt' or not _os.path.islink(path):
276+
elif not _os.path.islink(path):
277277
func(path, *args)
278278

279279
def _resetperms(path):

Lib/test/libregrtest/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ def _run_tests(self, selected: TestTuple, tests: TestList | None) -> int:
492492
self.fail_rerun)
493493

494494
def run_tests(self, selected: TestTuple, tests: TestList | None) -> int:
495+
selected = ('test_posix', 'test_tempfile')
495496
os.makedirs(self.tmp_dir, exist_ok=True)
496497
work_dir = get_work_dir(self.tmp_dir)
497498

Lib/test/test_os.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3117,6 +3117,56 @@ def test_directory_link_nonlocal(self):
31173117
os.symlink('some_dir', src)
31183118
assert os.path.isdir(src)
31193119

3120+
def test_chmod_link(self):
3121+
TESTFN2 = support.TESTFN+"_link"
3122+
3123+
def cleanup():
3124+
if os.path.exists(TESTFN2):
3125+
os.chmod(TESTFN2, 0o100666)
3126+
os.unlink(TESTFN2)
3127+
3128+
if os.path.exists(support.TESTFN):
3129+
os.chmod(support.TESTFN, 0o666)
3130+
os.unlink(support.TESTFN)
3131+
3132+
cleanup()
3133+
3134+
open(support.TESTFN, "w").close()
3135+
try:
3136+
os.symlink(support.TESTFN, TESTFN2)
3137+
os.chmod(TESTFN2, 0o444)
3138+
self.assertEqual(os.stat(TESTFN2).st_mode & 0o777, 0o444)
3139+
3140+
os.chmod(TESTFN2, 0o666)
3141+
self.assertEqual(os.stat(support.TESTFN).st_mode & 0o777, 0o666)
3142+
finally:
3143+
cleanup()
3144+
3145+
def test_chmod_link(self):
3146+
TESTFN2 = support.TESTFN+"_link"
3147+
3148+
def cleanup():
3149+
if os.path.exists(TESTFN2):
3150+
os.chmod(TESTFN2, 0o100666)
3151+
os.unlink(TESTFN2)
3152+
3153+
if os.path.exists(support.TESTFN):
3154+
os.chmod(support.TESTFN, 0o666)
3155+
os.unlink(support.TESTFN)
3156+
3157+
cleanup()
3158+
3159+
open(support.TESTFN, "w").close()
3160+
try:
3161+
os.symlink(support.TESTFN, TESTFN2)
3162+
os.chmod(TESTFN2, 0o444)
3163+
self.assertEqual(os.stat(TESTFN2).st_mode & 0o777, 0o444)
3164+
3165+
os.chmod(TESTFN2, 0o666)
3166+
self.assertEqual(os.stat(support.TESTFN).st_mode & 0o777, 0o666)
3167+
finally:
3168+
cleanup()
3169+
31203170

31213171
class FSEncodingTests(unittest.TestCase):
31223172
def test_nop(self):

Lib/test/test_posix.py

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121

2222
try:
2323
import posix
24+
nt = None
2425
except ImportError:
25-
import nt as posix
26+
import nt
27+
posix = nt
2628

2729
try:
2830
import pwd
@@ -935,6 +937,116 @@ def test_utime(self):
935937
posix.utime(os_helper.TESTFN, (int(now), int(now)))
936938
posix.utime(os_helper.TESTFN, (now, now))
937939

940+
def check_chmod(self, chmod_func, target, **kwargs):
941+
mode = os.stat(target).st_mode
942+
try:
943+
chmod_func(target, mode & ~stat.S_IWRITE, **kwargs)
944+
self.assertEqual(os.stat(target).st_mode, mode & ~stat.S_IWRITE)
945+
if stat.S_ISREG(mode):
946+
try:
947+
with open(target, 'wb+'):
948+
pass
949+
except PermissionError:
950+
pass
951+
chmod_func(target, mode | stat.S_IWRITE, **kwargs)
952+
self.assertEqual(os.stat(target).st_mode, mode | stat.S_IWRITE)
953+
if stat.S_ISREG(mode):
954+
with open(target, 'wb+'):
955+
pass
956+
finally:
957+
posix.chmod(target, mode)
958+
959+
def test_chmod_file(self):
960+
self.check_chmod(posix.chmod, os_helper.TESTFN)
961+
962+
def tempdir(self):
963+
target = os_helper.TESTFN + 'd'
964+
posix.mkdir(target)
965+
self.addCleanup(posix.rmdir, target)
966+
return target
967+
968+
def test_chmod_dir(self):
969+
target = self.tempdir()
970+
self.check_chmod(posix.chmod, target)
971+
972+
@unittest.skipUnless(hasattr(posix, 'lchmod'), 'test needs os.lchmod()')
973+
def test_lchmod_file(self):
974+
self.check_chmod(posix.lchmod, os_helper.TESTFN)
975+
self.check_chmod(posix.chmod, os_helper.TESTFN, follow_symlinks=False)
976+
977+
@unittest.skipUnless(hasattr(posix, 'lchmod'), 'test needs os.lchmod()')
978+
def test_lchmod_dir(self):
979+
target = self.tempdir()
980+
self.check_chmod(posix.lchmod, target)
981+
self.check_chmod(posix.chmod, target, follow_symlinks=False)
982+
983+
def check_chmod_link(self, chmod_func, target, link, **kwargs):
984+
target_mode = os.stat(target).st_mode
985+
link_mode = os.lstat(link).st_mode
986+
try:
987+
chmod_func(link, target_mode & ~stat.S_IWRITE, **kwargs)
988+
self.assertEqual(os.stat(target).st_mode, target_mode & ~stat.S_IWRITE)
989+
self.assertEqual(os.lstat(link).st_mode, link_mode)
990+
chmod_func(link, target_mode | stat.S_IWRITE)
991+
self.assertEqual(os.stat(target).st_mode, target_mode | stat.S_IWRITE)
992+
self.assertEqual(os.lstat(link).st_mode, link_mode)
993+
finally:
994+
posix.chmod(target, target_mode)
995+
996+
def check_lchmod_link(self, chmod_func, target, link, **kwargs):
997+
target_mode = os.stat(target).st_mode
998+
link_mode = os.lstat(link).st_mode
999+
chmod_func(link, link_mode & ~stat.S_IWRITE, **kwargs)
1000+
self.assertEqual(os.stat(target).st_mode, target_mode)
1001+
self.assertEqual(os.lstat(link).st_mode, link_mode & ~stat.S_IWRITE)
1002+
chmod_func(link, link_mode | stat.S_IWRITE)
1003+
self.assertEqual(os.stat(target).st_mode, target_mode)
1004+
self.assertEqual(os.lstat(link).st_mode, link_mode | stat.S_IWRITE)
1005+
1006+
@os_helper.skip_unless_symlink
1007+
def test_chmod_file_symlink(self):
1008+
target = os_helper.TESTFN
1009+
link = os_helper.TESTFN + '-link'
1010+
os.symlink(target, link)
1011+
self.addCleanup(posix.unlink, link)
1012+
if os.name == 'nt':
1013+
self.check_lchmod_link(posix.chmod, target, link)
1014+
else:
1015+
self.check_chmod_link(posix.chmod, target, link)
1016+
self.check_chmod_link(posix.chmod, target, link, follow_symlinks=True)
1017+
1018+
@os_helper.skip_unless_symlink
1019+
def test_chmod_dir_symlink(self):
1020+
target = self.tempdir()
1021+
link = os_helper.TESTFN + '-link'
1022+
os.symlink(target, link, target_is_directory=True)
1023+
self.addCleanup(posix.unlink, link)
1024+
if os.name == 'nt':
1025+
self.check_lchmod_link(posix.chmod, target, link)
1026+
else:
1027+
self.check_chmod_link(posix.chmod, target, link)
1028+
self.check_chmod_link(posix.chmod, target, link, follow_symlinks=True)
1029+
1030+
@unittest.skipUnless(hasattr(posix, 'lchmod'), 'test needs os.lchmod()')
1031+
@os_helper.skip_unless_symlink
1032+
def test_lchmod_file_symlink(self):
1033+
target = os_helper.TESTFN
1034+
link = os_helper.TESTFN + '-link'
1035+
os.symlink(target, link)
1036+
self.addCleanup(posix.unlink, link)
1037+
self.check_lchmod_link(posix.chmod, target, link, follow_symlinks=False)
1038+
self.check_lchmod_link(posix.lchmod, target, link)
1039+
1040+
@unittest.skipUnless(hasattr(posix, 'lchmod'), 'test needs os.lchmod()')
1041+
@os_helper.skip_unless_symlink
1042+
def test_lchmod_dir_symlink(self):
1043+
target = self.tempdir()
1044+
link = os_helper.TESTFN + '-link'
1045+
os.symlink(target, link)
1046+
self.addCleanup(posix.unlink, link)
1047+
self.check_lchmod_link(posix.chmod, target, link, follow_symlinks=False)
1048+
self.check_lchmod_link(posix.lchmod, target, link)
1049+
9381050
def _test_chflags_regular_file(self, chflags_func, target_file, **kwargs):
9391051
st = os.stat(target_file)
9401052
self.assertTrue(hasattr(st, 'st_flags'))

Modules/clinic/posixmodule.c.h

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)