Skip to content

Commit 2ee83a3

Browse files
committed
Use RealPathlibModule for all skipped modules
- avoids the use of FakePathLibPath in pathlib - add _pytest.pathlib to skipped modules, fake isinstance check for RealPathlibPathModule - prevents issue with _pytest.pathlib in pytest 7.0.0, which would cause all tests with fs fixture to fail - see #666
1 parent a96bdbb commit 2ee83a3

File tree

5 files changed

+34
-19
lines changed

5 files changed

+34
-19
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ The released versions correspond to PyPi releases.
1010
(see [#661](../../issues/661))
1111
* disallow `encoding` argument on binary `open()`
1212
(see [#664](../../issues/664))
13+
* fixed compatibility issue with pytest 7.0.0
14+
(see [#666](../../issues/666))
1315

1416
## [Version 4.5.4](https://pypi.python.org/pypi/pyfakefs/4.5.4) (2022-01-12)
1517
Minor bugfix release.

pyfakefs/fake_filesystem_unittest.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,6 @@ def _find_modules(self) -> None:
681681
for name, module in list(sys.modules.items()):
682682
try:
683683
if (self.use_cache and module in self.CACHED_MODULES or
684-
module in self.SKIPMODULES or
685684
not inspect.ismodule(module)):
686685
continue
687686
except Exception:
@@ -692,7 +691,8 @@ def _find_modules(self) -> None:
692691
if self.use_cache:
693692
self.__class__.CACHED_MODULES.add(module)
694693
continue
695-
skipped = (any([sn.startswith(module.__name__)
694+
skipped = (module in self.SKIPMODULES or
695+
any([sn.startswith(module.__name__)
696696
for sn in self._skip_names]))
697697
module_items = module.__dict__.copy().items()
698698

@@ -703,22 +703,22 @@ def _find_modules(self) -> None:
703703
for name, mod in modules.items():
704704
self.__class__.SKIPPED_FS_MODULES.setdefault(
705705
name, set()).add((module, mod.__name__))
706-
continue
707-
708-
for name, mod in modules.items():
709-
self.__class__.FS_MODULES.setdefault(name, set()).add(
710-
(module, mod.__name__))
711-
functions = {name: fct for name, fct in
712-
module_items
713-
if self._is_fs_function(fct)}
714-
715-
for name, fct in functions.items():
716-
self.__class__.FS_FUNCTIONS.setdefault(
717-
(name, fct.__name__, fct.__module__), set()).add(module)
718-
719-
# find default arguments that are file system functions
720-
if self.patch_default_args:
721-
self._find_def_values(module_items)
706+
else:
707+
for name, mod in modules.items():
708+
self.__class__.FS_MODULES.setdefault(name, set()).add(
709+
(module, mod.__name__))
710+
functions = {name: fct for name, fct in
711+
module_items
712+
if self._is_fs_function(fct)}
713+
714+
for name, fct in functions.items():
715+
self.__class__.FS_FUNCTIONS.setdefault(
716+
(name, fct.__name__, fct.__module__),
717+
set()).add(module)
718+
719+
# find default arguments that are file system functions
720+
if self.patch_default_args:
721+
self._find_def_values(module_items)
722722

723723
if self.use_cache:
724724
self.__class__.CACHED_MODULES.add(module)

pyfakefs/fake_pathlib.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,12 @@ class RealPathlibPathModule:
845845
"""Patches `pathlib.Path` by passing all calls to RealPathlibModule."""
846846
real_pathlib = None
847847

848+
@classmethod
849+
def __instancecheck__(cls, instance):
850+
# as we cannot derive from pathlib.Path, we fake
851+
# the inheritance to pass isinstance checks - see #666
852+
return isinstance(instance, PurePath)
853+
848854
def __init__(self):
849855
if self.real_pathlib is None:
850856
self.__class__.real_pathlib = RealPathlibModule()

pyfakefs/pytest_plugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ def my_fakefs_test(fs):
88
fs.create_file('/var/data/xx1.txt')
99
assert os.path.exists('/var/data/xx1.txt')
1010
"""
11-
11+
import _pytest
1212
import py
1313
import pytest
1414

1515
from pyfakefs.fake_filesystem_unittest import Patcher
1616

1717
Patcher.SKIPMODULES.add(py)
1818
Patcher.SKIPMODULES.add(pytest)
19+
Patcher.SKIPMODULES.add(_pytest.pathlib)
1920

2021

2122
@pytest.fixture

pyfakefs/tests/fake_filesystem_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,8 @@ def test_disk_full_after_reopened(self):
18141814
f.write('b' * 110)
18151815
with self.raises_os_error(errno.ENOSPC):
18161816
f.flush()
1817+
with self.open('bar.txt') as f:
1818+
self.assertEqual('', f.read())
18171819

18181820
def test_disk_full_append(self):
18191821
file_path = 'bar.txt'
@@ -1826,6 +1828,8 @@ def test_disk_full_append(self):
18261828
f.write('b' * 41)
18271829
with self.raises_os_error(errno.ENOSPC):
18281830
f.flush()
1831+
with self.open('bar.txt') as f:
1832+
self.assertEqual(f.read(), 'a' * 60)
18291833

18301834
def test_disk_full_after_reopened_rplus_seek(self):
18311835
with self.open('bar.txt', 'w') as f:
@@ -1838,6 +1842,8 @@ def test_disk_full_after_reopened_rplus_seek(self):
18381842
f.write('b' * 60)
18391843
with self.raises_os_error(errno.ENOSPC):
18401844
f.flush()
1845+
with self.open('bar.txt') as f:
1846+
self.assertEqual(f.read(), 'a' * 60)
18411847

18421848

18431849
class MountPointTest(TestCase):

0 commit comments

Comments
 (0)