85
85
86
86
def load_doctests (loader , tests , ignore , module ,
87
87
additional_skip_names = None ,
88
- patch_path = True , special_names = None ): # pylint: disable=unused-argument
88
+ patch_path = True ): # pylint: disable=unused-argument
89
89
"""Load the doctest tests for the specified module into unittest.
90
90
Args:
91
91
loader, tests, ignore : arguments passed in from `load_tests()`
@@ -96,7 +96,7 @@ def load_doctests(loader, tests, ignore, module,
96
96
File `example_test.py` in the pyfakefs release provides a usage example.
97
97
"""
98
98
_patcher = Patcher (additional_skip_names = additional_skip_names ,
99
- patch_path = patch_path , special_names = special_names )
99
+ patch_path = patch_path )
100
100
globs = _patcher .replace_globs (vars (module ))
101
101
tests .addTests (doctest .DocTestSuite (module ,
102
102
globs = globs ,
@@ -111,7 +111,7 @@ class TestCase(unittest.TestCase):
111
111
"""
112
112
113
113
def __init__ (self , methodName = 'runTest' , additional_skip_names = None ,
114
- patch_path = True , special_names = None ,
114
+ patch_path = True ,
115
115
modules_to_reload = None ,
116
116
use_dynamic_patch = True ):
117
117
"""Creates the test class instance and the stubber used to stub out
@@ -129,11 +129,6 @@ def __init__(self, methodName='runTest', additional_skip_names=None,
129
129
130
130
Irrespective of patch_path, module 'os.path' is still correctly faked
131
131
if imported the usual way using `import os` or `import os.path`.
132
- special_names: A dictionary with module names as key and a dictionary as
133
- value, where the key is the original name of the module to be patched,
134
- and the value is the name as it is imported.
135
- This allows to patch modules where some of the file system modules are
136
- imported as another name (e.g. `import os as _os`).
137
132
modules_to_reload (experimental): A list of modules that need to be reloaded
138
133
to be patched dynamically; may be needed if the module
139
134
imports file system modules under an alias
@@ -155,20 +150,13 @@ class MyTestCase(fake_filesystem_unittest.TestCase):
155
150
def __init__(self, methodName='runTest'):
156
151
super(MyTestCase, self).__init__(
157
152
methodName=methodName, additional_skip_names=['posixpath'])
158
-
159
-
160
- class AnotherTestCase(fake_filesystem_unittest.TestCase):
161
- def __init__(self, methodName='runTest'):
162
- # allow patching a module that imports `os` as `my_os`
163
- special_names = {'amodule': {'os': 'my_os'}}
164
- super(MyTestCase, self).__init__(
165
- methodName=methodName, special_names=special_names)
166
153
"""
167
154
super (TestCase , self ).__init__ (methodName )
168
155
self ._stubber = Patcher (additional_skip_names = additional_skip_names ,
169
- patch_path = patch_path ,
170
- special_names = special_names )
171
- self ._modules_to_reload = modules_to_reload or []
156
+ patch_path = patch_path )
157
+ self ._modules_to_reload = [tempfile ]
158
+ if modules_to_reload is not None :
159
+ self ._modules_to_reload .extend (modules_to_reload )
172
160
self ._use_dynamic_patch = use_dynamic_patch
173
161
174
162
@property
@@ -223,15 +211,19 @@ def setUpPyfakefs(self):
223
211
"""
224
212
self ._stubber .setUp ()
225
213
self .addCleanup (self ._stubber .tearDown )
214
+ dyn_patcher = DynamicPatcher (self ._stubber )
215
+ sys .meta_path .insert (0 , dyn_patcher )
226
216
227
217
for module in self ._modules_to_reload :
228
218
if module .__name__ in sys .modules :
229
219
reload (module )
220
+
230
221
if self ._use_dynamic_patch :
231
- dyn_patcher = DynamicPatcher (self ._stubber )
232
- sys .meta_path .insert (0 , dyn_patcher )
233
222
self .addCleanup (lambda : sys .meta_path .pop (0 ))
234
223
self .addCleanup (dyn_patcher .cleanup )
224
+ else :
225
+ dyn_patcher .cleanup ()
226
+ sys .meta_path .pop (0 )
235
227
236
228
@DeprecationWarning
237
229
def tearDownPyfakefs (self ):
@@ -268,13 +260,10 @@ class Patcher(object):
268
260
if HAS_PATHLIB :
269
261
SKIPNAMES .add ('pathlib' )
270
262
271
- def __init__ (self , additional_skip_names = None , patch_path = True ,
272
- special_names = None ):
263
+ def __init__ (self , additional_skip_names = None , patch_path = True ):
273
264
"""For a description of the arguments, see TestCase.__init__"""
274
265
275
266
self ._skipNames = self .SKIPNAMES .copy ()
276
- self ._special_names = special_names or {}
277
- self ._special_names ['tempfile' ] = {'os' : '_os' , 'io' : '_io' }
278
267
279
268
if additional_skip_names is not None :
280
269
self ._skipNames .update (additional_skip_names )
@@ -343,12 +332,6 @@ def _find_modules(self):
343
332
for name in self ._modules :
344
333
if inspect .ismodule (module .__dict__ .get (name )):
345
334
self ._modules [name ].add ((module , name ))
346
- if '__name__' in module .__dict__ and module .__name__ in self ._special_names :
347
- module_names = self ._special_names [module .__name__ ]
348
- for name in self ._modules :
349
- if name in module_names :
350
- if inspect .ismodule (module .__dict__ .get (module_names [name ])):
351
- self ._modules [name ].add ((module , module_names [name ]))
352
335
353
336
def _refresh (self ):
354
337
"""Renew the fake file system and set the _isStale flag to `False`."""
@@ -362,29 +345,8 @@ def _refresh(self):
362
345
self ._fake_modules ['path' ] = self ._fake_modules ['os' ].path
363
346
self .fake_open = fake_filesystem .FakeFileOpen (self .fs )
364
347
365
- if not self .IS_WINDOWS and 'tempfile' in sys .modules :
366
- self ._patch_tempfile ()
367
-
368
348
self ._isStale = False
369
349
370
- def _patch_tempfile (self ):
371
- """Hack to work around cached `os` functions in `tempfile`.
372
- Shall be replaced by a more generic mechanism.
373
- """
374
- if 'unlink' in tempfile ._TemporaryFileWrapper .__dict__ :
375
- # Python 2.7 to 3.2: unlink is a class method of _TemporaryFileWrapper
376
- tempfile ._TemporaryFileWrapper .unlink = self ._fake_modules ['os' ].unlink
377
-
378
- # Python 3.0 to 3.2 (and PyPy3 based on Python 3.2):
379
- # `TemporaryDirectory._rmtree` is used instead of `shutil.rmtree`
380
- # which uses several cached os functions - replace it with `shutil.rmtree`
381
- if 'TemporaryDirectory' in tempfile .__dict__ :
382
- tempfile .TemporaryDirectory ._rmtree = lambda o , path : shutil .rmtree (path )
383
- else :
384
- # Python > 3.2 - unlink is a default parameter of _TemporaryFileCloser
385
- tempfile ._TemporaryFileCloser .close .__defaults__ = (
386
- self ._fake_modules ['os' ].unlink ,)
387
-
388
350
def setUp (self , doctester = None ):
389
351
"""Bind the file-related modules to the :py:mod:`pyfakefs` fake
390
352
modules real ones. Also bind the fake `file()` and `open()` functions.
0 commit comments