Skip to content

Commit eb1c323

Browse files
committed
Fixed group plugin crashing when using hooks
1 parent c9a7339 commit eb1c323

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

material/plugins/group/plugin.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,18 @@ def on_config(self, config):
8282
# Invoke `on_startup` event for plugins in group
8383
command = "serve" if self.is_serve else "build"
8484
for method in option.plugins.events["startup"]:
85-
if method.__self__ in self.plugins.values():
85+
plugin = self._get_plugin(method)
86+
87+
# Ensure that we have a method bound to a plugin (and not a hook)
88+
if plugin and plugin in self.plugins.values():
8689
method(command = command, dirty = self.is_dirty)
8790

8891
# -------------------------------------------------------------------------
8992

93+
# Retrieve plugin instance for bound method or nothing
94+
def _get_plugin(self, method: Callable):
95+
return getattr(method, "__self__", None)
96+
9097
# Retrieve priority of plugin method
9198
def _get_priority(self, method: Callable):
9299
return getattr(method, "mkdocs_priority", 0)
@@ -117,17 +124,23 @@ def _patch(self, methods: list[Callable], config: MkDocsConfig):
117124
head = methods[at]
118125

119126
# Skip if the plugin is not part of the group
120-
if not head.__self__ in self.plugins.values():
127+
plugin = self._get_plugin(head)
128+
if not plugin or plugin not in self.plugins.values():
121129
continue
122130

123131
# Skip if the previous method has a higher priority than the current
124132
# one, because we know we can't swap them anyway
125133
if self._get_priority(tail) > self._get_priority(head):
126134
continue
127135

136+
# Ensure that we have a method bound to a plugin (and not a hook)
137+
plugin = self._get_plugin(tail)
138+
if not plugin:
139+
continue
140+
128141
# Both methods have the same priority, so we check if the ordering
129142
# of both methods is violated, and if it is, swap them
130-
if (position < self._get_position(tail.__self__, config)):
143+
if (position < self._get_position(plugin, config)):
131144
methods[at], methods[at - 1] = tail, head
132145

133146
# -----------------------------------------------------------------------------

src/plugins/group/plugin.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,18 @@ def on_config(self, config):
8282
# Invoke `on_startup` event for plugins in group
8383
command = "serve" if self.is_serve else "build"
8484
for method in option.plugins.events["startup"]:
85-
if method.__self__ in self.plugins.values():
85+
plugin = self._get_plugin(method)
86+
87+
# Ensure that we have a method bound to a plugin (and not a hook)
88+
if plugin and plugin in self.plugins.values():
8689
method(command = command, dirty = self.is_dirty)
8790

8891
# -------------------------------------------------------------------------
8992

93+
# Retrieve plugin instance for bound method or nothing
94+
def _get_plugin(self, method: Callable):
95+
return getattr(method, "__self__", None)
96+
9097
# Retrieve priority of plugin method
9198
def _get_priority(self, method: Callable):
9299
return getattr(method, "mkdocs_priority", 0)
@@ -117,17 +124,23 @@ def _patch(self, methods: list[Callable], config: MkDocsConfig):
117124
head = methods[at]
118125

119126
# Skip if the plugin is not part of the group
120-
if not head.__self__ in self.plugins.values():
127+
plugin = self._get_plugin(head)
128+
if not plugin or plugin not in self.plugins.values():
121129
continue
122130

123131
# Skip if the previous method has a higher priority than the current
124132
# one, because we know we can't swap them anyway
125133
if self._get_priority(tail) > self._get_priority(head):
126134
continue
127135

136+
# Ensure that we have a method bound to a plugin (and not a hook)
137+
plugin = self._get_plugin(tail)
138+
if not plugin:
139+
continue
140+
128141
# Both methods have the same priority, so we check if the ordering
129142
# of both methods is violated, and if it is, swap them
130-
if (position < self._get_position(tail.__self__, config)):
143+
if (position < self._get_position(plugin, config)):
131144
methods[at], methods[at - 1] = tail, head
132145

133146
# -----------------------------------------------------------------------------

0 commit comments

Comments
 (0)