8000 gh-113188: Fix shutil.copymode() and shutil.copystat() on Windows · python/cpython@d7dd649 · GitHub
[go: up one dir, main page]

Skip to content

Commit d7dd649

Browse files
gh-113188: Fix shutil.copymode() and shutil.copystat() on Windows
Previously it worked differenly if dst is a symbolic link: it modified the permission bits of dst itself rather than the file it points to if follow_symlinks is true or src is not a symbolic link, and did nothing if follow_symlinks is false and src is a symbolic link.
1 parent ca5a523 commit d7dd649

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

Lib/shutil.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,15 @@ def copymode(src, dst, *, follow_symlinks=True):
302302
sys.audit("shutil.copymode", src, dst)
303303

304304
if not follow_symlinks and _islink(src) and os.path.islink(dst):
305-
if hasattr(os, 'lchmod'):
305+
if os.name == 'nt':
306+
stat_func, chmod_func = os.lstat, os.chmod
307+
elif hasattr(os, 'lchmod'):
306308
stat_func, chmod_func = os.lstat, os.lchmod
307309
else:
308310
return
309311
else:
312+
if os.name == 'nt' and os.path.islink(dst):
313+
dst = os.realpath(dst, strict=True)
310314
stat_func, chmod_func = _stat, os.chmod
311315

312316
st = stat_func(src)
@@ -382,8 +386,16 @@ def lookup(name):
8000 382386
# We must copy extended attributes before the file is (potentially)
383387
# chmod()'ed read-only, otherwise setxattr() will error with -EACCES.
384388
_copyxattr(src, dst, follow_symlinks=follow)
389+
_chmod = lookup("chmod")
390+
if os.name == 'nt':
391+
if follow:
392+
if os.path.islink(dst):
393+
dst = os.path.realpath(dst, strict=True)
394+
else:
395+
def _chmod(*args, **kwargs):
396+
os.chmod(*args)
385397
try:
386-
lookup("chmod")(dst, mode, follow_symlinks=follow)
398+
_chmod(dst, mode, follow_symlinks=follow)
387399
except NotImplementedError:
388400
# if we got a NotImplementedError, it's because
389401
# * follow_symlinks=False,

Lib/test/test_shutil.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,19 +1046,18 @@ def test_copymode_follow_symlinks(self):
10461046
shutil.copymode(src, dst)
10471047
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
10481048
# On Windows, os.chmod does not follow symlinks (issue #15411)
1049-
if os.name != 'nt':
1050-
# follow src link
1051-
os.chmod(dst, stat.S_IRWXO)
1052-
shutil.copymode(src_link, dst)
1053-
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
1054-
# follow dst link
1055-
os.chmod(dst, stat.S_IRWXO)
1056-
shutil.copymode(src, dst_link)
1057-
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
1058-
# follow both links
1059-
os.chmod(dst, stat.S_IRWXO)
1060-
shutil.copymode(src_link, dst_link)
1061-
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
1049+
# follow src link
1050+
os.chmod(dst, stat.S_IRWXO)
1051+
shutil.copymode(src_link, dst)
1052+
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
1053+
# follow dst link
1054+
os.chmod(dst, stat.S_IRWXO)
1055+
shutil.copymode(src, dst_link)
1056+
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
1057+
# follow both links
1058+
os.chmod(dst, stat.S_IRWXO)
1059+
shutil.copymode(src_link, dst_link)
1060+
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
10621061

10631062
@unittest.skipUnless(hasattr(os, 'lchmod'), 'requires os.lchmod')
10641063
@os_helper.skip_unless_symlink
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix :func:`shutil.copymode` on Windows. Previously it worked differenly if
2+
*dst* is a symbolic link: it modified the permission bits of *dst* itself
3+
rather than the file it points to if *follow_symlinks* is true or *src* is
4+
not a symbolic link, and did not modify the permission bits if
5+
*follow_symlinks* is false and *src* is a symbolic link.

0 commit comments

Comments
 (0)
0