Skip to content

Commit

Permalink
Don't raise on invalid encoding in search_file_regexp(), just don't m…
Browse files Browse the repository at this point in the history
…atch (and issue a warning).
  • Loading branch information
blais committed Oct 6, 2024
1 parent ec37529 commit 8c1173f
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions beangulp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import datetime
import decimal
import hashlib
import logging
import os
import re

Expand Down Expand Up @@ -76,8 +77,14 @@ def search_file_regexp(filepath: str, *regexps: str,
"""Check if the header of the file matches the given regexp."""
with open(filepath, encoding=encoding) as infile:
# Note: Don't convert just to match on the contents.
contents = infile.read(nbytes)
return any(re.search(regexp, contents) for regexp in regexps)
try:
contents = infile.read(nbytes)
except UnicodeDecodeError as exc:
# The encoding wasn't right, don't match.
logging.warning(f"Error searching for regexp in '{filepath}': {exc}")
return False
else:
return any(re.search(regexp, contents) for regexp in regexps)


def parse_amount(string: str)-> decimal.Decimal:
Expand Down

0 comments on commit 8c1173f

Please sign in to comment.