47
47
import sys
48
48
import tempfile
49
49
import tokenize
50
- from enum import Enum
51
50
from importlib .abc import Loader , MetaPathFinder
52
51
from types import ModuleType , TracebackType , FunctionType
53
52
from typing import (
95
94
PATH_MODULE = "ntpath" if sys .platform == "win32" else "posixpath"
96
95
97
96
98
- class ModuleCleanupMode (Enum ):
99
- """Defines the behavior of module cleanup on dynamic patcher shutdown."""
100
-
101
- AUTO = 1
102
- DELETE = 2
103
- RELOAD = 3
104
-
105
-
106
97
def patchfs (
107
98
_func : Optional [Callable ] = None ,
108
99
* ,
@@ -115,7 +106,6 @@ def patchfs(
115
106
patch_default_args : bool = False ,
116
107
use_cache : bool = True ,
117
108
use_dynamic_patch : bool = True ,
118
- module_cleanup_mode : ModuleCleanupMode = ModuleCleanupMode .AUTO ,
119
109
) -> Callable :
120
110
"""Convenience decorator to use patcher with additional parameters in a
121
111
test function.
@@ -144,7 +134,6 @@ def wrapped(*args, **kwargs):
144
134
patch_default_args = patch_default_args ,
145
135
use_cache = use_cache ,
146
136
use_dynamic_patch = use_dynamic_patch ,
147
- module_cleanup_mode = module_cleanup_mode ,
148
137
) as p :
149
138
args = list (args )
150
139
args .append (p .fs )
@@ -181,7 +170,6 @@ def load_doctests(
181
170
patch_open_code : PatchMode = PatchMode .OFF ,
182
171
patch_default_args : bool = False ,
183
172
use_dynamic_patch : bool = True ,
184
- module_cleanup_mode : ModuleCleanupMode = ModuleCleanupMode .AUTO ,
185
173
) -> TestSuite : # pylint:disable=unused-argument
186
174
"""Load the doctest tests for the specified module into unittest.
187
175
Args:
@@ -202,7 +190,6 @@ def load_doctests(
202
190
patch_open_code = patch_open_code ,
203
191
patch_default_args = patch_default_args ,
204
192
use_dynamic_patch = use_dynamic_patch ,
205
- module_cleanup_mode = module_cleanup_mode ,
206
193
is_doc_test = True ,
207
194
)
208
195
assert Patcher .DOC_PATCHER is not None
@@ -283,7 +270,6 @@ def setUpPyfakefs(
283
270
patch_default_args : bool = False ,
284
271
use_cache : bool = True ,
285
272
use_dynamic_patch : bool = True ,
286
- module_cleanup_mode : ModuleCleanupMode = ModuleCleanupMode .AUTO ,
287
273
) -> None :
288
274
"""Bind the file-related modules to the :py:class:`pyfakefs` fake file
289
275
system instead of the real file system. Also bind the fake `open()`
@@ -316,7 +302,6 @@ def setUpPyfakefs(
316
302
patch_default_args = patch_default_args ,
317
303
use_cache = use_cache ,
318
304
use_dynamic_patch = use_dynamic_patch ,
319
- module_cleanup_mode = module_cleanup_mode ,
320
305
)
321
306
322
307
self ._patcher .setUp ()
@@ -334,7 +319,6 @@ def setUpClassPyfakefs(
334
319
patch_default_args : bool = False ,
335
320
use_cache : bool = True ,
336
321
use_dynamic_patch : bool = True ,
337
- module_cleanup_mode : ModuleCleanupMode = ModuleCleanupMode .AUTO ,
338
322
) -> None :
339
323
"""Similar to :py:func:`setUpPyfakefs`, but as a class method that
340
324
can be used in `setUpClass` instead of in `setUp`.
@@ -373,7 +357,6 @@ def setUpClassPyfakefs(
373
357
patch_default_args = patch_default_args ,
374
358
use_cache = use_cache ,
375
359
use_dynamic_patch = use_dynamic_patch ,
376
- module_cleanup_mode = module_cleanup_mode ,
377
360
)
378
361
379
362
Patcher .PATCHER .setUp ()
@@ -539,7 +522,6 @@ def __init__(
539
522
patch_default_args : bool = False ,
540
523
use_cache : bool = True ,
541
524
use_dynamic_patch : bool = True ,
542
- module_cleanup_mode : ModuleCleanupMode = ModuleCleanupMode .AUTO ,
543
525
is_doc_test : bool = False ,
544
526
) -> None :
545
527
"""
@@ -575,11 +557,6 @@ def __init__(
575
557
use_dynamic_patch: If `True`, dynamic patching after setup is used
576
558
(for example for modules loaded locally inside of functions).
577
559
Can be switched off if it causes unwanted side effects.
578
- module_cleanup_mode: Defines how the modules in the dynamic patcher are
579
- cleaned up after the test. The default (AUTO) currently depends
580
- on the availability of the `django` module, DELETE will delete
581
- all dynamically loaded modules, RELOAD will reload them.
582
- This option is subject to change in later versions.
583
560
"""
584
561
self .is_doc_test = is_doc_test
585
562
if is_doc_test :
@@ -618,7 +595,6 @@ def __init__(
618
595
self .patch_default_args = patch_default_args
619
596
self .use_cache = use_cache
620
597
self .use_dynamic_patch = use_dynamic_patch
621
- self .module_cleanup_mode = module_cleanup_mode
622
598
self .cleanup_handlers : Dict [str , Callable [[str ], bool ]] = {}
623
599
624
600
if use_known_patches :
@@ -971,7 +947,7 @@ def start_patching(self) -> None:
971
947
if sys .modules .get (module .__name__ ) is module :
972
948
reload (module )
973
949
if not self .use_dynamic_patch :
974
- self ._dyn_patcher .cleanup (ModuleCleanupMode . DELETE )
950
+ self ._dyn_patcher .cleanup ()
975
951
sys .meta_path .pop (0 )
976
952
977
953
def patch_functions (self ) -> None :
@@ -1049,7 +1025,7 @@ def stop_patching(self, temporary=False) -> None:
1049
1025
self ._stubs .smart_unset_all ()
1050
1026
self .unset_defaults ()
1051
1027
if self .use_dynamic_patch and self ._dyn_patcher :
1052
- self ._dyn_patcher .cleanup (self . module_cleanup_mode )
1028
+ self ._dyn_patcher .cleanup ()
1053
1029
sys .meta_path .pop (0 )
1054
1030
1055
1031
@property
@@ -1142,7 +1118,7 @@ def __init__(self, patcher: Patcher) -> None:
1142
1118
for name , module in self .modules .items ():
1143
1119
sys .modules [name ] = module
1144
1120
1145
- def cleanup (self , cleanup_mode : ModuleCleanupMode ) -> None :
1121
+ def cleanup (self ) -> None :
1146
1122
for module_name in self .sysmodules :
1147
1123
sys .modules [module_name ] = self .sysmodules [module_name ]
1148
1124
for module in self ._patcher .modules_to_reload :
@@ -1153,21 +1129,11 @@ def cleanup(self, cleanup_mode: ModuleCleanupMode) -> None:
1153
1129
]
1154
1130
# Delete all modules loaded during the test, ensuring that
1155
1131
# they are reloaded after the test.
1156
- # If cleanup_mode is set to RELOAD, reload the modules instead.
1157
- # This is probably not needed anymore with the cleanup handlers in place.
1158
- if cleanup_mode == ModuleCleanupMode .AUTO :
1159
- cleanup_mode = ModuleCleanupMode .DELETE
1160
1132
for name in self ._loaded_module_names :
1161
1133
if name in sys .modules and name not in reloaded_module_names :
1162
1134
if name in self .cleanup_handlers and self .cleanup_handlers [name ](name ):
1163
1135
continue
1164
- if cleanup_mode == ModuleCleanupMode .RELOAD :
1165
- try :
1166
- reload (sys .modules [name ])
1167
- except Exception :
1168
- del sys .modules [name ]
1169
- else :
1170
- del sys .modules [name ]
1136
+ del sys .modules [name ]
1171
1137
1172
1138
def needs_patch (self , name : str ) -> bool :
1173
1139
"""Check if the module with the given name shall be replaced."""
0 commit comments