From eb1c32327b046d2356ef78f52c6c0a7cd83f8d72 Mon Sep 17 00:00:00 2001 From: squidfunk Date: Mon, 11 Sep 2023 18:17:41 +0200 Subject: [PATCH] Fixed group plugin crashing when using hooks --- material/plugins/group/plugin.py | 19 ++++++++++++++++--- src/plugins/group/plugin.py | 19 ++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/material/plugins/group/plugin.py b/material/plugins/group/plugin.py index b2d2deadb..4ab13dbf3 100644 --- a/material/plugins/group/plugin.py +++ b/material/plugins/group/plugin.py @@ -82,11 +82,18 @@ class GroupPlugin(BasePlugin[GroupConfig]): # Invoke `on_startup` event for plugins in group command = "serve" if self.is_serve else "build" for method in option.plugins.events["startup"]: - if method.__self__ in self.plugins.values(): + plugin = self._get_plugin(method) + + # Ensure that we have a method bound to a plugin (and not a hook) + if plugin and plugin in self.plugins.values(): method(command = command, dirty = self.is_dirty) # ------------------------------------------------------------------------- + # Retrieve plugin instance for bound method or nothing + def _get_plugin(self, method: Callable): + return getattr(method, "__self__", None) + # Retrieve priority of plugin method def _get_priority(self, method: Callable): return getattr(method, "mkdocs_priority", 0) @@ -117,7 +124,8 @@ class GroupPlugin(BasePlugin[GroupConfig]): head = methods[at] # Skip if the plugin is not part of the group - if not head.__self__ in self.plugins.values(): + plugin = self._get_plugin(head) + if not plugin or plugin not in self.plugins.values(): continue # Skip if the previous method has a higher priority than the current @@ -125,9 +133,14 @@ class GroupPlugin(BasePlugin[GroupConfig]): if self._get_priority(tail) > self._get_priority(head): continue + # Ensure that we have a method bound to a plugin (and not a hook) + plugin = self._get_plugin(tail) + if not plugin: + continue + # Both methods have the same priority, so we check if the ordering # of both methods is violated, and if it is, swap them - if (position < self._get_position(tail.__self__, config)): + if (position < self._get_position(plugin, config)): methods[at], methods[at - 1] = tail, head # ----------------------------------------------------------------------------- diff --git a/src/plugins/group/plugin.py b/src/plugins/group/plugin.py index b2d2deadb..4ab13dbf3 100644 --- a/src/plugins/group/plugin.py +++ b/src/plugins/group/plugin.py @@ -82,11 +82,18 @@ class GroupPlugin(BasePlugin[GroupConfig]): # Invoke `on_startup` event for plugins in group command = "serve" if self.is_serve else "build" for method in option.plugins.events["startup"]: - if method.__self__ in self.plugins.values(): + plugin = self._get_plugin(method) + + # Ensure that we have a method bound to a plugin (and not a hook) + if plugin and plugin in self.plugins.values(): method(command = command, dirty = self.is_dirty) # ------------------------------------------------------------------------- + # Retrieve plugin instance for bound method or nothing + def _get_plugin(self, method: Callable): + return getattr(method, "__self__", None) + # Retrieve priority of plugin method def _get_priority(self, method: Callable): return getattr(method, "mkdocs_priority", 0) @@ -117,7 +124,8 @@ class GroupPlugin(BasePlugin[GroupConfig]): head = methods[at] # Skip if the plugin is not part of the group - if not head.__self__ in self.plugins.values(): + plugin = self._get_plugin(head) + if not plugin or plugin not in self.plugins.values(): continue # Skip if the previous method has a higher priority than the current @@ -125,9 +133,14 @@ class GroupPlugin(BasePlugin[GroupConfig]): if self._get_priority(tail) > self._get_priority(head): continue + # Ensure that we have a method bound to a plugin (and not a hook) + plugin = self._get_plugin(tail) + if not plugin: + continue + # Both methods have the same priority, so we check if the ordering # of both methods is violated, and if it is, swap them - if (position < self._get_position(tail.__self__, config)): + if (position < self._get_position(plugin, config)): methods[at], methods[at - 1] = tail, head # -----------------------------------------------------------------------------