aboutsummaryrefslogtreecommitdiff
path: root/pyfakefs
diff options
context:
space:
mode:
authormrbean-bremen <hansemrbean@googlemail.com>2022-09-03 21:58:26 +0200
committermrbean-bremen <mrbean-bremen@users.noreply.github.com>2022-09-04 09:01:09 +0200
commitfc7f200174fad373bf1a5d283c32056b6c3b109d (patch)
treeedb572418584fb7a2a17ca81d72509ec4ce28c8e /pyfakefs
parent6872f28b95637aca06de60dcce35aff4d064c83d (diff)
downloadpyfakefs-fc7f200174fad373bf1a5d283c32056b6c3b109d.tar.gz
Do not skip filesystem modules by name
- allows using own modules with the same name - fixes #708
Diffstat (limited to 'pyfakefs')
-rw-r--r--pyfakefs/fake_filesystem_unittest.py24
-rw-r--r--pyfakefs/pytest_tests/io.py14
-rw-r--r--pyfakefs/pytest_tests/pytest_plugin_test.py10
3 files changed, 43 insertions, 5 deletions
diff --git a/pyfakefs/fake_filesystem_unittest.py b/pyfakefs/fake_filesystem_unittest.py
index aaebc64..6d62ac0 100644
--- a/pyfakefs/fake_filesystem_unittest.py
+++ b/pyfakefs/fake_filesystem_unittest.py
@@ -35,10 +35,14 @@ Existing unit tests that use the real file system can be retrofitted to use
pyfakefs by simply changing their base class from `:py:class`unittest.TestCase`
to `:py:class`pyfakefs.fake_filesystem_unittest.TestCase`.
"""
+import _io # type:ignore [import]
import doctest
import functools
+import genericpath
import inspect
+import io
import linecache
+import os
import shutil
import sys
import tempfile
@@ -366,8 +370,6 @@ class Patcher:
'''Stub nothing that is imported within these modules.
`sys` is included to prevent `sys.path` from being stubbed with the fake
`os.path`.
- The `pytest` and `py` modules are used by pytest and have to access the
- real file system.
The `linecache` module is used to read the test file in case of test
failure to get traceback information before test tear down.
In order to make sure that reading the test file is not faked,
@@ -376,8 +378,21 @@ class Patcher:
'''
SKIPMODULES = {
None, fake_filesystem, fake_filesystem_shutil,
- sys, linecache, tokenize
+ sys, linecache, tokenize, os, io, _io, genericpath, os.path
}
+ if sys.platform == 'win32':
+ import nt # type:ignore [import]
+ import ntpath
+ SKIPMODULES.add(nt)
+ SKIPMODULES.add(ntpath)
+ else:
+ import posix
+ import posixpath
+ import fcntl
+ SKIPMODULES.add(posix)
+ SKIPMODULES.add(posixpath)
+ SKIPMODULES.add(fcntl)
+
# caches all modules that do not have file system modules or function
# to speed up _find_modules
CACHED_MODULES: Set[ModuleType] = set()
@@ -391,8 +406,7 @@ class Patcher:
IS_WINDOWS = sys.platform in ('win32', 'cygwin')
- SKIPNAMES = {'os', 'path', 'io', 'genericpath', 'fcntl',
- OS_MODULE, PATH_MODULE}
+ SKIPNAMES: Set[str] = set()
# hold values from last call - if changed, the cache has to be invalidated
PATCHED_MODULE_NAMES: Set[str] = set()
diff --git a/pyfakefs/pytest_tests/io.py b/pyfakefs/pytest_tests/io.py
new file mode 100644
index 0000000..5d622f1
--- /dev/null
+++ b/pyfakefs/pytest_tests/io.py
@@ -0,0 +1,14 @@
+"""
+This is a test case for pyfakefs issue #708.
+It tests the usage of an own module with the same name as a patched filesystem
+module, the content is taken from the issue.
+"""
+
+
+class InputStream:
+ def __init__(self, name):
+ self.name = name
+
+ def read(self):
+ with open(self.name, 'r') as f:
+ return f.readline()
diff --git a/pyfakefs/pytest_tests/pytest_plugin_test.py b/pyfakefs/pytest_tests/pytest_plugin_test.py
index 5632ff6..04741f6 100644
--- a/pyfakefs/pytest_tests/pytest_plugin_test.py
+++ b/pyfakefs/pytest_tests/pytest_plugin_test.py
@@ -3,6 +3,7 @@ import os
import tempfile
from pyfakefs.fake_filesystem_unittest import Pause
+import pyfakefs.pytest_tests.io
def test_fs_fixture(fs):
@@ -50,3 +51,12 @@ def test_pause_resume_contextmanager(fs):
assert os.path.exists(real_temp_file.name)
assert not os.path.exists(real_temp_file.name)
assert os.path.exists(fake_temp_file.name)
+
+
+def test_use_own_io_module(fs):
+ filepath = 'foo.txt'
+ with open(filepath, 'w') as f:
+ f.write('bar')
+
+ stream = pyfakefs.pytest_tests.io.InputStream(filepath)
+ assert stream.read() == 'bar'