Skip to content

Commit b88851f

Browse files
committed
Adapt handling of pathlib.Path.lchmod
- it calls chmod() in Python 3.10, so this has to handled
1 parent affc4f3 commit b88851f

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

pyfakefs/fake_pathlib.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,24 @@ class _FakeAccessor(accessor):
9494

9595
listdir = _wrap_strfunc(FakeFilesystem.listdir)
9696

97-
chmod = _wrap_strfunc(FakeFilesystem.chmod)
98-
9997
if use_scandir:
10098
scandir = _wrap_strfunc(fake_scandir.scandir)
10199

102100
if hasattr(os, "lchmod"):
103101
lchmod = _wrap_strfunc(lambda fs, path, mode: FakeFilesystem.chmod(
104102
fs, path, mode, follow_symlinks=False))
103+
chmod = _wrap_strfunc(FakeFilesystem.chmod)
105104
else:
106-
def lchmod(self, pathobj, mode):
105+
def lchmod(self, pathobj, *args, **kwargs):
107106
"""Raises not implemented for Windows systems."""
108107
raise NotImplementedError("lchmod() not available on this system")
109108

109+
def chmod(self, pathobj, *args, **kwargs):
110+
if "follow_symlinks" in kwargs and not kwargs["follow_symlinks"]:
111+
raise NotImplementedError(
112+
"lchmod() not available on this system")
113+
return pathobj.filesystem.chmod(str(pathobj), *args, **kwargs)
114+
110115
mkdir = _wrap_strfunc(FakeFilesystem.makedir)
111116

112117
unlink = _wrap_strfunc(FakeFilesystem.remove)
@@ -135,7 +140,8 @@ def lchmod(self, pathobj, mode):
135140
FakeFilesystem.link(fs, file_path, link_target))
136141

137142
# this will use the fake filesystem because os is patched
138-
getcwd = lambda p: os.getcwd()
143+
def getcwd(self):
144+
return os.getcwd()
139145

140146
readlink = _wrap_strfunc(FakeFilesystem.readlink)
141147

pyfakefs/tests/fake_pathlib_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,12 @@ def test_chmod(self):
380380

381381
def test_lchmod(self):
382382
self.skip_if_symlink_not_supported()
383+
if (sys.version_info >= (3, 10) and self.use_real_fs() and
384+
'chmod' not in os.supports_follow_symlinks):
385+
raise unittest.SkipTest('follow_symlinks not available for chmod')
383386
file_stat = self.os.stat(self.file_path)
384387
link_stat = self.os.lstat(self.file_link_path)
385-
if not hasattr(os, "lchmod") and sys.version_info < (3, 10):
388+
if not hasattr(os, "lchmod"):
386389
with self.assertRaises(NotImplementedError):
387390
self.path(self.file_link_path).lchmod(0o444)
388391
else:

0 commit comments

Comments
 (0)