@@ -619,7 +619,7 @@ def __init__(
619
619
self .use_cache = use_cache
620
620
self .use_dynamic_patch = use_dynamic_patch
621
621
self .module_cleanup_mode = module_cleanup_mode
622
- self .cleanup_handlers : Dict [str , Callable [[], bool ]] = {}
622
+ self .cleanup_handlers : Dict [str , Callable [[str ], bool ]] = {}
623
623
624
624
if use_known_patches :
625
625
from pyfakefs .patched_packages import (
@@ -683,7 +683,7 @@ def clear_cache(self) -> None:
683
683
"""Clear the module cache (convenience instance method)."""
684
684
self .__class__ .clear_fs_cache ()
685
685
686
- def register_cleanup_handler (self , name : str , handler : Callable [[], bool ]):
686
+ def register_cleanup_handler (self , name : str , handler : Callable [[str ], bool ]):
687
687
"""Register a handler for cleaning up a module after it had been loaded by
688
688
the dynamic patcher. This allows to handle modules that cannot be reloaded
689
689
without unwanted side effects.
@@ -965,9 +965,7 @@ def start_patching(self) -> None:
965
965
self .patch_functions ()
966
966
self .patch_defaults ()
967
967
968
- self ._dyn_patcher = DynamicPatcher (
969
- self , cleanup_handlers = self .cleanup_handlers
970
- )
968
+ self ._dyn_patcher = DynamicPatcher (self )
971
969
sys .meta_path .insert (0 , self ._dyn_patcher )
972
970
for module in self .modules_to_reload :
973
971
if sys .modules .get (module .__name__ ) is module :
@@ -1127,16 +1125,12 @@ class DynamicPatcher(MetaPathFinder, Loader):
1127
1125
Implements the protocol needed for import hooks.
1128
1126
"""
1129
1127
1130
- def __init__ (
1131
- self ,
1132
- patcher : Patcher ,
1133
- cleanup_handlers : Optional [Dict [str , Callable [[], bool ]]] = None ,
1134
- ) -> None :
1128
+ def __init__ (self , patcher : Patcher ) -> None :
1135
1129
self ._patcher = patcher
1136
1130
self .sysmodules = {}
1137
1131
self .modules = self ._patcher .fake_modules
1138
1132
self ._loaded_module_names : Set [str ] = set ()
1139
- self .cleanup_handlers = cleanup_handlers or {}
1133
+ self .cleanup_handlers = patcher . cleanup_handlers
1140
1134
1141
1135
# remove all modules that have to be patched from `sys.modules`,
1142
1136
# otherwise the find_... methods will not be called
@@ -1159,17 +1153,13 @@ def cleanup(self, cleanup_mode: ModuleCleanupMode) -> None:
1159
1153
]
1160
1154
# Delete all modules loaded during the test, ensuring that
1161
1155
# they are reloaded after the test.
1162
- # If cleanup_mode is set to RELOAD, or it is AUTO and django is imported,
1163
- # reload the modules instead - this is a workaround related to some internal
1164
- # module caching by django, that will likely change in the future.
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.
1165
1158
if cleanup_mode == ModuleCleanupMode .AUTO :
1166
- if "django" in sys .modules :
1167
- cleanup_mode = ModuleCleanupMode .RELOAD
1168
- else :
1169
- cleanup_mode = ModuleCleanupMode .DELETE
1159
+ cleanup_mode = ModuleCleanupMode .DELETE
1170
1160
for name in self ._loaded_module_names :
1171
1161
if name in sys .modules and name not in reloaded_module_names :
1172
- if name in self .cleanup_handlers and self .cleanup_handlers [name ]():
1162
+ if name in self .cleanup_handlers and self .cleanup_handlers [name ](name ):
1173
1163
continue
1174
1164
if cleanup_mode == ModuleCleanupMode .RELOAD :
1175
1165
try :
0 commit comments