From 8c3a06e8ca3c82c0161353475ccec262c880bba8 Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Sat, 29 Apr 2023 20:09:59 +0000 Subject: [PATCH 1/3] MAINT: Use importlib, pkgutil over __import__ --- asv/plugin_manager.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/asv/plugin_manager.py b/asv/plugin_manager.py index 9ffb5d385..806e5cd60 100644 --- a/asv/plugin_manager.py +++ b/asv/plugin_manager.py @@ -2,6 +2,7 @@ import sys import pkgutil +import importlib from . import commands, plugins from .console import log @@ -23,12 +24,9 @@ def __init__(self): def load_plugins(self, package): prefix = package.__name__ + "." - for module_finder, name, ispkg in pkgutil.iter_modules( - package.__path__, prefix - ): + for module_finder, name, ispkg in pkgutil.iter_modules(package.__path__, prefix): try: - __import__(name) - mod = sys.modules[name] + mod = importlib.import_module(name) self.init_plugin(mod) self._plugins.append(mod) except ModuleNotFoundError as err: @@ -42,7 +40,7 @@ def import_plugin(self, name): sys.path.insert(0, ".") name = name[1:] try: - mod = __import__(name, {}, {}, [], level=0) + mod = importlib.import_module(name) self.init_plugin(mod) self._plugins.append(mod) finally: From 434bb6d71395a461608d701be7cd5cff2ae677dc Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Sat, 29 Apr 2023 20:41:12 +0000 Subject: [PATCH 2/3] BUG: Actually load local plugins correctly --- asv/plugin_manager.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/asv/plugin_manager.py b/asv/plugin_manager.py index 806e5cd60..52a3fefc1 100644 --- a/asv/plugin_manager.py +++ b/asv/plugin_manager.py @@ -33,6 +33,14 @@ def load_plugins(self, package): log.error(f"Couldn't load {name} because\n{err}") continue # Couldn't find mamba + def _load_plugin_by_name(self, name): + prefix = plugins.__name__ + "." + for module_finder, module_name, ispkg in pkgutil.iter_modules(plugins.__path__, prefix): + if name in module_name: + mod = importlib.import_module(module_name) + return mod + return None + def import_plugin(self, name): extended = False if name.startswith("."): @@ -40,9 +48,13 @@ def import_plugin(self, name): sys.path.insert(0, ".") name = name[1:] try: - mod = importlib.import_module(name) - self.init_plugin(mod) - self._plugins.append(mod) + if extended: + mod = importlib.import_module(name) + else: + mod = self._load_plugin_by_name(name) + if mod: + self.init_plugin(mod) + self._plugins.append(mod) finally: if extended: del sys.path[0] From 29b6c3047c52dae78efd85d821726a4b051f26dc Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Sun, 30 Apr 2023 01:16:04 +0000 Subject: [PATCH 3/3] TST: Add a test for an asv plugin --- test/asv.conf.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/asv.conf.json b/test/asv.conf.json index 1f3a90d11..a8294d73c 100644 --- a/test/asv.conf.json +++ b/test/asv.conf.json @@ -26,7 +26,7 @@ "version": 1, - "plugins": [".example_plugin"], + "plugins": [".example_plugin", "github"], "benchmark_dir": "benchmark",