Skip to content

Commit f8b0f34

Browse files
committed
fix issue #4: raise specific HookCallError exception when arguments mismatch
1 parent 60d81f2 commit f8b0f34

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
0.3.2
2+
-----
3+
4+
- fix issue #4: specific HookCallError exception for when a hook call
5+
provides not enough arguments.
6+
17
0.3.1
28
-----
39

pluggy.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@
6767
import sys
6868
import inspect
6969

70-
__version__ = '0.3.1'
71-
__all__ = ["PluginManager", "PluginValidationError",
70+
__version__ = '0.3.2'
71+
72+
__all__ = ["PluginManager", "PluginValidationError", "HookCallError",
7273
"HookspecMarker", "HookimplMarker"]
7374

7475
_py3 = sys.version_info > (3, 0)
@@ -590,7 +591,13 @@ def execute(self):
590591

591592
while self.hook_impls:
592593
hook_impl = self.hook_impls.pop()
593-
args = [all_kwargs[argname] for argname in hook_impl.argnames]
594+
try:
595+
args = [all_kwargs[argname] for argname in hook_impl.argnames]
596+
except KeyError:
597+
for argname in hook_impl.argnames:
598+
if argname not in all_kwargs:
599+
raise HookCallError(
600+
"hook call must provide argument %r" % (argname,))
594601
if hook_impl.hookwrapper:
595602
return _wrapped_call(hook_impl.function(*args), self.execute)
596603
res = hook_impl.function(*args)
@@ -763,6 +770,10 @@ class PluginValidationError(Exception):
763770
""" plugin failed validation. """
764771

765772

773+
class HookCallError(Exception):
774+
""" Hook was called wrongly. """
775+
776+
766777
if hasattr(inspect, 'signature'):
767778
def _formatdef(func):
768779
return "%s%s" % (

testing/test_pluggy.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55

66
from pluggy import (PluginManager, varnames, PluginValidationError,
7-
HookimplMarker, HookspecMarker)
7+
HookCallError, HookimplMarker, HookspecMarker)
88

99
from pluggy import (_MultiCall, _TagTracer, HookImpl, _formatdef)
1010

@@ -235,6 +235,22 @@ def he_method1(arg):
235235
l = pm.hook.he_method1.call_extra([he_method1], dict(arg=1))
236236
assert l == [10]
237237

238+
def test_call_with_too_few_args(self, pm):
239+
class Hooks:
240+
@hookspec
241+
def he_method1(self, arg):
242+
pass
243+
244+
pm.add_hookspecs(Hooks)
245+
246+
class Plugin1:
247+
@hookimpl
248+
def he_method1(self, arg):
249+
0 / 0
250+
pm.register(Plugin1())
251+
with pytest.raises(HookCallError):
252+
pm.hook.he_method1()
253+
238254
def test_subset_hook_caller(self, pm):
239255
class Hooks:
240256
@hookspec
@@ -756,7 +772,7 @@ def test_tags_call_error(self):
756772
def f(x):
757773
return x
758774
multicall = self.MC([f], {})
759-
pytest.raises(KeyError, multicall.execute)
775+
pytest.raises(HookCallError, multicall.execute)
760776

761777
def test_call_subexecute(self):
762778
@hookimpl
@@ -868,7 +884,7 @@ def m2():
868884

869885

870886
class TestHookRelay:
871-
def test_hapmypath(self, pm):
887+
def test_happypath(self, pm):
872888
class Api:
873889
@hookspec
874890
def hello(self, arg):

0 commit comments

Comments
 (0)