@@ -176,36 +176,29 @@ def _remove_modules(self, plugin_name):
176176 # Get the plugins import path
177177 base_name = self .base_import + plugin_name
178178
179- # Loop through all loaded modules
179+ # Remove modules from sys. modules
180180 for module in list (sys .modules ):
181+ if self ._is_related_module (base_name , module ):
182+ del sys .modules [module ]
181183
182- # Is the current module not within the plugin?
183- if not module .startswith (base_name ):
184+ # Unload AutoUnload instances
185+ for module , instances in list (_module_instances .items ()):
186+ if not self ._is_related_module (base_name , module ):
184187 continue
185188
186- # Does the current module have any AutoUnload objects?
187- if module in _module_instances :
188-
189- # Loop through all AutoUnload objects in the current module
190- for instance in _module_instances [module ]:
191-
192- # Use try/except in-case the instance
193- # does not have an _unload_instance method
194- try :
195-
196- # Unload the object
197- instance ._unload_instance ()
198-
199- # Was a NotImplementedError encountered?
200- except NotImplementedError :
201-
202- # Print the error to console, but allow all
203- # other AutoUnload instances to be unloaded
204- # and the plugin to be fully unloaded itself
205- except_hooks .print_exception ()
206-
207- # Remove the module from AutoUnload
208- del _module_instances [module ]
209-
210- # Delete the module
211- del sys .modules [module ]
189+ for instance in instances :
190+ try :
191+ instance ._unload_instance ()
192+ except NotImplementedError :
193+ # Print the error to console, but allow all
194+ # other AutoUnload instances to be unloaded
195+ # and the plugin to be fully unloaded itself
196+ except_hooks .print_exception ()
197+
198+ del _module_instances [module ]
199+
200+ @staticmethod
201+ def _is_related_module (base_name , module ):
202+ """Check if a plugin's base name is related to a module name."""
203+ return (module .startswith ('{}.' .format (base_name ))
204+ or module == base_name )
0 commit comments