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
- change PyPy version in CI to 3.7
- fixes pytest-dev#723
  • Loading branch information
mrbean-bremen committed Oct 1, 2022
1 parent 0f88211 commit c6315ca
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
os: [ubuntu-latest, macOS-latest, windows-latest]
python-version: [3.7, 3.8, 3.9, "3.10", "3.11-dev"]
include:
- python-version: pypy3
- python-version: "pypy-3.7"
os: ubuntu-latest

steps:
Expand Down
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
6 changes: 4 additions & 2 deletions 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 @@ -123,14 +123,16 @@ def __init__(self, filesystem, path):
if isinstance(path, int):
if not use_scandir_package and (
sys.version_info < (3, 7) or
self.filesystem.is_windows_fs):
self.filesystem.is_windows_fs
):
raise NotImplementedError(
'scandir does not support file descriptor '
'path argument')
self.abspath = self.filesystem.absnormpath(
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
23 changes: 20 additions & 3 deletions pyfakefs/tests/fake_os_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5045,6 +5045,22 @@ 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")
@unittest.skipIf(sys.version_info < (3, 7),
"fd not supported for scandir")
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 +5152,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
5 changes: 5 additions & 0 deletions pyfakefs/tests/patched_packages_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
with pyfakefs.
"""
import os
import unittest

from pyfakefs import fake_filesystem_unittest
from pyfakefs.helpers import IS_PYPY

try:
import pandas as pd
Expand Down Expand Up @@ -45,13 +47,15 @@ def test_read_csv(self):
df = pd.read_csv(path)
assert (df.columns == ['1', '2', '3', '4']).all()

@unittest.skipIf(IS_PYPY, "Has a problem with current PyPy")
def test_read_table(self):
path = '/foo/bar.csv'
self.fs.create_file(path, contents='1|2|3|4')
df = pd.read_table(path, delimiter='|')
assert (df.columns == ['1', '2', '3', '4']).all()

if pd is not None and xlrd is not None:
@unittest.skipIf(IS_PYPY, "Has a problem with current PyPy")
def test_read_excel(self):
path = '/foo/bar.xlsx'
src_path = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -63,6 +67,7 @@ def test_read_excel(self):
assert (df.columns == [1, 2, 3, 4]).all()

if pd is not None and openpyxl is not None:
@unittest.skipIf(IS_PYPY, "Has a problem with current PyPy")
def test_write_excel(self):
self.fs.create_dir('/foo')
path = '/foo/bar.xlsx'
Expand Down

0 comments on commit c6315ca

Please sign in to comment.