@@ -105,6 +105,7 @@ def patchfs(
105
105
patch_open_code : PatchMode = PatchMode .OFF ,
106
106
patch_default_args : bool = False ,
107
107
use_cache : bool = True ,
108
+ use_dynamic_patch : bool = True ,
108
109
) -> Callable :
109
110
"""Convenience decorator to use patcher with additional parameters in a
110
111
test function.
@@ -132,6 +133,7 @@ def wrapped(*args, **kwargs):
132
133
patch_open_code = patch_open_code ,
133
134
patch_default_args = patch_default_args ,
134
135
use_cache = use_cache ,
136
+ use_dynamic_patch = use_dynamic_patch ,
135
137
) as p :
136
138
args = list (args )
137
139
args .append (p .fs )
@@ -167,6 +169,7 @@ def load_doctests(
167
169
use_known_patches : bool = True ,
168
170
patch_open_code : PatchMode = PatchMode .OFF ,
169
171
patch_default_args : bool = False ,
172
+ use_dynamic_patch : bool = True ,
170
173
) -> TestSuite : # pylint:disable=unused-argument
171
174
"""Load the doctest tests for the specified module into unittest.
172
175
Args:
@@ -186,6 +189,7 @@ def load_doctests(
186
189
use_known_patches = use_known_patches ,
187
190
patch_open_code = patch_open_code ,
188
191
patch_default_args = patch_default_args ,
192
+ use_dynamic_patch = use_dynamic_patch ,
189
193
is_doc_test = True ,
190
194
)
191
195
assert Patcher .DOC_PATCHER is not None
@@ -312,6 +316,7 @@ def setUpClassPyfakefs(
312
316
patch_open_code : PatchMode = PatchMode .OFF ,
313
317
patch_default_args : bool = False ,
314
318
use_cache : bool = True ,
319
+ use_dynamic_patch : bool = True ,
315
320
) -> None :
316
321
"""Similar to :py:func:`setUpPyfakefs`, but as a class method that
317
322
can be used in `setUpClass` instead of in `setUp`.
@@ -349,6 +354,7 @@ def setUpClassPyfakefs(
349
354
patch_open_code = patch_open_code ,
350
355
patch_default_args = patch_default_args ,
351
356
use_cache = use_cache ,
357
+ use_dynamic_patch = use_dynamic_patch ,
352
358
)
353
359
354
360
Patcher .PATCHER .setUp ()
@@ -513,6 +519,7 @@ def __init__(
513
519
patch_open_code : PatchMode = PatchMode .OFF ,
514
520
patch_default_args : bool = False ,
515
521
use_cache : bool = True ,
522
+ use_dynamic_patch : bool = True ,
516
523
is_doc_test : bool = False ,
517
524
) -> None :
518
525
"""
@@ -545,6 +552,9 @@ def __init__(
545
552
cached between tests for performance reasons. As this is a new
546
553
feature, this argument allows to turn it off in case it
547
554
causes any problems.
555
+ use_dynamic_patch: If `True`, dynamic patching after setup is used
556
+ (for example for modules loaded locally inside of functions).
557
+ Can be switched off if it causes unwanted side effects.
548
558
"""
549
559
self .is_doc_test = is_doc_test
550
560
if is_doc_test :
@@ -582,6 +592,7 @@ def __init__(
582
592
self .modules_to_reload .extend (modules_to_reload )
583
593
self .patch_default_args = patch_default_args
584
594
self .use_cache = use_cache
595
+ self .use_dynamic_patch = use_dynamic_patch
585
596
586
597
if use_known_patches :
587
598
from pyfakefs .patched_packages import (
@@ -914,6 +925,9 @@ def start_patching(self) -> None:
914
925
for module in self .modules_to_reload :
915
926
if sys .modules .get (module .__name__ ) is module :
916
927
reload (module )
928
+ if not self .use_dynamic_patch :
929
+ self ._dyn_patcher .cleanup ()
930
+ sys .meta_path .pop (0 )
917
931
918
932
def patch_functions (self ) -> None :
919
933
assert self ._stubs is not None
@@ -989,7 +1003,7 @@ def stop_patching(self, temporary=False) -> None:
989
1003
if self ._stubs :
990
1004
self ._stubs .smart_unset_all ()
991
1005
self .unset_defaults ()
992
- if self ._dyn_patcher :
1006
+ if self .use_dynamic_patch and self . _dyn_patcher :
993
1007
self ._dyn_patcher .cleanup ()
994
1008
sys .meta_path .pop (0 )
995
1009
0 commit comments