From b2d66b9e7b25d3de5e3d5b19454817b68bcc427c Mon Sep 17 00:00:00 2001 From: holger krekel Date: Mon, 27 Apr 2015 14:10:33 +0200 Subject: [PATCH] simplify load_setuptools_entrypoints and refine comments/docstrings --HG-- branch : more_plugin --- _pytest/config.py | 5 ++++- _pytest/core.py | 22 +++++++++++----------- testing/test_core.py | 4 ++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/_pytest/config.py b/_pytest/config.py index fb7441dc1..f37d417b2 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -800,7 +800,10 @@ class Config(object): args[:] = self.getini("addopts") + args self._checkversion() self.pluginmanager.consider_preparse(args) - self.pluginmanager.load_setuptools_entrypoints("pytest11") + try: + self.pluginmanager.load_setuptools_entrypoints("pytest11") + except ImportError as e: + self.warn("I2", "could not load setuptools entry import: %s" % (e,)) self.pluginmanager.consider_env() self.known_args_namespace = ns = self._parser.parse_known_args(args) try: diff --git a/_pytest/core.py b/_pytest/core.py index 240e93928..5dcc30801 100644 --- a/_pytest/core.py +++ b/_pytest/core.py @@ -256,10 +256,6 @@ class PluginManager(object): return hc return orig - def get_canonical_name(self, plugin): - """ Return canonical name for a plugin object. """ - return getattr(plugin, "__name__", None) or str(id(plugin)) - def register(self, plugin, name=None): """ Register a plugin and return its canonical name or None if the name is blocked from registering. Raise a ValueError if the plugin is already @@ -344,6 +340,13 @@ class PluginManager(object): """ Return True if the plugin is already registered. """ return plugin in self._plugin2hookcallers + def get_canonical_name(self, plugin): + """ Return canonical name for a plugin object. Note that a plugin + may be registered under a different name which was specified + by the caller of register(plugin, name). To obtain the name + of an registered plugin use ``get_name(plugin)`` instead.""" + return getattr(plugin, "__name__", None) or str(id(plugin)) + def get_plugin(self, name): """ Return a plugin or None for the given name. """ return self._name2plugin.get(name) @@ -386,14 +389,11 @@ class PluginManager(object): "unknown hook %r in plugin %r" %(name, plugin)) def load_setuptools_entrypoints(self, entrypoint_name): - """ Load modules from querying the specified entrypoint name. - Return None if setuptools was not operable, otherwise - the number of loaded plugins. """ - try: - from pkg_resources import iter_entry_points, DistributionNotFound - except ImportError: - return # XXX issue a warning + """ Load modules from querying the specified setuptools entrypoint name. + Return the number of loaded plugins. """ + from pkg_resources import iter_entry_points, DistributionNotFound for ep in iter_entry_points(entrypoint_name): + # is the plugin registered or blocked? if self.get_plugin(ep.name) or ep.name in self._name2plugin: continue try: diff --git a/testing/test_core.py b/testing/test_core.py index 64805546c..4975e7e05 100644 --- a/testing/test_core.py +++ b/testing/test_core.py @@ -419,8 +419,8 @@ class TestAddMethodOrdering: def test_load_setuptools_not_installed(self, monkeypatch, pm): monkeypatch.setitem(py.std.sys.modules, 'pkg_resources', py.std.types.ModuleType("pkg_resources")) - assert pm.load_setuptools_entrypoints("qwe") is None - # ok, we did not explode + with pytest.raises(ImportError): + pm.load_setuptools_entrypoints("qwe") class TestPytestPluginInteractions: