From 5a75b1e5b63e9fb7d58b276f6c1c493ee9b27829 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sun, 27 Sep 2015 22:22:19 -0300 Subject: [PATCH] Make pluggy more resilient when registering plugins with bad attributes Fix #11 --- pluggy.py | 10 ++++++++-- testing/test_pluggy.py | 20 ++++++++++++++++++++ tox.ini | 2 +- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/pluggy.py b/pluggy.py index 7ed329db..ac3a1cad 100644 --- a/pluggy.py +++ b/pluggy.py @@ -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 @@ -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)) diff --git a/testing/test_pluggy.py b/testing/test_pluggy.py index f07d4177..ae91c056 100644 --- a/testing/test_pluggy.py +++ b/testing/test_pluggy.py @@ -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 diff --git a/tox.ini b/tox.ini index 5a704b55..fb3be065 100644 --- a/tox.ini +++ b/tox.ini @@ -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*