From a042c572272f0a1a199e11ce2a9249e8989443d7 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Sat, 25 Apr 2015 20:42:41 +0200 Subject: [PATCH] ensure proper get_name references --HG-- branch : more_plugin --- _pytest/core.py | 16 +++++++++++++--- testing/test_core.py | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/_pytest/core.py b/_pytest/core.py index 99c36de2d..2a2ba1b7d 100644 --- a/_pytest/core.py +++ b/_pytest/core.py @@ -237,6 +237,10 @@ class PluginManager(object): return TracedHookExecution(self, before, after).undo def make_hook_caller(self, name, plugins): + """ Return a new HookCaller instance which manages calls to + all methods named "name" in the plugins. The new hook caller + is registered internally such that when one of the plugins gets + unregistered, its method will be removed from the hook caller. """ caller = getattr(self.hook, name) hc = HookCaller(caller.name, self._hookexec, caller._specmodule_or_class) for plugin in plugins: @@ -248,7 +252,7 @@ class PluginManager(object): return hc def get_canonical_name(self, plugin): - """ Return canonical name for the plugin object. """ + """ Return canonical name for a plugin object. """ return getattr(plugin, "__name__", None) or str(id(plugin)) def register(self, plugin, name=None): @@ -288,7 +292,7 @@ class PluginManager(object): be specified. """ if name is None: assert plugin is not None - name = self.get_canonical_name(plugin) + name = self.get_name(plugin) if plugin is None: plugin = self.get_plugin(name) @@ -340,9 +344,15 @@ class PluginManager(object): """ Return a plugin or None for the given name. """ return self._name2plugin.get(name) + def get_name(self, plugin): + """ Return name for registered plugin or None if not registered. """ + for name, val in self._name2plugin.items(): + if plugin == val: + return name + def _verify_hook(self, hook, plugin): method = getattr(plugin, hook.name) - pluginname = self.get_canonical_name(plugin) + pluginname = self.get_name(plugin) if hook.is_historic() and hasattr(method, "hookwrapper"): raise PluginValidationError( diff --git a/testing/test_core.py b/testing/test_core.py index d369c3d1c..5fef02f74 100644 --- a/testing/test_core.py +++ b/testing/test_core.py @@ -34,6 +34,22 @@ class TestPluginManager: assert pm.unregister(a1) == a1 assert not pm.is_registered(a1) + def test_pm_name(self, pm): + class A: pass + a1 = A() + name = pm.register(a1, name="hello") + assert name == "hello" + pm.unregister(a1) + assert pm.get_plugin(a1) is None + assert not pm.is_registered(a1) + assert not pm.get_plugins() + name2 = pm.register(a1, name="hello") + assert name2 == name + pm.unregister(name="hello") + assert pm.get_plugin(a1) is None + assert not pm.is_registered(a1) + assert not pm.get_plugins() + def test_set_blocked(self, pm): class A: pass a1 = A()