Merge pull request #2483 from mhils/ignore-imports-with-hook-names

Gracefully ignore imports with hook names
This commit is contained in:
Maximilian Hils 2017-07-30 02:04:36 +02:00 committed by GitHub
commit ecc01b0f02
2 changed files with 16 additions and 3 deletions

View File

@ -1,3 +1,4 @@
import types
import typing
import traceback
import contextlib
@ -229,11 +230,18 @@ class AddonManager:
for a in traverse([addon]):
func = getattr(a, name, None)
if func:
if not callable(func):
if callable(func):
func(*args, **kwargs)
elif isinstance(func, types.ModuleType):
# we gracefully exclude module imports with the same name as hooks.
# For example, a user may have "from mitmproxy import log" in an addon,
# which has the same name as the "log" hook. In this particular case,
# we end up in an error loop because we "log" this error.
pass
else:
raise exceptions.AddonManagerError(
"Addon handler %s not callable" % name
"Addon handler {} ({}) not callable".format(name, a)
)
func(*args, **kwargs)
def trigger(self, name, *args, **kwargs):
"""

View File

@ -117,6 +117,11 @@ def test_simple():
a.trigger("tick")
tctx.master.has_log("not callable")
tctx.master.clear()
a.get("one").tick = addons
a.trigger("tick")
assert not tctx.master.has_log("not callable")
a.remove(a.get("one"))
assert not a.get("one")