pytest-dev / pyfakefs

Provides a fake file system that mocks the Python file system modules.
https://pytest-pyfakefs.readthedocs.io
Apache License 2.0
651 stars 91 forks source link

AttributeError: 'FakeFcntlModule' object has no attribute 'LOCK_SH' (Also 'LOCK_UN' and 'LOCK_EX') #1074

Closed sj98ta closed 1 month ago

sj98ta commented 1 month ago

Describe the bug After updating from pyfakefs 5.6.0 to 5.7.0 the following error occurs on flock with fcntl.LOCK_SH, fcntl.LOCK_UN and LOCK_EX.

The error does not occur on version 5.6.0

self = <pyfakefs.fake_io.FakeFcntlModule object at 0x7f237a9ba760>, name = 'LOCK_SH'

    def __getattribute__(self, name):
        """Forwards any unfaked calls to the standard fcntl module."""
        fs: FakeFilesystem = object.__getattribute__(self, "filesystem")
        fnctl_module = object.__getattribute__(self, "_fcntl_module")
        if fs.patcher:
            if is_called_from_skipped_module(
                skip_names=fs.patcher.skip_names,
                case_sensitive=fs.is_case_sensitive,
            ):
                # remove the `self` argument for FakeOsModule methods
                return getattr(fnctl_module, name)

>       return object.__getattribute__(self, name)
E       AttributeError: 'FakeFcntlModule' object has no attribute 'LOCK_SH'

How To Reproduce

import fcntl
from os import path

def lock():
    with open('lock_file', 'a+') as lock_file:
        fcntl.flock(lock_file, fcntl.LOCK_SH)
        fcntl.flock(lock_file, fcntl.LOCK_UN)

def test_lock(fs):
    lock()

Your environment Please run the following in the environment where the problem happened and paste the output.

python -c "import platform; print(platform.platform())"
python -c "import sys; print('Python', sys.version)"
python -c "from pyfakefs import __version__; print('pyfakefs', __version__)"
python -c "import pytest; print('pytest', pytest.__version__)"
python -c "import platform; print(platform.platform())"
python -c "import sys; print('Python', sys.version)"
python -c "from pyfakefs import __version__; print('pyfakefs', __version__)"
python -c "import pytest; print('pytest', pytest.__version__)"
Linux-6.10.11-200.fc40.x86_64-x86_64-with-glibc2.39
Python 3.9.16 (main, Sep  6 2024, 09:21:47) 
[GCC 14.2.1 20240801 (Red Hat 14.2.1-1)]
pyfakefs 5.7.0
pytest 8.3.3
mrbean-bremen commented 1 month ago

Thanks! I can't think of any changes in the last version that would cause that, but obviously there is something I miss...

mrbean-bremen commented 1 month ago

Ok, found the problem - I did indeed miss this during the review, and there was no test in place that caught that..

mrbean-bremen commented 1 month ago

Should be fixed in main now, please check! I'll probably wait a couple of days, in case something else comes up, and make a patch release.

sj98ta commented 1 month ago

I confirmed that it now works as expect. Thanks for the quick fix!

mrbean-bremen commented 1 month ago

FYI: I made the patch release yesterday.