Skip to content

Commit

Permalink
fix issue #4: raise specific HookCallError exception when arguments m…
Browse files Browse the repository at this point in the history
…ismatch
  • Loading branch information
hpk42 committed Sep 22, 2015
1 parent 60d81f2 commit f8b0f34
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
0.3.2
-----

- fix issue #4: specific HookCallError exception for when a hook call
provides not enough arguments.

0.3.1
-----

Expand Down
17 changes: 14 additions & 3 deletions pluggy.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@
import sys
import inspect

__version__ = '0.3.1'
__all__ = ["PluginManager", "PluginValidationError",
__version__ = '0.3.2'

__all__ = ["PluginManager", "PluginValidationError", "HookCallError",
"HookspecMarker", "HookimplMarker"]

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

while self.hook_impls:
hook_impl = self.hook_impls.pop()
args = [all_kwargs[argname] for argname in hook_impl.argnames]
try:
args = [all_kwargs[argname] for argname in hook_impl.argnames]
except KeyError:
for argname in hook_impl.argnames:
if argname not in all_kwargs:
raise HookCallError(
"hook call must provide argument %r" % (argname,))
if hook_impl.hookwrapper:
return _wrapped_call(hook_impl.function(*args), self.execute)
res = hook_impl.function(*args)
Expand Down Expand Up @@ -763,6 +770,10 @@ class PluginValidationError(Exception):
""" plugin failed validation. """


class HookCallError(Exception):
""" Hook was called wrongly. """


if hasattr(inspect, 'signature'):
def _formatdef(func):
return "%s%s" % (
Expand Down
22 changes: 19 additions & 3 deletions testing/test_pluggy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest

from pluggy import (PluginManager, varnames, PluginValidationError,
HookimplMarker, HookspecMarker)
HookCallError, HookimplMarker, HookspecMarker)

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

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

def test_call_with_too_few_args(self, pm):
class Hooks:
@hookspec
def he_method1(self, arg):
pass

pm.add_hookspecs(Hooks)

class Plugin1:
@hookimpl
def he_method1(self, arg):
0 / 0
pm.register(Plugin1())
with pytest.raises(HookCallError):
pm.hook.he_method1()

def test_subset_hook_caller(self, pm):
class Hooks:
@hookspec
Expand Down Expand Up @@ -756,7 +772,7 @@ def test_tags_call_error(self):
def f(x):
return x
multicall = self.MC([f], {})
pytest.raises(KeyError, multicall.execute)
pytest.raises(HookCallError, multicall.execute)

def test_call_subexecute(self):
@hookimpl
Expand Down Expand Up @@ -868,7 +884,7 @@ def m2():


class TestHookRelay:
def test_hapmypath(self, pm):
def test_happypath(self, pm):
class Api:
@hookspec
def hello(self, arg):
Expand Down

0 comments on commit f8b0f34

Please sign in to comment.