From 587e1a4ad89da2be46368baafb4f00b3b3d0c64a Mon Sep 17 00:00:00 2001 From: shniubobo Date: Sat, 31 Oct 2020 20:34:45 +0800 Subject: [PATCH] Fix plugins running twice in autoreload mode Fixes #2817 --- pelican/plugins/_utils.py | 3 +++ pelican/tests/test_plugins.py | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/pelican/plugins/_utils.py b/pelican/plugins/_utils.py index 4e6ec3c5..ffe32799 100644 --- a/pelican/plugins/_utils.py +++ b/pelican/plugins/_utils.py @@ -53,6 +53,9 @@ def load_legacy_plugin(plugin, plugin_paths): if spec is None: raise ImportError('Cannot import plugin `{}`'.format(plugin)) 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 diff --git a/pelican/tests/test_plugins.py b/pelican/tests/test_plugins.py index 06940884..29729539 100644 --- a/pelican/tests/test_plugins.py +++ b/pelican/tests/test_plugins.py @@ -131,6 +131,17 @@ class PluginTest(unittest.TestCase): 'normal subpackage plugin'}, get_plugin_names(plugins)) + # ensure normal plugins are loaded only once + SETTINGS = { + 'PLUGINS': ['normal_plugin'], + 'PLUGIN_PATHS': [self._NORMAL_PLUGIN_FOLDER], + } + plugins = load_plugins(SETTINGS) + for plugin in load_plugins(SETTINGS): + # The second load_plugins() should return the same plugin + # objects as the first one + self.assertIn(plugin, plugins) + # namespace plugin short SETTINGS = { 'PLUGINS': ['ns_plugin']