Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion dol/tests/test_trans.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
"""Test trans.py functionality."""

from dol.trans import filt_iter, redirect_getattr_to_getitem
import dol.util
from dol.trans import (
filt_iter,
filter_prefixes,
filter_regex,
filter_suffixes,
redirect_getattr_to_getitem,
)


def test_filter_regex_is_os_independent():
"""Regex filters must compile as REGEXES, not as path templates.

Regression for a Windows-only bug: ``filter_regex`` used the path-oriented
``safe_compile``, which ``re.escape``s its input on Windows. That turned a
pattern like ``(\\.json)$`` into a literal matcher, so ``filter_suffixes('.json')``
rejected every ``*.json`` key on Windows and ``dol.Jsons`` raised
``KeyError: 'Key not in store: <key>.json'`` on write.
"""
real_system = dol.util.platform.system
try:
# Simulate every platform, including Windows (the broken one).
for system in ("Linux", "Darwin", "Windows"):
dol.util.platform.system = lambda system=system: system
assert bool(filter_regex(r"(\.json)$")("doc-001.json")) is True
assert bool(filter_regex(r"(\.json)$")("doc-001.txt")) is False
assert bool(filter_suffixes(".json")("doc-001.json")) is True
assert bool(filter_suffixes([".txt", ".doc"])("report.doc")) is True
assert bool(filter_suffixes(".json")("doc-001.txt")) is False
assert bool(filter_prefixes("test")("test_image.jpg")) is True
assert bool(filter_prefixes("test")("report.doc")) is False
finally:
dol.util.platform.system = real_system


def test_filt_iter():
Expand Down
14 changes: 13 additions & 1 deletion dol/trans.py
Original file line number Diff line number Diff line change
Expand Up @@ -1481,9 +1481,21 @@ def filter_regex(regex, *, return_search_func=False):
>>> is_txt("report.doc")
False

The argument is a *regular expression*, so it is compiled with ``re.compile``
-- NOT ``safe_compile`` (which is for file-path templates and ``re.escape``s its
input on Windows). Using ``safe_compile`` here silently broke every regex filter
on Windows: e.g. ``filter_suffixes('.json')`` (used by ``Jsons``) had its
``(\.json)$`` pattern escaped into a literal string matcher, so no ``*.json`` key
matched and the store raised ``KeyError: 'Key not in store: <key>.json'``.

>>> is_json = filter_regex(r"(\.json)$") # works identically on every OS
>>> is_json("doc-001.json")
True
>>> is_json("doc-001.txt")
False
"""
if isinstance(regex, str):
regex = safe_compile(regex)
regex = re.compile(regex)
if return_search_func:
return regex.search
else:
Expand Down
Loading