Browse Source

plugins: when loading plugins, use newer importlib mechanism

fixes #4842
3.3.3.1
SomberNight 6 years ago
parent
commit
e04e8d2365
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 27
      electrum/plugin.py

27
electrum/plugin.py

@ -26,6 +26,7 @@ import traceback
import sys
import os
import pkgutil
import importlib.util
import time
import threading
from typing import NamedTuple, Any, Union, TYPE_CHECKING, Optional
@ -66,9 +67,16 @@ class Plugins(DaemonThread):
def load_plugins(self):
for loader, name, ispkg in pkgutil.iter_modules([self.pkgpath]):
mod = pkgutil.find_loader('electrum.plugins.' + name)
m = mod.load_module()
d = m.__dict__
full_name = f'electrum.plugins.{name}'
spec = importlib.util.find_spec(full_name)
if spec is None: # pkgutil found it but importlib can't ?!
raise Exception(f"Error pre-loading {full_name}: no spec")
try:
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
except Exception as e:
raise Exception(f"Error pre-loading {full_name}: {repr(e)}") from e
d = module.__dict__
gui_good = self.gui_name in d.get('available_for', [])
if not gui_good:
continue
@ -95,16 +103,17 @@ class Plugins(DaemonThread):
def load_plugin(self, name):
if name in self.plugins:
return self.plugins[name]
full_name = 'electrum.plugins.' + name + '.' + self.gui_name
loader = pkgutil.find_loader(full_name)
if not loader:
full_name = f'electrum.plugins.{name}.{self.gui_name}'
spec = importlib.util.find_spec(full_name)
if spec is None:
raise RuntimeError("%s implementation for %s plugin not found"
% (self.gui_name, name))
try:
p = loader.load_module()
plugin = p.Plugin(self, self.config, name)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
plugin = module.Plugin(self, self.config, name)
except Exception as e:
raise Exception(f"Error loading {name} plugin: {e}") from e
raise Exception(f"Error loading {name} plugin: {repr(e)}") from e
self.add_jobs(plugin.thread_jobs())
self.plugins[name] = plugin
self.print_error("loaded", name)

Loading…
Cancel
Save