bpo-39906: Add follow_symlinks parameter to pathlib.Path.stat() and chmod() (GH-18864)
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 9db8ae2..eaaf980 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -393,8 +393,6 @@ class _NormalAccessor(_Accessor):
stat = os.stat
- lstat = os.lstat
-
open = os.open
listdir = os.listdir
@@ -403,12 +401,6 @@ class _NormalAccessor(_Accessor):
chmod = os.chmod
- if hasattr(os, "lchmod"):
- lchmod = os.lchmod
- else:
- def lchmod(self, path, mode):
- raise NotImplementedError("os.lchmod() not available on this system")
-
mkdir = os.mkdir
unlink = os.unlink
@@ -1191,12 +1183,12 @@ def resolve(self, strict=False):
normed = self._flavour.pathmod.normpath(s)
return self._from_parts((normed,))
- def stat(self):
+ def stat(self, *, follow_symlinks=True):
"""
Return the result of the stat() system call on this path, like
os.stat() does.
"""
- return self._accessor.stat(self)
+ return self._accessor.stat(self, follow_symlinks=follow_symlinks)
def owner(self):
"""
@@ -1286,18 +1278,18 @@ def mkdir(self, mode=0o777, parents=False, exist_ok=False):
if not exist_ok or not self.is_dir():
raise
- def chmod(self, mode):
+ def chmod(self, mode, *, follow_symlinks=True):
"""
Change the permissions of the path, like os.chmod().
"""
- self._accessor.chmod(self, mode)
+ self._accessor.chmod(self, mode, follow_symlinks=follow_symlinks)
def lchmod(self, mode):
"""
Like chmod(), except if the path points to a symlink, the symlink's
permissions are changed, rather than its target's.
"""
- self._accessor.lchmod(self, mode)
+ self.chmod(mode, follow_symlinks=False)
def unlink(self, missing_ok=False):
"""
@@ -1321,7 +1313,7 @@ def lstat(self):
Like stat(), except if the path points to a symlink, the symlink's
status information is returned, rather than its target's.
"""
- return self._accessor.lstat(self)
+ return self.stat(follow_symlinks=False)
def link_to(self, target):
"""
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 9be7294..2643119 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -1828,6 +1828,21 @@ def test_chmod(self):
p.chmod(new_mode)
self.assertEqual(p.stat().st_mode, new_mode)
+ # On Windows, os.chmod does not follow symlinks (issue #15411)
+ @only_posix
+ def test_chmod_follow_symlinks_true(self):
+ p = self.cls(BASE) / 'linkA'
+ q = p.resolve()
+ mode = q.stat().st_mode
+ # Clear writable bit.
+ new_mode = mode & ~0o222
+ p.chmod(new_mode, follow_symlinks=True)
+ self.assertEqual(q.stat().st_mode, new_mode)
+ # Set writable bit
+ new_mode = mode | 0o222
+ p.chmod(new_mode, follow_symlinks=True)
+ self.assertEqual(q.stat().st_mode, new_mode)
+
# XXX also need a test for lchmod.
def test_stat(self):
@@ -1840,6 +1855,17 @@ def test_stat(self):
self.assertNotEqual(p.stat(), st)
@os_helper.skip_unless_symlink
+ def test_stat_no_follow_symlinks(self):
+ p = self.cls(BASE) / 'linkA'
+ st = p.stat()
+ self.assertNotEqual(st, p.stat(follow_symlinks=False))
+
+ def test_stat_no_follow_symlinks_nosymlink(self):
+ p = self.cls(BASE) / 'fileA'
+ st = p.stat()
+ self.assertEqual(st, p.stat(follow_symlinks=False))
+
+ @os_helper.skip_unless_symlink
def test_lstat(self):
p = self.cls(BASE)/ 'linkA'
st = p.stat()