Skip to content

Commit

Permalink
Fix incorrect handling of dirfd in os.symlink
Browse files Browse the repository at this point in the history
- also removed check for supported dirfd where not used
- changed dirfd tests to run both in the fake and real OS
- fixed pytest-dev#968
  • Loading branch information
mrbean-bremen committed Mar 10, 2024
1 parent ebbaa73 commit 190390f
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 188 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The released versions correspond to PyPI releases.
(see [#960](../../issues/960))
* fixed creation of the temp directory in the fake file system after a filesystem reset
(see [#965](../../issues/965))
* fixed handling of `dirfd` in `os.symlink` (see [#968](../../issues/968))

## [Version 5.3.5](https://pypi.python.org/pypi/pyfakefs/5.3.5) (2024-01-30)
Fixes a regression.
Expand Down
26 changes: 17 additions & 9 deletions pyfakefs/fake_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ def lstat(self, path: AnyStr, *, dir_fd: Optional[int] = None) -> FakeStatResult
OSError: if the filesystem object doesn't exist.
"""
# stat should return the tuple representing return value of os.stat
path = self._path_with_dir_fd(path, self.lstat, dir_fd)
path = self._path_with_dir_fd(path, self.lstat, dir_fd, check_supported=False)
return self.filesystem.stat(path, follow_symlinks=False)

def remove(self, path: AnyStr, dir_fd: Optional[int] = None) -> None:
Expand All @@ -687,7 +687,7 @@ def remove(self, path: AnyStr, dir_fd: Optional[int] = None) -> None:
OSError: if path does not exist.
OSError: if removal failed.
"""
path = self._path_with_dir_fd(path, self.remove, dir_fd)
path = self._path_with_dir_fd(path, self.remove, dir_fd, check_supported=False)
self.filesystem.remove(path)

def unlink(self, path: AnyStr, *, dir_fd: Optional[int] = None) -> None:
Expand Down Expand Up @@ -798,8 +798,12 @@ def replace(
OSError: if the file would be moved to another filesystem
(e.g. mount point)
"""
src = self._path_with_dir_fd(src, self.rename, src_dir_fd)
dst = self._path_with_dir_fd(dst, self.rename, dst_dir_fd)
src = self._path_with_dir_fd(
src, self.rename, src_dir_fd, check_supported=False
)
dst = self._path_with_dir_fd(
dst, self.rename, dst_dir_fd, check_supported=False
)
self.filesystem.rename(src, dst, force_replace=True)

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

def _path_with_dir_fd(
self, path: AnyStr, fct: Callable, dir_fd: Optional[int]
self,
path: AnyStr,
fct: Callable,
dir_fd: Optional[int],
check_supported: bool = True,
) -> AnyStr:
"""Return the path considering dir_fd. Raise on invalid parameters."""
try:
Expand All @@ -901,7 +909,7 @@ def _path_with_dir_fd(
path = path
if dir_fd is not None:
# check if fd is supported for the built-in real function
if fct not in self.supports_dir_fd:
if check_supported and (fct not in self.supports_dir_fd):
raise NotImplementedError("dir_fd unavailable on this platform")
if isinstance(path, int):
raise ValueError(
Expand Down Expand Up @@ -1162,12 +1170,12 @@ def symlink(
dst: Path to the symlink to create.
target_is_directory: Currently ignored.
dir_fd: If not `None`, the file descriptor of a directory,
with `src` being relative to this directory.
with `dst` being relative to this directory.
Raises:
OSError: if the file already exists.
"""
src = self._path_with_dir_fd(src, self.symlink, dir_fd)
dst = self._path_with_dir_fd(dst, self.symlink, dir_fd)
self.filesystem.create_symlink(dst, src, create_missing_dirs=False)

def link(
Expand All @@ -1178,7 +1186,7 @@ def link(
src_dir_fd: Optional[int] = None,
dst_dir_fd: Optional[int] = None,
) -> None:
"""Create a hard link at new_path, pointing at old_path.
"""Create a hard link at dst, pointing at src.
Args:
src: An existing path to the target file.
Expand Down
Loading

0 comments on commit 190390f

Please sign in to comment.