Skip to content

Commit

Permalink
Fix uncaught exception on empty files
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriPapadopoulos committed Dec 11, 2021
1 parent d8948ad commit 411ae57
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,30 +200,26 @@ def open_with_chardet(self, filename):
return lines, encoding

def open_with_internal(self, filename):
curr = 0
while True:
first_try = True
for encoding in encodings:
if first_try:
first_try = False
else:
print("WARNING: Trying next encoding %s"
% encoding, file=sys.stderr)
try:
f = codecs.open(filename, 'r', encoding=encodings[curr])
f = codecs.open(filename, 'r', encoding=encoding)
except UnicodeDecodeError:
if not self.quiet_level & QuietLevels.ENCODING:
print("WARNING: Decoding file using encoding=%s failed: %s"
% (encodings[curr], filename,), file=sys.stderr)
try:
print("WARNING: Trying next encoding %s"
% encodings[curr + 1], file=sys.stderr)
except IndexError:
pass

curr += 1
% (encoding, filename,), file=sys.stderr)
else:
lines = f.readlines()
f.close()
break
if not lines:
else:
raise Exception('Unknown encoding')

encoding = encodings[curr]

return lines, encoding

# -.-:-.-:-.-:-.:-.-:-.-:-.-:-.-:-.:-.-:-.-:-.-:-.-:-.:-.-:-
Expand Down

0 comments on commit 411ae57

Please sign in to comment.