From 0857a45b0a83d0379c207a7f6bc5a2b5bccfa7de Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 15 Jun 2017 14:53:31 +0200 Subject: [PATCH] configtypes: parse regex flags properly --- qutebrowser/config/configtypes.py | 10 +++++++++- tests/unit/config/test_configtypes.py | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index 632f2311c..0ce5aadd1 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -27,6 +27,8 @@ import itertools import collections import warnings import datetime +import functools +import operator import yaml from PyQt5.QtCore import QUrl, Qt @@ -862,8 +864,14 @@ class Regex(BaseType): def __init__(self, flags=0, none_ok=False): super().__init__(none_ok) - self.flags = flags self._regex_type = type(re.compile('')) + # Parse flags from configdata.yml + if flags == 0: + self.flags = flags + else: + self.flags = functools.reduce( + operator.or_, + (getattr(re, flag.strip()) for flag in flags.split(' | '))) def _compile_regex(self, pattern): """Check if the given regex is valid. diff --git a/tests/unit/config/test_configtypes.py b/tests/unit/config/test_configtypes.py index c15f2e09d..bc1af3b9c 100644 --- a/tests/unit/config/test_configtypes.py +++ b/tests/unit/config/test_configtypes.py @@ -1239,6 +1239,15 @@ class TestRegex: with pytest.raises(configexc.ValidationError): regex.from_py('foo') + @pytest.mark.parametrize('flags, expected', [ + (0, 0), + ('IGNORECASE', re.IGNORECASE), + ('IGNORECASE | VERBOSE', re.IGNORECASE | re.VERBOSE), + ]) + def test_flag_parsing(self, klass, flags, expected): + typ = klass(flags=flags) + assert typ.flags == expected + class TestDict: