Skip to content

Commit

Permalink
Add some missing methods to FakePipeWrapper
Browse files Browse the repository at this point in the history
- add readable, writable and seekable
- see #650
  • Loading branch information
mrbean-bremen committed Dec 3, 2021
1 parent 38a5589 commit b70140a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ The released versions correspond to PyPi releases.

## Version 4.6.0 (as yet unreleased)

### Fixes
* added missing mocked functions for fake pipe (see [#650](../../issues/650))


## [Version 4.5.3](https://pypi.python.org/pypi/pyfakefs/4.5.3) (2021-11-08)
Reverts a change in the previous release that could cause a regression.

Expand Down
25 changes: 19 additions & 6 deletions pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,7 @@ def path(self) -> AnyStr:
dir_path = sep + dir_path
else:
dir_path = sep.join(names)
dir_path = self.filesystem.absnormpath(dir_path)
return dir_path
return self.filesystem.absnormpath(dir_path)

@Deprecator('property path')
def GetPath(self):
Expand Down Expand Up @@ -3933,10 +3932,10 @@ def write(self, fd: int, contents: bytes) -> int:

def pipe(self) -> Tuple[int, int]:
read_fd, write_fd = os.pipe()
read_wrapper = FakePipeWrapper(self.filesystem, read_fd)
read_wrapper = FakePipeWrapper(self.filesystem, read_fd, False)
file_des = self.filesystem._add_open_file(read_wrapper)
read_wrapper.filedes = file_des
write_wrapper = FakePipeWrapper(self.filesystem, write_fd)
write_wrapper = FakePipeWrapper(self.filesystem, write_fd, True)
file_des = self.filesystem._add_open_file(write_wrapper)
write_wrapper.filedes = file_des
return read_wrapper.filedes, write_wrapper.filedes
Expand Down Expand Up @@ -5429,9 +5428,10 @@ class FakePipeWrapper:
used in open files list.
"""

def __init__(self, filesystem: FakeFilesystem, fd: int):
def __init__(self, filesystem: FakeFilesystem, fd: int, can_write: bool):
self._filesystem = filesystem
self.fd = fd # the real file descriptor
self.can_write = can_write
self.file_object = None
self.filedes: Optional[int] = None

Expand Down Expand Up @@ -5475,6 +5475,18 @@ def close(self) -> None:
open_files.remove(self)
os.close(self.fd)

def readable(self) -> bool:
"""The pipe end can either be readable or writable."""
return not self.can_write

def writable(self) -> bool:
"""The pipe end can either be readable or writable."""
return self.can_write

def seekable(self) -> bool:
"""A pipe is not seekable."""
return False


Deprecator.add(FakeFileWrapper, FakeFileWrapper.get_object, 'GetObject')
Deprecator.add(FakeFileWrapper, FakeFileWrapper.size, 'Size')
Expand Down Expand Up @@ -5559,7 +5571,8 @@ def call(self, file_: Union[AnyStr, int],
assert wrappers is not None
existing_wrapper = wrappers[0]
assert isinstance(existing_wrapper, FakePipeWrapper)
wrapper = FakePipeWrapper(self.filesystem, existing_wrapper.fd)
wrapper = FakePipeWrapper(self.filesystem, existing_wrapper.fd,
existing_wrapper.can_write)
file_des = self.filesystem._add_open_file(wrapper)
wrapper.filedes = file_des
return wrapper
Expand Down

0 comments on commit b70140a

Please sign in to comment.