Skip to content

Commit

Permalink
Make pluggy more resilient when registering plugins with bad attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Sep 28, 2015
1 parent 2c30390 commit 5a75b1e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
10 changes: 8 additions & 2 deletions pluggy.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,10 @@ def register(self, plugin, name=None):

def parse_hookimpl_opts(self, plugin, name):
method = getattr(plugin, name)
res = getattr(method, self.project_name + "_impl", None)
try:
res = getattr(method, self.project_name + "_impl", None)
except Exception:
res = {}
if res is not None and not isinstance(res, dict):
# false positive
res = None
Expand Down Expand Up @@ -640,7 +643,10 @@ def varnames(func, startindex=None):
startindex = 1
else:
if not inspect.isfunction(func) and not inspect.ismethod(func):
func = getattr(func, '__call__', func)
try:
func = getattr(func, '__call__', func)
except Exception:
return ()
if startindex is None:
startindex = int(inspect.ismethod(func))

Expand Down
20 changes: 20 additions & 0 deletions testing/test_pluggy.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,26 @@ def x1meth2(self):
assert pm.hook.x1meth2._wrappers[0].hookwrapper


def test_plugin_getattr_raises_errors():
"""Pluggy must be able to handle plugins which raise weird exceptions
when getattr() gets called (#11).
"""
class DontTouchMe:
def __getattr__(self, x):
raise Exception('cant touch me')

class Module:
pass

module = Module()
module.x = DontTouchMe()

pm = PluginManager(hookspec.project_name)
# register() would raise an error
pm.register(module, 'donttouch')
assert pm.get_plugin('donttouch') is module


def test_varnames():
def f(x):
i = 3 # noqa
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ minversion=2.0
#--pyargs --doctest-modules --ignore=.tox
addopts= -rxsX
pep8ignore = E501 E128 E127
norecursedirs = .tox ja .hg
norecursedirs = .tox ja .hg .env*

0 comments on commit 5a75b1e

Please sign in to comment.