aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2018-03-15 00:29:52 +0100
committerGitHub <noreply@github.com>2018-03-15 00:29:52 +0100
commit2001184b6c373a2a0ea86a9379f8decf3f7912a5 (patch)
tree717c9c054e86f87a66dd0e672a3e040b6cdde923
parent0bc3f1fbf16ab49bd2025b8524d9222d879e7af5 (diff)
parent8aad2d63cfaf30204464fcfee4ecf6f159a8f731 (diff)
downloadmitmproxy-2001184b6c373a2a0ea86a9379f8decf3f7912a5.tar.gz
mitmproxy-2001184b6c373a2a0ea86a9379f8decf3f7912a5.tar.bz2
mitmproxy-2001184b6c373a2a0ea86a9379f8decf3f7912a5.zip
Merge pull request #2956 from kira0204/test-for-2850
Test for #2850
-rw-r--r--mitmproxy/command.py11
-rw-r--r--test/mitmproxy/test_command.py25
2 files changed, 33 insertions, 3 deletions
diff --git a/mitmproxy/command.py b/mitmproxy/command.py
index 45141576..114e882d 100644
--- a/mitmproxy/command.py
+++ b/mitmproxy/command.py
@@ -1,5 +1,5 @@
"""
- This module manges and invokes typed commands.
+ This module manages and invokes typed commands.
"""
import inspect
import types
@@ -131,8 +131,13 @@ class CommandManager(mitmproxy.types._CommandBase):
for i in dir(addon):
if not i.startswith("__"):
o = getattr(addon, i)
- if hasattr(o, "command_path"):
- self.add(o.command_path, o)
+ try:
+ is_command = hasattr(o, "command_path")
+ except Exception:
+ pass # hasattr may raise if o implements __getattr__.
+ else:
+ if is_command:
+ self.add(o.command_path, o)
def add(self, path: str, func: typing.Callable):
self.commands[path] = Command(self, path, func)
diff --git a/test/mitmproxy/test_command.py b/test/mitmproxy/test_command.py
index e2b80753..3d0a43f8 100644
--- a/test/mitmproxy/test_command.py
+++ b/test/mitmproxy/test_command.py
@@ -309,6 +309,31 @@ class TDec:
pass
+class TAttr:
+ def __getattr__(self, item):
+ raise IOError
+
+
+class TCmds(TAttr):
+ def __init__(self):
+ self.TAttr = TAttr()
+
+ @command.command("empty")
+ def empty(self) -> None:
+ pass
+
+
+def test_collect_commands():
+ """
+ This tests for the error thrown by hasattr()
+ """
+ with taddons.context() as tctx:
+ c = command.CommandManager(tctx.master)
+ a = TCmds()
+ c.collect_commands(a)
+ assert "empty" in c.commands
+
+
def test_decorator():
with taddons.context() as tctx:
c = command.CommandManager(tctx.master)