diff options
-rw-r--r-- | mitmproxy/command.py | 11 | ||||
-rw-r--r-- | test/mitmproxy/test_command.py | 14 |
2 files changed, 22 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..ffbb20af 100644 --- a/test/mitmproxy/test_command.py +++ b/test/mitmproxy/test_command.py @@ -1,4 +1,5 @@ import typing +from unittest import mock from mitmproxy import command from mitmproxy import flow from mitmproxy import exceptions @@ -309,6 +310,19 @@ class TDec: pass +def test_collect_commands(): + """ + This tests for the error thrown by hasattr() + """ + with mock.patch("mitmproxy.command.hasattr") as mock_hasattr: + mock_hasattr.return_value = False + with taddons.context() as tctx: + mock_hasattr.side_effect = OSError + c = command.CommandManager(tctx.master) + a = TDec() + c.collect_commands(a) + + def test_decorator(): with taddons.context() as tctx: c = command.CommandManager(tctx.master) |