Skip to content

Commit 5e40b73

Browse files
committed
Merge pull request #12 from nicoddemus/discovery-bug
Make pluggy more resilient when registering plugins with bad attributes
2 parents f56164f + 5a75b1e commit 5e40b73

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

pluggy.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,10 @@ def register(self, plugin, name=None):
375375

376376
def parse_hookimpl_opts(self, plugin, name):
377377
method = getattr(plugin, name)
378-
res = getattr(method, self.project_name + "_impl", None)
378+
try:
379+
res = getattr(method, self.project_name + "_impl", None)
380+
except Exception:
381+
res = {}
379382
if res is not None and not isinstance(res, dict):
380383
# false positive
381384
res = None
@@ -640,7 +643,10 @@ def varnames(func, startindex=None):
640643
startindex = 1
641644
else:
642645
if not inspect.isfunction(func) and not inspect.ismethod(func):
643-
func = getattr(func, '__call__', func)
646+
try:
647+
func = getattr(func, '__call__', func)
648+
except Exception:
649+
return ()
644650
if startindex is None:
645651
startindex = int(inspect.ismethod(func))
646652

testing/test_pluggy.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,26 @@ def x1meth2(self):
645645
assert pm.hook.x1meth2._wrappers[0].hookwrapper
646646

647647

648+
def test_plugin_getattr_raises_errors():
649+
"""Pluggy must be able to handle plugins which raise weird exceptions
650+
when getattr() gets called (#11).
651+
"""
652+
class DontTouchMe:
653+
def __getattr__(self, x):
654+
raise Exception('cant touch me')
655+
656+
class Module:
657+
pass
658+
659+
module = Module()
660+
module.x = DontTouchMe()
661+
662+
pm = PluginManager(hookspec.project_name)
663+
# register() would raise an error
664+
pm.register(module, 'donttouch')
665+
assert pm.get_plugin('donttouch') is module
666+
667+
648668
def test_varnames():
649669
def f(x):
650670
i = 3 # noqa

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ minversion=2.0
2121
#--pyargs --doctest-modules --ignore=.tox
2222
addopts= -rxsX
2323
pep8ignore = E501 E128 E127
24-
norecursedirs = .tox ja .hg
24+
norecursedirs = .tox ja .hg .env*

0 commit comments

Comments
 (0)