Skip to content

Commit

Permalink
multicall: hookwrapper: use yielded results, support firstresult=True
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Jan 12, 2020
1 parent 1a341b6 commit 70f4b1c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/pluggy/callers.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,12 @@ def _multicall(hook_impls, caller_kwargs, firstresult=False):
if hook_impl.hookwrapper:
try:
gen = hook_impl.function(*args)
next(gen) # first yield
res = next(gen) # first yield
teardowns.append(gen)
if res is not None:
results.append(res)
if firstresult:
break
except StopIteration:
_raise_wrapfail(gen, "did not yield")
else:
Expand Down
25 changes: 24 additions & 1 deletion testing/test_multicall.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def m2():
out.append("m2 finish")

res = MC([m2, m1], {})
assert res == []
assert res == [1, 2]
assert out == ["m1 init", "m2 init", "m2 finish", "m1 finish"]


Expand Down Expand Up @@ -184,3 +184,26 @@ def m2():
with pytest.raises(exc):
MC([m2, m1], {})
assert out == ["m1 init", "m1 finish"]


def test_hookwrapper_result():
out = []

@hookimpl(hookwrapper=True)
def m1():
out.append("m1 init")
yield "hookwrapper_result"
out.append("m1 finish")

@hookimpl
def m2():
out.append("m2")
return 2

res = MC([m2, m1], {})
assert res == ["hookwrapper_result", 2]
assert out == ["m1 init", "m2", "m1 finish"]
out[:] = []
res = MC([m2, m1], {}, firstresult=True)
assert res == "hookwrapper_result"
assert out == ["m1 init", "m1 finish"]

0 comments on commit 70f4b1c

Please sign in to comment.