Skip to content

Commit fc7f200

Browse files
committed
Do not skip filesystem modules by name
- allows using own modules with the same name - fixes #708
1 parent 6872f28 commit fc7f200

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

Diff for: CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ The released versions correspond to PyPi releases.
1919
(see [#707](../../issues/707))
2020
* return the expected type from `fcntl.ioctl` and `fcntl.fcntl` calls if `arg`
2121
is of type `byte`; the call itself does nothing as before
22+
* do not skip filesystem modules by name to allow using own modules with
23+
the same name (see [#707](../../issues/707))
2224

2325

2426
## [Version 4.6.3](https://pypi.python.org/pypi/pyfakefs/4.6.3) (2022-07-20)

Diff for: pyfakefs/fake_filesystem_unittest.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@
3535
pyfakefs by simply changing their base class from `:py:class`unittest.TestCase`
3636
to `:py:class`pyfakefs.fake_filesystem_unittest.TestCase`.
3737
"""
38+
import _io # type:ignore [import]
3839
import doctest
3940
import functools
41+
import genericpath
4042
import inspect
43+
import io
4144
import linecache
45+
import os
4246
import shutil
4347
import sys
4448
import tempfile
@@ -366,8 +370,6 @@ class Patcher:
366370
'''Stub nothing that is imported within these modules.
367371
`sys` is included to prevent `sys.path` from being stubbed with the fake
368372
`os.path`.
369-
The `pytest` and `py` modules are used by pytest and have to access the
370-
real file system.
371373
The `linecache` module is used to read the test file in case of test
372374
failure to get traceback information before test tear down.
373375
In order to make sure that reading the test file is not faked,
@@ -376,8 +378,21 @@ class Patcher:
376378
'''
377379
SKIPMODULES = {
378380
None, fake_filesystem, fake_filesystem_shutil,
379-
sys, linecache, tokenize
381+
sys, linecache, tokenize, os, io, _io, genericpath, os.path
380382
}
383+
if sys.platform == 'win32':
384+
import nt # type:ignore [import]
385+
import ntpath
386+
SKIPMODULES.add(nt)
387+
SKIPMODULES.add(ntpath)
388+
else:
389+
import posix
390+
import posixpath
391+
import fcntl
392+
SKIPMODULES.add(posix)
393+
SKIPMODULES.add(posixpath)
394+
SKIPMODULES.add(fcntl)
395+
381396
# caches all modules that do not have file system modules or function
382397
# to speed up _find_modules
383398
CACHED_MODULES: Set[ModuleType] = set()
@@ -391,8 +406,7 @@ class Patcher:
391406

392407
IS_WINDOWS = sys.platform in ('win32', 'cygwin')
393408

394-
SKIPNAMES = {'os', 'path', 'io', 'genericpath', 'fcntl',
395-
OS_MODULE, PATH_MODULE}
409+
SKIPNAMES: Set[str] = set()
396410

397411
# hold values from last call - if changed, the cache has to be invalidated
398412
PATCHED_MODULE_NAMES: Set[str] = set()

Diff for: pyfakefs/pytest_tests/io.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
This is a test case for pyfakefs issue #708.
3+
It tests the usage of an own module with the same name as a patched filesystem
4+
module, the content is taken from the issue.
5+
"""
6+
7+
8+
class InputStream:
9+
def __init__(self, name):
10+
self.name = name
11+
12+
def read(self):
13+
with open(self.name, 'r') as f:
14+
return f.readline()

Diff for: pyfakefs/pytest_tests/pytest_plugin_test.py

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import tempfile
44

55
from pyfakefs.fake_filesystem_unittest import Pause
6+
import pyfakefs.pytest_tests.io
67

78

89
def test_fs_fixture(fs):
@@ -50,3 +51,12 @@ def test_pause_resume_contextmanager(fs):
5051
assert os.path.exists(real_temp_file.name)
5152
assert not os.path.exists(real_temp_file.name)
5253
assert os.path.exists(fake_temp_file.name)
54+
55+
56+
def test_use_own_io_module(fs):
57+
filepath = 'foo.txt'
58+
with open(filepath, 'w') as f:
59+
f.write('bar')
60+
61+
stream = pyfakefs.pytest_tests.io.InputStream(filepath)
62+
assert stream.read() == 'bar'

0 commit comments

Comments
 (0)