Skip to content

Commit e7baa82

Browse files
committed
Improve historic hook callbacks
Historic callbacks always immediately invoke all registered hooks, but the callback is **only** triggered on hooks that come from newly registered plugins. This change captures all information. Added a test to specifically make sure this works correctly.
1 parent ded4c2e commit e7baa82

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

pluggy.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,9 @@ def __call__(self, **kwargs):
747747
def call_historic(self, proc=None, kwargs=None):
748748
self._call_history.append((kwargs or {}, proc))
749749
# historizing hooks don't return results
750-
self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
750+
res = self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
751+
if res and proc is not None:
752+
proc(res[0])
751753

752754
def call_extra(self, methods, kwargs):
753755
""" Call the hook with some additional temporarily participating

testing/test_pluggy.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,25 @@ def he_method1(self, arg):
212212
pm.register(Plugin())
213213
assert l == [10]
214214

215+
def test_with_immediate_result_memorized(self, pm):
216+
class Hooks:
217+
@hookspec(historic=True)
218+
def he_method1(self, arg):
219+
pass
220+
pm.add_hookspecs(Hooks)
221+
222+
class Plugin:
223+
@hookimpl
224+
def he_method1(self, arg):
225+
return arg * 10
226+
227+
l = []
228+
pm.register(Plugin())
229+
230+
he_method1 = pm.hook.he_method1
231+
he_method1.call_historic(lambda res: l.append(res), dict(arg=1))
232+
assert l == [10]
233+
215234
def test_register_historic_incompat_hookwrapper(self, pm):
216235
class Hooks:
217236
@hookspec(historic=True)

0 commit comments

Comments
 (0)