When reloading plugins delete top level module too

importlib.reload() has some differences in semantics from import
which leads to some plugins not reloading correctly
This commit is contained in:
qu1ck 2021-12-02 22:17:42 -08:00 committed by Jon Evans
parent 34a0d1947a
commit 8170e2f57c
1 changed files with 5 additions and 15 deletions

View File

@ -113,17 +113,10 @@ def LoadPluginModule(Dirname, ModuleName, FileName):
import sys import sys
import traceback import traceback
try:
from importlib import reload # Python 3.4 or above
except ImportError:
from imp import reload # Python <3.4; harmless alias on 2.7
global NOT_LOADED_WIZARDS global NOT_LOADED_WIZARDS
global FULL_BACK_TRACE global FULL_BACK_TRACE
global KICAD_PLUGINS global KICAD_PLUGINS
top_level_modules = KICAD_PLUGINS.keys()
try: # If there is an error loading the script, skip it try: # If there is an error loading the script, skip it
module_filename = os.path.join( Dirname, FileName ) module_filename = os.path.join( Dirname, FileName )
@ -134,19 +127,16 @@ def LoadPluginModule(Dirname, ModuleName, FileName):
plugin = KICAD_PLUGINS[ModuleName] plugin = KICAD_PLUGINS[ModuleName]
for dependency in plugin["dependencies"]: for dependency in plugin["dependencies"]:
if dependency in sys.modules and dependency not in top_level_modules: if dependency in sys.modules:
del sys.modules[dependency] del sys.modules[dependency]
mods_before = set( sys.modules ) mods_before = set( sys.modules )
mod = reload( plugin["ModuleName"] ) if sys.version_info >= (3,0,0):
import importlib
mod = importlib.import_module( ModuleName )
else: else:
if sys.version_info >= (3,0,0): mod = __import__( ModuleName, locals(), globals() )
import importlib
mod = importlib.import_module( ModuleName )
else:
mod = __import__( ModuleName, locals(), globals() )
mods_after = set( sys.modules ).difference( mods_before ) mods_after = set( sys.modules ).difference( mods_before )
dependencies = [m for m in mods_after if m.startswith(ModuleName)] dependencies = [m for m in mods_after if m.startswith(ModuleName)]