Skip to content

Commit a1210c9

Browse files
committed
Fix incorrect exception on opening non-existing directory
- fixes #986
1 parent 071b548 commit a1210c9

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

pyfakefs/fake_os.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,11 @@ def open(
254254
has_directory_flag = (
255255
hasattr(os, "O_DIRECTORY") and flags & os.O_DIRECTORY == os.O_DIRECTORY
256256
)
257-
if has_directory_flag and not self.filesystem.isdir(path):
257+
if (
258+
has_directory_flag
259+
and self.filesystem.exists(path)
260+
and not self.filesystem.isdir(path)
261+
):
258262
raise OSError(errno.ENOTDIR, "path is not a directory", path)
259263

260264
has_follow_flag = (

pyfakefs/tests/fake_os_test.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,10 @@ def test_open_raises_with_trailing_separator_windows(self):
605605
self.check_open_raises_with_trailing_separator(errno.EINVAL)
606606

607607
@unittest.skipIf(not hasattr(os, "O_DIRECTORY"), "opening directory not supported")
608-
def test_open_raises_if_not_dir(self):
608+
def test_open_with_o_directory(self):
609609
self.check_posix_only()
610+
with self.assertRaises(FileNotFoundError):
611+
self.os.open("bogus", os.O_RDONLY | os.O_DIRECTORY)
610612
file_path = self.make_path("file.txt")
611613
self.create_file(file_path, contents="foo")
612614
with self.assertRaises(NotADirectoryError):

0 commit comments

Comments
 (0)