Skip to content

Commit

Permalink
Made sure to always decode data if needed. Fixes #69
Browse files Browse the repository at this point in the history
  • Loading branch information
wolph committed Feb 14, 2019
1 parent c69ba92 commit a9195c9
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions mt940/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ def safe_is_file(filename):
if hasattr(src, 'read'): # pragma: no branch
data = src.read()
elif safe_is_file(src):
return parse(open(src, 'rb').read())
elif hasattr(src, 'decode'):
with open(src, 'rb') as fh:
data = fh.read()
else: # pragma: no cover
data = src

if hasattr(data, 'decode'):
exception = None
encodings = [encoding, 'utf-8', 'cp852', 'iso8859-15', 'latin1']

Expand All @@ -59,14 +63,12 @@ def safe_is_file(filename):
continue

try:
data = src.decode(encoding)
data = data.decode(encoding)
break
except UnicodeDecodeError as e:
exception = e
else: # pragma: no cover
raise exception
else: # pragma: no cover
data = src

transactions = mt940.models.Transactions()
transactions.parse(data)
Expand Down

0 comments on commit a9195c9

Please sign in to comment.