diff options
author | mrbean-bremen <hansemrbean@googlemail.com> | 2021-11-05 21:55:06 +0100 |
---|---|---|
committer | mrbean-bremen <mrbean-bremen@users.noreply.github.com> | 2021-11-07 15:44:17 +0100 |
commit | 90dbff3201d10404180c644e3e752d6cd56abd2c (patch) | |
tree | 6f9f9f9b322142c3140c5ae0ed66ce729dda946b | |
parent | 81ac8d7077f27eb03e1010586f405c24485f9a97 (diff) | |
download | pyfakefs-90dbff3201d10404180c644e3e752d6cd56abd2c.tar.gz |
Add basic patching of fcntl module
- all fcntl functions are implemented empty
-rw-r--r-- | CHANGES.md | 3 | ||||
-rw-r--r-- | pyfakefs/fake_filesystem.py | 43 | ||||
-rw-r--r-- | pyfakefs/fake_filesystem_unittest.py | 6 |
3 files changed, 51 insertions, 1 deletions
@@ -6,6 +6,9 @@ The released versions correspond to PyPi releases. ### Changes * `os.listdir`, `os.scandir` and `pathlib.Path.listdir` now return the directory list in a random order (see [#638](../../issues/638)) +* the `fcntl` module under Unix is now mocked, e.g. all functions have no + effect (this may be changed in the future if needed, + see [#645](../../issues/645)) ### Fixes * fixed handling of alternative path separator in `os.path.split`, diff --git a/pyfakefs/fake_filesystem.py b/pyfakefs/fake_filesystem.py index 98086e3..bdbaa80 100644 --- a/pyfakefs/fake_filesystem.py +++ b/pyfakefs/fake_filesystem.py @@ -4864,6 +4864,49 @@ class FakeIoModule: return getattr(self._io_module, name) +if sys.platform != 'win32': + import fcntl + + class FakeFcntlModule: + """Replaces the fcntl module. Only valid under Linux/MacOS, + currently just mocks the functionality away. + """ + + @staticmethod + def dir() -> List[str]: + """Return the list of patched function names. Used for patching + functions imported from the module. + """ + return ['fcntl', 'ioctl', 'flock', 'lockf'] + + def __init__(self, filesystem: FakeFilesystem): + """ + Args: + filesystem: FakeFilesystem used to provide file system + information (currently not used). + """ + self.filesystem = filesystem + self._fcntl_module = fcntl + + def fcntl(self, fd: int, cmd: int, arg: int = 0) -> Union[int, bytes]: + return 0 + + def ioctl(self, fd: int, request: int, arg: int = 0, + mutate_flag: bool = True) -> Union[int, bytes]: + return 0 + + def flock(self, fd: int, operation: int) -> None: + pass + + def lockf(self, fd: int, cmd: int, len: int = 0, + start: int = 0, whence=0) -> Any: + pass + + def __getattr__(self, name): + """Forwards any unfaked calls to the standard fcntl module.""" + return getattr(self._fcntl_module, name) + + class FakeFileWrapper: """Wrapper for a stream object for use by a FakeFile object. diff --git a/pyfakefs/fake_filesystem_unittest.py b/pyfakefs/fake_filesystem_unittest.py index ce5e003..c2ec11e 100644 --- a/pyfakefs/fake_filesystem_unittest.py +++ b/pyfakefs/fake_filesystem_unittest.py @@ -391,7 +391,8 @@ class Patcher: IS_WINDOWS = sys.platform in ('win32', 'cygwin') - SKIPNAMES = {'os', 'path', 'io', 'genericpath', OS_MODULE, PATH_MODULE} + SKIPNAMES = {'os', 'path', 'io', 'genericpath', 'fcntl', + OS_MODULE, PATH_MODULE} # hold values from last call - if changed, the cache has to be invalidated PATCHED_MODULE_NAMES: Set[str] = set() @@ -537,6 +538,9 @@ class Patcher: if IS_PYPY: # in PyPy io.open, the module is referenced as _io self._fake_module_classes['_io'] = fake_filesystem.FakeIoModule + if sys.platform != 'win32': + self._fake_module_classes[ + 'fcntl'] = fake_filesystem.FakeFcntlModule # class modules maps class names against a list of modules they can # be contained in - this allows for alternative modules like |