Skip to content

Commit 08f45ff

Browse files
committed
Added missing cleanup for dynamic patcher
- enable dynamic patcher by default - adapted some tests to run with pytest
1 parent 1b1ddd2 commit 08f45ff

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

pyfakefs/fake_filesystem_unittest.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class TestCase(unittest.TestCase):
113113
def __init__(self, methodName='runTest', additional_skip_names=None,
114114
patch_path=True, special_names=None,
115115
modules_to_reload=None,
116-
use_dynamic_patch=False):
116+
use_dynamic_patch=True):
117117
"""Creates the test class instance and the stubber used to stub out
118118
file system related modules.
119119
@@ -140,11 +140,10 @@ def __init__(self, methodName='runTest', additional_skip_names=None,
140140
Note: this is done independently of `use_dynamic_patch`
141141
Caution: this may not work with some Python versions
142142
or have unwanted side effects.
143-
use_dynamic_patch (experimental): If `True`, dynamic patching
143+
use_dynamic_patch: If `True`, dynamic patching
144144
after setup is used (for example for modules loaded locally
145145
inside of functions).
146-
Caution: this may not work with some Python versions
147-
or have unwanted side effects.
146+
Can be switched off if it causes unwanted side effects.
148147
149148
If you specify arguments `additional_skip_names` or `patch_path` here
150149
and you have DocTests, consider also specifying the same arguments to
@@ -232,6 +231,7 @@ def setUpPyfakefs(self):
232231
dyn_patcher = DynamicPatcher(self._stubber)
233232
sys.meta_path.insert(0, dyn_patcher)
234233
self.addCleanup(lambda: sys.meta_path.pop(0))
234+
self.addCleanup(dyn_patcher.cleanup)
235235

236236
@DeprecationWarning
237237
def tearDownPyfakefs(self):
@@ -436,6 +436,7 @@ class DynamicPatcher(object):
436436
def __init__(self, patcher):
437437
self._patcher = patcher
438438
self._patching = False
439+
self.sysmodules = {}
439440
self.modules = self._patcher._fake_modules
440441
if 'path' in self.modules:
441442
self.modules['os.path'] = self.modules['path']
@@ -445,8 +446,13 @@ def __init__(self, patcher):
445446
# otherwise the find_... methods will not be called
446447
for module in self.modules:
447448
if self.needs_patch(module) and module in sys.modules:
449+
self.sysmodules[module] = sys.modules[module]
448450
del sys.modules[module]
449451

452+
def cleanup(self):
453+
for module in self.sysmodules:
454+
sys.modules[module] = self.sysmodules[module]
455+
450456
def needs_patch(self, name):
451457
"""Check if the module with the given name shall be replaced."""
452458
if self._patching or name not in self.modules:

tests/fake_os_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ def test_fsync_pass_posix(self):
13911391
# Test that this doesn't raise anything
13921392
self.os.fsync(test_fd)
13931393
# And just for sanity, double-check that this still raises
1394-
self.assert_raises_os_error(errno.EBADF, self.os.fsync, test_fd + 1)
1394+
self.assert_raises_os_error(errno.EBADF, self.os.fsync, test_fd + 10)
13951395

13961396
def test_fsync_pass_windows(self):
13971397
self.check_windows_only()
@@ -1402,7 +1402,7 @@ def test_fsync_pass_windows(self):
14021402
# Test that this doesn't raise anything
14031403
self.os.fsync(test_fd)
14041404
# And just for sanity, double-check that this still raises
1405-
self.assert_raises_os_error(errno.EBADF, self.os.fsync, test_fd + 1)
1405+
self.assert_raises_os_error(errno.EBADF, self.os.fsync, test_fd + 10)
14061406
with self.open(test_file_path, 'r') as test_file:
14071407
test_fd = test_file.fileno()
14081408
self.assert_raises_os_error(errno.EBADF, self.os.fsync, test_fd)
@@ -1417,7 +1417,7 @@ def test_fdatasync_pass(self):
14171417
# Test that this doesn't raise anything
14181418
self.os.fdatasync(test_fd)
14191419
# And just for sanity, double-check that this still raises
1420-
self.assert_raises_os_error(errno.EBADF, self.os.fdatasync, test_fd + 1)
1420+
self.assert_raises_os_error(errno.EBADF, self.os.fdatasync, test_fd + 10)
14211421

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

28422842
def test_chmod(self):
28432843
# set up

0 commit comments

Comments
 (0)