Skip to content

Commit fa4f228

Browse files
committed
Fix incorrect handling of dirfd in os.symlink
- also removed check for supported dirfd where not used - changed dirfd tests to run both in the fake and real OS - fixed #968
1 parent ebbaa73 commit fa4f228

File tree

3 files changed

+221
-188
lines changed

3 files changed

+221
-188
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The released versions correspond to PyPI releases.
2020
(see [#960](../../issues/960))
2121
* fixed creation of the temp directory in the fake file system after a filesystem reset
2222
(see [#965](../../issues/965))
23+
* fixed handling of `dirfd` in `os.symlink` (see [#968](../../issues/968))
2324

2425
## [Version 5.3.5](https://pypi.python.org/pypi/pyfakefs/5.3.5) (2024-01-30)
2526
Fixes a regression.

pyfakefs/fake_os.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ def lstat(self, path: AnyStr, *, dir_fd: Optional[int] = None) -> FakeStatResult
671671
OSError: if the filesystem object doesn't exist.
672672
"""
673673
# stat should return the tuple representing return value of os.stat
674-
path = self._path_with_dir_fd(path, self.lstat, dir_fd)
674+
path = self._path_with_dir_fd(path, self.lstat, dir_fd, check_supported=False)
675675
return self.filesystem.stat(path, follow_symlinks=False)
676676

677677
def remove(self, path: AnyStr, dir_fd: Optional[int] = None) -> None:
@@ -687,7 +687,7 @@ def remove(self, path: AnyStr, dir_fd: Optional[int] = None) -> None:
687687
OSError: if path does not exist.
688688
OSError: if removal failed.
689689
"""
690-
path = self._path_with_dir_fd(path, self.remove, dir_fd)
690+
path = self._path_with_dir_fd(path, self.remove, dir_fd, check_supported=False)
691691
self.filesystem.remove(path)
692692

693693
def unlink(self, path: AnyStr, *, dir_fd: Optional[int] = None) -> None:
@@ -798,8 +798,12 @@ def replace(
798798
OSError: if the file would be moved to another filesystem
799799
(e.g. mount point)
800800
"""
801-
src = self._path_with_dir_fd(src, self.rename, src_dir_fd)
802-
dst = self._path_with_dir_fd(dst, self.rename, dst_dir_fd)
801+
src = self._path_with_dir_fd(
802+
src, self.rename, src_dir_fd, check_supported=False
803+
)
804+
dst = self._path_with_dir_fd(
805+
dst, self.rename, dst_dir_fd, check_supported=False
806+
)
803807
self.filesystem.rename(src, dst, force_replace=True)
804808

805809
def rmdir(self, path: AnyStr, *, dir_fd: Optional[int] = None) -> None:
@@ -891,7 +895,11 @@ def makedirs(
891895
self.filesystem.makedirs(name, mode, exist_ok)
892896

893897
def _path_with_dir_fd(
894-
self, path: AnyStr, fct: Callable, dir_fd: Optional[int]
898+
self,
899+
path: AnyStr,
900+
fct: Callable,
901+
dir_fd: Optional[int],
902+
check_supported: bool = True,
895903
) -> AnyStr:
896904
"""Return the path considering dir_fd. Raise on invalid parameters."""
897905
try:
@@ -901,7 +909,7 @@ def _path_with_dir_fd(
901909
path = path
902910
if dir_fd is not None:
903911
# check if fd is supported for the built-in real function
904-
if fct not in self.supports_dir_fd:
912+
if check_supported and (fct not in self.supports_dir_fd):
905913
raise NotImplementedError("dir_fd unavailable on this platform")
906914
if isinstance(path, int):
907915
raise ValueError(
@@ -1162,12 +1170,12 @@ def symlink(
11621170
dst: Path to the symlink to create.
11631171
target_is_directory: Currently ignored.
11641172
dir_fd: If not `None`, the file descriptor of a directory,
1165-
with `src` being relative to this directory.
1173+
with `dst` being relative to this directory.
11661174
11671175
Raises:
11681176
OSError: if the file already exists.
11691177
"""
1170-
src = self._path_with_dir_fd(src, self.symlink, dir_fd)
1178+
dst = self._path_with_dir_fd(dst, self.symlink, dir_fd)
11711179
self.filesystem.create_symlink(dst, src, create_missing_dirs=False)
11721180

11731181
def link(
@@ -1178,7 +1186,7 @@ def link(
11781186
src_dir_fd: Optional[int] = None,
11791187
dst_dir_fd: Optional[int] = None,
11801188
) -> None:
1181-
"""Create a hard link at new_path, pointing at old_path.
1189+
"""Create a hard link at dst, pointing at src.
11821190
11831191
Args:
11841192
src: An existing path to the target file.

0 commit comments

Comments
 (0)