forked from github/pelican
Merge pull request #2730 from avaris/namespace-redux
Fix legacy plugin loading
This commit is contained in:
commit
59462ad415
3 changed files with 21 additions and 1 deletions
|
|
@ -3,6 +3,7 @@ import importlib.machinery
|
|||
import importlib.util
|
||||
import logging
|
||||
import pkgutil
|
||||
import sys
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
|
@ -47,8 +48,23 @@ def load_legacy_plugin(plugin, plugin_paths):
|
|||
if spec is None:
|
||||
raise ImportError('Cannot import plugin `{}`'.format(plugin))
|
||||
else:
|
||||
# create module object from spec
|
||||
mod = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(mod)
|
||||
# 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
|
||||
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
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
from .submodule import noop # noqa: F401
|
||||
|
||||
NAME = 'normal plugin'
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
def noop():
|
||||
pass
|
||||
Loading…
Add table
Add a link
Reference in a new issue