Skip to content

Commit

Permalink
Added missing cleanup for dynamic patcher
Browse files Browse the repository at this point in the history
- enable dynamic patcher by default
- adapted some tests to run with pytest
  • Loading branch information
mrbean-bremen committed Jan 11, 2018
1 parent 1b1ddd2 commit 08f45ff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
14 changes: 10 additions & 4 deletions pyfakefs/fake_filesystem_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class TestCase(unittest.TestCase):
def __init__(self, methodName='runTest', additional_skip_names=None,
patch_path=True, special_names=None,
modules_to_reload=None,
use_dynamic_patch=False):
use_dynamic_patch=True):
"""Creates the test class instance and the stubber used to stub out
file system related modules.
Expand All @@ -140,11 +140,10 @@ def __init__(self, methodName='runTest', additional_skip_names=None,
Note: this is done independently of `use_dynamic_patch`
Caution: this may not work with some Python versions
or have unwanted side effects.
use_dynamic_patch (experimental): If `True`, dynamic patching
use_dynamic_patch: If `True`, dynamic patching
after setup is used (for example for modules loaded locally
inside of functions).
Caution: this may not work with some Python versions
or have unwanted side effects.
Can be switched off if it causes unwanted side effects.
If you specify arguments `additional_skip_names` or `patch_path` here
and you have DocTests, consider also specifying the same arguments to
Expand Down Expand Up @@ -232,6 +231,7 @@ def setUpPyfakefs(self):
dyn_patcher = DynamicPatcher(self._stubber)
sys.meta_path.insert(0, dyn_patcher)
self.addCleanup(lambda: sys.meta_path.pop(0))
self.addCleanup(dyn_patcher.cleanup)

@DeprecationWarning
def tearDownPyfakefs(self):
Expand Down Expand Up @@ -436,6 +436,7 @@ class DynamicPatcher(object):
def __init__(self, patcher):
self._patcher = patcher
self._patching = False
self.sysmodules = {}
self.modules = self._patcher._fake_modules
if 'path' in self.modules:
self.modules['os.path'] = self.modules['path']
Expand All @@ -445,8 +446,13 @@ def __init__(self, patcher):
# otherwise the find_... methods will not be called
for module in self.modules:
if self.needs_patch(module) and module in sys.modules:
self.sysmodules[module] = sys.modules[module]
del sys.modules[module]

def cleanup(self):
for module in self.sysmodules:
sys.modules[module] = self.sysmodules[module]

def needs_patch(self, name):
"""Check if the module with the given name shall be replaced."""
if self._patching or name not in self.modules:
Expand Down
8 changes: 4 additions & 4 deletions tests/fake_os_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1391,7 +1391,7 @@ def test_fsync_pass_posix(self):
# Test that this doesn't raise anything
self.os.fsync(test_fd)
# And just for sanity, double-check that this still raises
self.assert_raises_os_error(errno.EBADF, self.os.fsync, test_fd + 1)
self.assert_raises_os_error(errno.EBADF, self.os.fsync, test_fd + 10)

def test_fsync_pass_windows(self):
self.check_windows_only()
Expand All @@ -1402,7 +1402,7 @@ def test_fsync_pass_windows(self):
# Test that this doesn't raise anything
self.os.fsync(test_fd)
# And just for sanity, double-check that this still raises
self.assert_raises_os_error(errno.EBADF, self.os.fsync, test_fd + 1)
self.assert_raises_os_error(errno.EBADF, self.os.fsync, test_fd + 10)
with self.open(test_file_path, 'r') as test_file:
test_fd = test_file.fileno()
self.assert_raises_os_error(errno.EBADF, self.os.fsync, test_fd)
Expand All @@ -1417,7 +1417,7 @@ def test_fdatasync_pass(self):
# Test that this doesn't raise anything
self.os.fdatasync(test_fd)
# And just for sanity, double-check that this still raises
self.assert_raises_os_error(errno.EBADF, self.os.fdatasync, test_fd + 1)
self.assert_raises_os_error(errno.EBADF, self.os.fdatasync, test_fd + 10)

def test_access700(self):
# set up
Expand Down Expand Up @@ -2837,7 +2837,7 @@ def test_fsync_pass(self):
# Test that this doesn't raise anything
self.os.fsync(test_fd)
# And just for sanity, double-check that this still raises
self.assert_raises_os_error(errno.EBADF, self.os.fsync, test_fd + 1)
self.assert_raises_os_error(errno.EBADF, self.os.fsync, test_fd + 10)

def test_chmod(self):
# set up
Expand Down

0 comments on commit 08f45ff

Please sign in to comment.