simplify load_setuptools_entrypoints and refine comments/docstrings

--HG--
branch : more_plugin
This commit is contained in:
holger krekel 2015-04-27 14:10:33 +02:00
parent c54afbe42e
commit b2d66b9e7b
3 changed files with 17 additions and 14 deletions

View File

@ -800,7 +800,10 @@ class Config(object):
args[:] = self.getini("addopts") + args args[:] = self.getini("addopts") + args
self._checkversion() self._checkversion()
self.pluginmanager.consider_preparse(args) self.pluginmanager.consider_preparse(args)
try:
self.pluginmanager.load_setuptools_entrypoints("pytest11") 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.pluginmanager.consider_env()
self.known_args_namespace = ns = self._parser.parse_known_args(args) self.known_args_namespace = ns = self._parser.parse_known_args(args)
try: try:

View File

@ -256,10 +256,6 @@ class PluginManager(object):
return hc return hc
return orig 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): def register(self, plugin, name=None):
""" Register a plugin and return its canonical name or None if the name """ 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 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 True if the plugin is already registered. """
return plugin in self._plugin2hookcallers 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): def get_plugin(self, name):
""" Return a plugin or None for the given name. """ """ Return a plugin or None for the given name. """
return self._name2plugin.get(name) return self._name2plugin.get(name)
@ -386,14 +389,11 @@ class PluginManager(object):
"unknown hook %r in plugin %r" %(name, plugin)) "unknown hook %r in plugin %r" %(name, plugin))
def load_setuptools_entrypoints(self, entrypoint_name): def load_setuptools_entrypoints(self, entrypoint_name):
""" Load modules from querying the specified entrypoint name. """ Load modules from querying the specified setuptools entrypoint name.
Return None if setuptools was not operable, otherwise Return the number of loaded plugins. """
the number of loaded plugins. """
try:
from pkg_resources import iter_entry_points, DistributionNotFound from pkg_resources import iter_entry_points, DistributionNotFound
except ImportError:
return # XXX issue a warning
for ep in iter_entry_points(entrypoint_name): 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: if self.get_plugin(ep.name) or ep.name in self._name2plugin:
continue continue
try: try:

View File

@ -419,8 +419,8 @@ class TestAddMethodOrdering:
def test_load_setuptools_not_installed(self, monkeypatch, pm): def test_load_setuptools_not_installed(self, monkeypatch, pm):
monkeypatch.setitem(py.std.sys.modules, 'pkg_resources', monkeypatch.setitem(py.std.sys.modules, 'pkg_resources',
py.std.types.ModuleType("pkg_resources")) py.std.types.ModuleType("pkg_resources"))
assert pm.load_setuptools_entrypoints("qwe") is None with pytest.raises(ImportError):
# ok, we did not explode pm.load_setuptools_entrypoints("qwe")
class TestPytestPluginInteractions: class TestPytestPluginInteractions: