diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index c15de3d0f..4ad5037f4 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -46,6 +46,12 @@ BOOLEAN_STATES = {'1': True, 'yes': True, 'true': True, 'on': True, def _validate_regex(pattern, flags): + """Check if the given regex is valid. + + This is more complicated than it could be since there's a warning on + invalid escapes with newer Python versions, and we want to catch that case + and treat it as invalid. + """ with warnings.catch_warnings(record=True) as recorded_warnings: warnings.simplefilter('always') try: diff --git a/tests/unit/config/test_configtypes.py b/tests/unit/config/test_configtypes.py index a6d328ea4..2b3b44485 100644 --- a/tests/unit/config/test_configtypes.py +++ b/tests/unit/config/test_configtypes.py @@ -1092,12 +1092,29 @@ class TestRegex: Warning('foo'), DeprecationWarning('foo'), ]) def test_passed_warnings(self, mocker, klass, warning): + """Simulate re.compile showing a warning we don't know about yet. + + The warning should be passed. + """ m = mocker.patch('qutebrowser.config.configtypes.re') m.compile.side_effect = lambda *args: warnings.warn(warning) m.error = re.error with pytest.raises(type(warning)): klass().validate('foo') + def test_bad_pattern_warning(self, mocker, klass): + """Test a simulated bad pattern warning. + + This only seems to happen with Python 3.5, so we simulate this for + better coverage. + """ + m = mocker.patch('qutebrowser.config.configtypes.re') + m.compile.side_effect = lambda *args: warnings.warn(r'bad escape \C', + DeprecationWarning) + m.error = re.error + with pytest.raises(configexc.ValidationError): + klass().validate('foo') + class TestRegexList: