From 065f82f485112dfa1495207d713c524647178003 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sat, 1 Jul 2017 22:08:50 +0200 Subject: [PATCH] Fix endless recursion while validating aliases --- qutebrowser/config/configtypes.py | 28 ++++++++++++++++++++------- tests/unit/config/test_configtypes.py | 1 + 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index c59283fd6..a39da0979 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -726,18 +726,32 @@ class Command(BaseType): """Base class for a command value with arguments.""" + # See to_py for details + unvalidated = False + def to_py(self, value): self._basic_py_validation(value, str) if not value: return None - # FIXME:conf is it okay to import runners.py here? - from qutebrowser.commands import runners, cmdexc - parser = runners.CommandParser() - try: - list(parser.parse_all(value)) - except cmdexc.Error as e: - raise configexc.ValidationError(value, str(e)) + # This requires some trickery, as runners.CommandParser uses + # conf.val.aliases, which in turn map to a command again, + # leading to an endless recursion. + # To fix that, we turn off validating other commands (alias values) + # while validating a command. + # FIXME:conf Can't test this because we don't have a real config in + # TestCommand + if not Command.unvalidated: # pragma: no branch + Command.unvalidated = True + try: + from qutebrowser.commands import runners, cmdexc + parser = runners.CommandParser() + try: + list(parser.parse_all(value)) + except cmdexc.Error as e: + raise configexc.ValidationError(value, str(e)) + finally: + Command.unvalidated = False return value diff --git a/tests/unit/config/test_configtypes.py b/tests/unit/config/test_configtypes.py index 2e4958095..dcb7733e2 100644 --- a/tests/unit/config/test_configtypes.py +++ b/tests/unit/config/test_configtypes.py @@ -1035,6 +1035,7 @@ class TestCommand: 'cmd2': stubs.FakeCommand(desc="desc 2")}) monkeypatch.setattr(configtypes, 'cmdutils', cmd_utils) # FIXME:conf use some kind of config_stub here + # also remove the no branch pragma from configtypes.Command then ns = types.SimpleNamespace() ns.aliases = {'alias': 'cmd1'} monkeypatch.setattr('qutebrowser.config.config.val', ns)