Skip to content

Commit 90dbff3

Browse files
committed
Add basic patching of fcntl module
- all fcntl functions are implemented empty
1 parent 81ac8d7 commit 90dbff3

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The released versions correspond to PyPi releases.
66
### Changes
77
* `os.listdir`, `os.scandir` and `pathlib.Path.listdir` now return the
88
directory list in a random order (see [#638](../../issues/638))
9+
* the `fcntl` module under Unix is now mocked, e.g. all functions have no
10+
effect (this may be changed in the future if needed,
11+
see [#645](../../issues/645))
912

1013
### Fixes
1114
* fixed handling of alternative path separator in `os.path.split`,

pyfakefs/fake_filesystem.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4864,6 +4864,49 @@ def __getattr__(self, name):
48644864
return getattr(self._io_module, name)
48654865

48664866

4867+
if sys.platform != 'win32':
4868+
import fcntl
4869+
4870+
class FakeFcntlModule:
4871+
"""Replaces the fcntl module. Only valid under Linux/MacOS,
4872+
currently just mocks the functionality away.
4873+
"""
4874+
4875+
@staticmethod
4876+
def dir() -> List[str]:
4877+
"""Return the list of patched function names. Used for patching
4878+
functions imported from the module.
4879+
"""
4880+
return ['fcntl', 'ioctl', 'flock', 'lockf']
4881+
4882+
def __init__(self, filesystem: FakeFilesystem):
4883+
"""
4884+
Args:
4885+
filesystem: FakeFilesystem used to provide file system
4886+
information (currently not used).
4887+
"""
4888+
self.filesystem = filesystem
4889+
self._fcntl_module = fcntl
4890+
4891+
def fcntl(self, fd: int, cmd: int, arg: int = 0) -> Union[int, bytes]:
4892+
return 0
4893+
4894+
def ioctl(self, fd: int, request: int, arg: int = 0,
4895+
mutate_flag: bool = True) -> Union[int, bytes]:
4896+
return 0
4897+
4898+
def flock(self, fd: int, operation: int) -> None:
4899+
pass
4900+
4901+
def lockf(self, fd: int, cmd: int, len: int = 0,
4902+
start: int = 0, whence=0) -> Any:
4903+
pass
4904+
4905+
def __getattr__(self, name):
4906+
"""Forwards any unfaked calls to the standard fcntl module."""
4907+
return getattr(self._fcntl_module, name)
4908+
4909+
48674910
class FakeFileWrapper:
48684911
"""Wrapper for a stream object for use by a FakeFile object.
48694912

pyfakefs/fake_filesystem_unittest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,8 @@ class Patcher:
391391

392392
IS_WINDOWS = sys.platform in ('win32', 'cygwin')
393393

394-
SKIPNAMES = {'os', 'path', 'io', 'genericpath', OS_MODULE, PATH_MODULE}
394+
SKIPNAMES = {'os', 'path', 'io', 'genericpath', 'fcntl',
395+
OS_MODULE, PATH_MODULE}
395396

396397
# hold values from last call - if changed, the cache has to be invalidated
397398
PATCHED_MODULE_NAMES: Set[str] = set()
@@ -537,6 +538,9 @@ def _init_fake_module_classes(self) -> None:
537538
if IS_PYPY:
538539
# in PyPy io.open, the module is referenced as _io
539540
self._fake_module_classes['_io'] = fake_filesystem.FakeIoModule
541+
if sys.platform != 'win32':
542+
self._fake_module_classes[
543+
'fcntl'] = fake_filesystem.FakeFcntlModule
540544

541545
# class modules maps class names against a list of modules they can
542546
# be contained in - this allows for alternative modules like

0 commit comments

Comments
 (0)