Commit c99a2dd7 authored by Kai Willadsen's avatar Kai Willadsen

filters: Break out forgiving regex compilation and add a test

parent f2b9731a
......@@ -13,10 +13,23 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <>.
import logging
import re
from . import misc
log = logging.getLogger(__name__)
def try_compile(regex, flags=0):
compiled = re.compile(regex, flags)
except re.error:
'Error compiling regex {!r} with flags {!r}'.format(regex, flags))
compiled = None
return compiled
class FilterEntry:
......@@ -37,12 +50,7 @@ class FilterEntry:
# TODO: Register a custom error handling function to replace
# encoding errors with '.'?
regex = regex.encode('utf8', 'replace')
compiled = re.compile(regex, re.M)
except re.error:
compiled = None
return compiled
return try_compile(regex, re.M)
def _compile_shell_pattern(cls, pattern):
......@@ -55,13 +63,7 @@ class FilterEntry:
regex = "(%s)$" % "|".join(regexes)
regex = misc.shell_to_regex(bits[0])
compiled = re.compile(regex)
except re.error:
compiled = None
return compiled
return try_compile(regex)
def new_from_gsetting(cls, elements, filter_type):
......@@ -26,3 +26,15 @@ def test_file_filters(patterns, filename, expected_match):
# that's what we test here, even if it looks a bit weird.
match = any(f.filter.match(filename) for f in filters)
assert match == expected_match
@pytest.mark.parametrize("pattern", [
r'*.foo*', # Trailing wildcard
r'\xfoo', # Invalid escape
def test_bad_regex_compilation(pattern):
from meld.filters import FilterEntry
f = FilterEntry.new_from_gsetting(
("name", True, pattern), FilterEntry.REGEX)
assert f.filter is None
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment