From b880bfadaa6ae7e03fb463ecb4ccb4dcd147eedd Mon Sep 17 00:00:00 2001 From: Yasser Tahiri Date: Mon, 19 Jun 2023 10:01:44 +0400 Subject: [PATCH] . --- pelican/plugins/_utils.py | 41 ++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/pelican/plugins/_utils.py b/pelican/plugins/_utils.py index ac9f319f..a337db0f 100644 --- a/pelican/plugins/_utils.py +++ b/pelican/plugins/_utils.py @@ -53,27 +53,28 @@ def load_legacy_plugin(plugin, plugin_paths): spec = importlib.util.find_spec(plugin) if spec is None: raise ImportError('Cannot import plugin `{}`'.format(plugin)) - # Avoid loading the same plugin twice - if spec.name in sys.modules: - return sys.modules[spec.name] - # create module object from spec - mod = importlib.util.module_from_spec(spec) - # place it into sys.modules cache - # necessary if module imports itself at some point (e.g. packages) - sys.modules[spec.name] = mod - try: - # try to execute it inside module object - spec.loader.exec_module(mod) - except Exception: # problem with import + else: + # Avoid loading the same plugin twice + if spec.name in sys.modules: + return sys.modules[spec.name] + # create module object from spec + mod = importlib.util.module_from_spec(spec) + # place it into sys.modules cache + # necessary if module imports itself at some point (e.g. packages) + sys.modules[spec.name] = mod try: - # remove module from sys.modules since it can't be loaded - del sys.modules[spec.name] - except KeyError: - pass - raise + # try to execute it inside module object + spec.loader.exec_module(mod) + except Exception: # problem with import + try: + # remove module from sys.modules since it can't be loaded + del sys.modules[spec.name] + except KeyError: + pass + raise - # if all went well, we have the plugin module - return mod + # if all went well, we have the plugin module + return mod def load_plugins(settings): @@ -121,4 +122,4 @@ def get_plugin_name(plugin): if inspect.ismodule(plugin): return plugin.__name__ - return type(plugin).__qualname__ + return type(plugin).__qualname__ \ No newline at end of file