From 70f4b1c1cb61da524931f3c070724f82a07e8898 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 12 Jan 2020 03:59:12 +0100 Subject: [PATCH 1/4] multicall: hookwrapper: use yielded results, support firstresult=True Ref: https://github.com/pytest-dev/pytest/issues/5301#issuecomment-573363212 --- src/pluggy/callers.py | 6 +++++- testing/test_multicall.py | 25 ++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/pluggy/callers.py b/src/pluggy/callers.py index e7ea464b..3243dd8b 100644 --- a/src/pluggy/callers.py +++ b/src/pluggy/callers.py @@ -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: diff --git a/testing/test_multicall.py b/testing/test_multicall.py index eaf534a0..bf7fd6ea 100644 --- a/testing/test_multicall.py +++ b/testing/test_multicall.py @@ -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"] @@ -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"] From 18fd6bc818d41c5885c50e032e57225ef5370819 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 12 Jan 2020 04:09:19 +0100 Subject: [PATCH 2/4] fixup! multicall: hookwrapper: use yielded results, support firstresult=True --- testing/test_multicall.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/testing/test_multicall.py b/testing/test_multicall.py index bf7fd6ea..3c8b551b 100644 --- a/testing/test_multicall.py +++ b/testing/test_multicall.py @@ -186,7 +186,7 @@ def m2(): assert out == ["m1 init", "m1 finish"] -def test_hookwrapper_result(): +def test_hookwrapper_firstresult(): out = [] @hookimpl(hookwrapper=True) @@ -200,10 +200,6 @@ 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"] From a59821d0a4f8498bb579254263f8d7dcb936a612 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 12 Jan 2020 04:24:54 +0100 Subject: [PATCH 3/4] .coveragerc: add report section --- .coveragerc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.coveragerc b/.coveragerc index 1b1de1cd..f16624af 100644 --- a/.coveragerc +++ b/.coveragerc @@ -12,3 +12,11 @@ source = pluggy/ */lib/python*/site-packages/pluggy/ */pypy*/site-packages/pluggy/ *\Lib\site-packages\pluggy\ + +[report] +skip_covered = True +show_missing = True +exclude_lines = + \#\s*pragma: no cover + ^\s*raise NotImplementedError\b + ^\s*return NotImplemented\b From 76838b1f75458d3f2678f20b3d9c3f0f72bd6379 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 12 Jan 2020 04:25:00 +0100 Subject: [PATCH 4/4] fixup! fixup! multicall: hookwrapper: use yielded results, support firstresult=True --- testing/test_multicall.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/testing/test_multicall.py b/testing/test_multicall.py index 3c8b551b..bfe4fa51 100644 --- a/testing/test_multicall.py +++ b/testing/test_multicall.py @@ -197,8 +197,7 @@ def m1(): @hookimpl def m2(): - out.append("m2") - return 2 + raise NotImplementedError() res = MC([m2, m1], {}, firstresult=True) assert res == "hookwrapper_result"