Skip to content

Commit

Permalink
Avoid sys.modules reuse and replacement to fix tests
Browse files Browse the repository at this point in the history
(That suggests something is wrong with some plugins' initialization.)
  • Loading branch information
Kwpolska committed Jan 2, 2024
1 parent 5d30ad6 commit 43098d0
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions nikola/plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,8 @@ def __init__(self, plugin_places: List[Path]):
self._plugins_by_category = {}
self.logger = get_logger("PluginManager")

def locate_plugins(self, force=False) -> List[PluginCandidate]:
def locate_plugins(self) -> List[PluginCandidate]:
"""Locate plugins in plugin_places."""
if self.candidates and not force:
# Already located
return self.candidates

self.candidates = []

plugin_files: List[Path] = []
Expand Down Expand Up @@ -172,18 +168,15 @@ def load_plugins(self, candidates: List[PluginCandidate]) -> None:
except ValueError:
pass

if full_module_name.startswith("nikola.plugins") and full_module_name in sys.modules:
# Loaded by something else (a dependent plugin?)
module_object = sys.modules[full_module_name]
else:
try:
spec = importlib.util.spec_from_file_location(full_module_name, py_file_location)
module_object = importlib.util.module_from_spec(spec)
try:
spec = importlib.util.spec_from_file_location(full_module_name, py_file_location)
module_object = importlib.util.module_from_spec(spec)
if full_module_name not in sys.modules:
sys.modules[full_module_name] = module_object
spec.loader.exec_module(module_object)
except Exception:
self.logger.exception(f"{plugin_id} threw an exception while loading")
continue
spec.loader.exec_module(module_object)
except Exception:
self.logger.exception(f"{plugin_id} threw an exception while loading")
continue

plugin_classes = [
c
Expand Down

0 comments on commit 43098d0

Please sign in to comment.