47
47
from pyfakefs .deprecator import Deprecator
48
48
from pyfakefs .fake_filesystem import set_uid , set_gid , reset_ids
49
49
from pyfakefs .helpers import IS_PYPY
50
+ from pyfakefs .patched_packages import get_modules_to_patch , \
51
+ get_classes_to_patch , get_fake_module_classes
50
52
51
53
try :
52
54
from importlib .machinery import ModuleSpec
@@ -74,7 +76,8 @@ def patchfs(_func=None, *,
74
76
additional_skip_names = None ,
75
77
modules_to_reload = None ,
76
78
modules_to_patch = None ,
77
- allow_root_user = True ):
79
+ allow_root_user = True ,
80
+ use_known_patches = True ):
78
81
"""Convenience decorator to use patcher with additional parameters in a
79
82
test function.
80
83
@@ -96,7 +99,8 @@ def wrapped(*args, **kwargs):
96
99
additional_skip_names = additional_skip_names ,
97
100
modules_to_reload = modules_to_reload ,
98
101
modules_to_patch = modules_to_patch ,
99
- allow_root_user = allow_root_user ) as p :
102
+ allow_root_user = allow_root_user ,
103
+ use_known_patches = use_known_patches ) as p :
100
104
kwargs ['fs' ] = p .fs
101
105
return f (* args , ** kwargs )
102
106
@@ -117,7 +121,8 @@ def load_doctests(loader, tests, ignore, module,
117
121
additional_skip_names = None ,
118
122
modules_to_reload = None ,
119
123
modules_to_patch = None ,
120
- allow_root_user = True ): # pylint: disable=unused-argument
124
+ allow_root_user = True ,
125
+ use_known_patches = True ): # pylint: disable=unused-argument
121
126
"""Load the doctest tests for the specified module into unittest.
122
127
Args:
123
128
loader, tests, ignore : arguments passed in from `load_tests()`
@@ -129,7 +134,8 @@ def load_doctests(loader, tests, ignore, module,
129
134
_patcher = Patcher (additional_skip_names = additional_skip_names ,
130
135
modules_to_reload = modules_to_reload ,
131
136
modules_to_patch = modules_to_patch ,
132
- allow_root_user = allow_root_user )
137
+ allow_root_user = allow_root_user ,
138
+ use_known_patches = use_known_patches )
133
139
globs = _patcher .replace_globs (vars (module ))
134
140
tests .addTests (doctest .DocTestSuite (module ,
135
141
globs = globs ,
@@ -155,6 +161,8 @@ class TestCaseMixin:
155
161
modules_to_patch: A dictionary of fake modules mapped to the
156
162
fully qualified patched module names. Can be used to add patching
157
163
of modules not provided by `pyfakefs`.
164
+ use_known_patches: If True (the default), some patches for commonly
165
+ used packges are applied which make them usable with pyfakes.
158
166
159
167
If you specify some of these attributes here and you have DocTests,
160
168
consider also specifying the same arguments to :py:func:`load_doctests`.
@@ -190,7 +198,8 @@ def setUpPyfakefs(self,
190
198
additional_skip_names = None ,
191
199
modules_to_reload = None ,
192
200
modules_to_patch = None ,
193
- allow_root_user = True ):
201
+ allow_root_user = True ,
202
+ use_known_patches = True ):
194
203
"""Bind the file-related modules to the :py:class:`pyfakefs` fake file
195
204
system instead of the real file system. Also bind the fake `open()`
196
205
function.
@@ -212,7 +221,8 @@ def setUpPyfakefs(self,
212
221
additional_skip_names = additional_skip_names ,
213
222
modules_to_reload = modules_to_reload ,
214
223
modules_to_patch = modules_to_patch ,
215
- allow_root_user = allow_root_user
224
+ allow_root_user = allow_root_user ,
225
+ use_known_patches = use_known_patches
216
226
)
217
227
218
228
self ._stubber .setUp ()
@@ -247,7 +257,8 @@ def __init__(self, methodName='runTest',
247
257
additional_skip_names = None ,
248
258
modules_to_reload = None ,
249
259
modules_to_patch = None ,
250
- allow_root_user = True ):
260
+ allow_root_user = True ,
261
+ use_known_patches = True ):
251
262
"""Creates the test class instance and the patcher used to stub out
252
263
file system related modules.
253
264
@@ -261,6 +272,7 @@ def __init__(self, methodName='runTest',
261
272
self .modules_to_reload = modules_to_reload
262
273
self .modules_to_patch = modules_to_patch
263
274
self .allow_root_user = allow_root_user
275
+ self .use_known_patches = use_known_patches
264
276
265
277
@Deprecator ('add_real_file' )
266
278
def copyRealFile (self , real_file_path , fake_file_path = None ,
@@ -337,7 +349,7 @@ class Patcher:
337
349
338
350
def __init__ (self , additional_skip_names = None ,
339
351
modules_to_reload = None , modules_to_patch = None ,
340
- allow_root_user = True ):
352
+ allow_root_user = True , use_known_patches = True ):
341
353
"""For a description of the arguments, see TestCase.__init__"""
342
354
343
355
if not allow_root_user :
@@ -361,6 +373,12 @@ def __init__(self, additional_skip_names=None,
361
373
362
374
self .modules_to_reload = modules_to_reload or []
363
375
376
+ if use_known_patches :
377
+ modules_to_patch = modules_to_patch or {}
378
+ modules_to_patch .update (get_modules_to_patch ())
379
+ self ._class_modules .update (get_classes_to_patch ())
380
+ self ._fake_module_classes .update (get_fake_module_classes ())
381
+
364
382
if modules_to_patch is not None :
365
383
for name , fake_module in modules_to_patch .items ():
366
384
self ._fake_module_classes [name ] = fake_module
@@ -516,7 +534,8 @@ def _find_modules(self):
516
534
# where py.error has no __name__ attribute
517
535
# see https://github.com/pytest-dev/py/issues/73
518
536
continue
519
-
537
+ if name == 'pandas.io.parsers' :
538
+ print (name )
520
539
module_items = module .__dict__ .copy ().items ()
521
540
522
541
# suppress specific pytest warning - see #466
@@ -588,6 +607,8 @@ def start_patching(self):
588
607
self ._patching = True
589
608
590
609
for name , modules in self ._modules .items ():
610
+ if name == 'TextFileReader' :
611
+ print (name , modules )
591
612
for module , attr in modules :
592
613
self ._stubs .smart_set (
593
614
module , name , self .fake_modules [attr ])
0 commit comments