pytest-dev / pyfakefs

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

`os.open(O_DIRECTORY)` should raise `ENOENT` when the file does not exist #986

Closed vector-of-bool closed 3 months ago

vector-of-bool commented 3 months ago

Describe the bug In the case of os.open with O_DIRECTORY, if the named file does not exist, it should fail with ENOENT and not ENOTDIR. From POSIX:

O_DIRECTORY: If path resolves to a non-directory file, fail and set errno to [ENOTDIR].

The issue appears to be the use of isdir() to check if the path is a directory, which simply returns False when the file does not exist.

How To Reproduce

def do_open_dir_noent():
    with pytest.raises(FileNotFoundError):
        os.open("this-path-does-not-name-an-existing-file", os.O_RDONLY | os.O_DIRECTORY)

def test_open_dir_noent_realfs():
    do_open_dir_noent()

def test_open_dir_noent_fakefs(fs):
    do_open_dir_noent()

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

$ python -c "import platform; print(platform.platform())"
Linux-6.7.6-arch1-1-x86_64-with-glibc2.39
$ python -c "import sys; print('Python', sys.version)"
Python 3.11.7 (main, Jan 29 2024, 16:03:57) [GCC 13.2.1 20230801]
$ python -c "from pyfakefs import __version__; print('pyfakefs', __version__)"
pyfakefs 5.4.dev0
$ python -c "import pytest; print('pytest', pytest.__version__)"
pytest 7.2.0