tplugins: when loading plugins, use newer importlib mechanism - electrum - Electrum Bitcoin wallet (HTM) git clone https://git.parazyd.org/electrum (DIR) Log (DIR) Files (DIR) Refs (DIR) Submodules --- (DIR) commit e04e8d236573d5ad5cd40bcca8c6d613b6ee31a4 (DIR) parent aceb022f9df7c778856debecc53eaeaa49dfbfaa (HTM) Author: SomberNight <somber.night@protonmail.com> Date: Sun, 11 Nov 2018 23:55:34 +0100 plugins: when loading plugins, use newer importlib mechanism fixes #4842 Diffstat: M electrum/plugin.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) --- (DIR) diff --git a/electrum/plugin.py b/electrum/plugin.py t@@ -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 t@@ -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 t@@ -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)