Fix endless recursion while validating aliases

This commit is contained in:
Florian Bruhin 2017-07-01 22:08:50 +02:00
parent ac78039171
commit 065f82f485
2 changed files with 22 additions and 7 deletions

View File

@ -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

View File

@ -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)