Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/pluggy/_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def __init__(self, project_name: str) -> None:
#: The project name.
self.project_name: Final = project_name
self._name2plugin: Final[dict[str, _Plugin]] = {}
self._plugin2name: Final[dict[_Plugin, str]] = {}
self._name2dist: Final[dict[str, importlib.metadata.Distribution]] = {}
#: The "hook relay", used to call a hook on all registered plugins.
#: See :ref:`calling`.
Expand Down Expand Up @@ -239,6 +240,10 @@ def register(self, plugin: _Plugin, name: str | None = None) -> str | None:
hookimpls.append((hook_name, hookimpl, verified))

self._name2plugin[plugin_name] = plugin
try:
self._plugin2name[plugin] = plugin_name
except TypeError:
pass
for hook_name, hookimpl, verified in hookimpls:
hook = getattr(self.hook, hook_name, None)
if hook is None:
Expand Down Expand Up @@ -323,6 +328,10 @@ def unregister(

if not self.is_blocked(name):
self._name2plugin.pop(name, None)
try:
self._plugin2name.pop(plugin, None)
except TypeError:
pass
self._name2dist.pop(name, None)

return plugin
Expand Down Expand Up @@ -404,7 +413,10 @@ def get_plugins(self) -> set[Any]:

def is_registered(self, plugin: _Plugin) -> bool:
"""Return whether the plugin is already registered."""
return any(plugin == val for val in self._name2plugin.values())
try:
return plugin in self._plugin2name
except TypeError:
return any(plugin == val for val in self._name2plugin.values())

def get_canonical_name(self, plugin: _Plugin) -> str:
"""Return a canonical name for a plugin object.
Expand Down Expand Up @@ -649,14 +661,12 @@ def subset_hook_caller(
"""Return a proxy :class:`~pluggy.HookCaller` for the named hook which
calls all registered plugins except the ones from remove_plugins."""
orig: HookCaller = getattr(self.hook, name)
plugins_to_remove = set(remove_plugins)
# Optimization: discard plugins to remove which don't actually implement
# the hook, to make checks faster.
plugins_to_remove.intersection_update(
hookimpl.plugin for hookimpl in orig._hookimpls
)
implementing = {hookimpl.plugin for hookimpl in orig._hookimpls}
plugins_to_remove = {
p for p in remove_plugins if p in implementing or not self.is_registered(p)
}
# Optimization: if none of the plugins to remove actually implement the
# hook, avoid the subset hook caller overhead.
# hook or could be registered later, avoid the subset hook caller overhead.
if not plugins_to_remove:
return orig
return _SubsetHookCaller(orig, plugins_to_remove)
Expand Down
34 changes: 34 additions & 0 deletions testing/test_pluginmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,40 @@ def he_method1(self):
assert hc() == [2]


def test_subset_hook_caller_excludes_later_registered_plugins(
pm: PluginManager,
) -> None:
class Hooks:
@hookspec
def he_method1(self):
pass

pm.add_hookspecs(Hooks)

class Plugin1:
@hookimpl
def he_method1(self):
return 1 # pragma: no cover

class Plugin2:
@hookimpl(specname="he_method1")
def alias(self):
return 2 # pragma: no cover

class Plugin3:
@hookimpl
def he_method1(self):
return 3

plugin1, plugin2, plugin3 = Plugin1(), Plugin2(), Plugin3()
pm.register(plugin3)
hc = pm.subset_hook_caller("he_method1", remove_plugins=[plugin1, plugin2])

pm.register(plugin1)
pm.register(plugin2)
assert hc() == [3]


def test_get_hookimpls(pm: PluginManager) -> None:
class Hooks:
@hookspec
Expand Down