Skip to content

Commit

Permalink
Fix handling of O_TMPFILE flag in os.open
Browse files Browse the repository at this point in the history
- the check for the flag was incorrect
- fixes pytest-dev#723
  • Loading branch information
mrbean-bremen committed Oct 1, 2022
1 parent 0f88211 commit e1bf119
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ The released versions correspond to PyPi releases.

## Unreleased

### Fixes
* fixed handling of `O_TMPFILE` in `os.open` (caused handling of
`O_DIRECTORY` as `O_TMPFILE`) (see [#723](../../issues/723))

## [Version 4.7.0](https://pypi.python.org/pypi/pyfakefs/4.7.0) (2022-09-18)
Changed handling of nested fixtures and bug fixes.

Expand Down
2 changes: 1 addition & 1 deletion pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -3998,7 +3998,7 @@ def open(self, path: AnyStr, flags: int, mode: Optional[int] = None, *,
mode = 0o777 & ~self._umask()

has_tmpfile_flag = (hasattr(os, 'O_TMPFILE') and
flags & getattr(os, 'O_TMPFILE'))
flags & os.O_TMPFILE == os.O_TMPFILE)
open_modes = _OpenModes(
must_exist=not flags & os.O_CREAT and not has_tmpfile_flag,
can_read=not flags & os.O_WRONLY,
Expand Down
3 changes: 2 additions & 1 deletion pyfakefs/fake_scandir.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import sys

from pyfakefs.extra_packages import use_scandir_package
from pyfakefs.helpers import to_string
from pyfakefs.helpers import to_string, make_string_path

if sys.version_info >= (3, 6):
BaseClass = os.PathLike
Expand Down Expand Up @@ -131,6 +131,7 @@ def __init__(self, filesystem, path):
self.filesystem.get_open_file(path).get_object().path)
self.path = ''
else:
path = make_string_path(path)
self.abspath = self.filesystem.absnormpath(path)
self.path = to_string(path)
entries = self.filesystem.confirmdir(self.abspath).entries
Expand Down
21 changes: 18 additions & 3 deletions pyfakefs/tests/fake_os_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5045,6 +5045,20 @@ def test_scandir_stat_nlink(self):
self.assertEqual(1, scandir_stat_nlink)
self.assertEqual(1, self.os.stat(self.file_path).st_nlink)

@unittest.skipIf(not hasattr(os, 'O_DIRECTORY'),
"opening directory not supported")
def test_scandir_with_fd(self):
# regression test for #723
temp_dir = self.make_path('tmp', 'dir')
self.create_dir(temp_dir)
self.create_file(self.os.path.join(temp_dir, 'file1'))
self.create_file(self.os.path.join(temp_dir, 'file2'))
self.create_dir(self.os.path.join(temp_dir, 'subdir'))
self.os.chdir(temp_dir)
fd = self.os.open(temp_dir, flags=os.O_RDONLY | os.O_DIRECTORY)
children = [dir_entry.name for dir_entry in self.os.scandir(fd)]
assert sorted(children) == ['file1', 'file2', 'subdir']

def check_stat(self, absolute_symlink_expected_size,
relative_symlink_expected_size):
self.assertEqual(self.FILE_SIZE, self.dir_entries[1].stat().st_size)
Expand Down Expand Up @@ -5136,9 +5150,10 @@ def use_real_fs(self):
return True


@unittest.skipIf(sys.version_info < (3, 7) or TestCase.is_windows or
use_scandir_package,
'dir_fd support for os.scandir was introduced in Python 3.7')
@unittest.skipIf(TestCase.is_windows,
'dir_fd not supported for os.scandir in Windows')
@unittest.skipIf(use_scandir_package,
'no dir_fd support for scandir package')
class FakeScandirFdTest(FakeScandirTest):
def tearDown(self):
self.os.close(self.dir_fd)
Expand Down

0 comments on commit e1bf119

Please sign in to comment.