Skip to content

Commit 411ae57

Browse files
Fix uncaught exception on empty files
1 parent d8948ad commit 411ae57

1 file changed

Lines changed: 10 additions & 14 deletions

File tree

codespell_lib/_codespell.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,30 +200,26 @@ def open_with_chardet(self, filename):
200200
return lines, encoding
201201

202202
def open_with_internal(self, filename):
203-
curr = 0
204-
while True:
203+
first_try = True
204+
for encoding in encodings:
205+
if first_try:
206+
first_try = False
207+
else:
208+
print("WARNING: Trying next encoding %s"
209+
% encoding, file=sys.stderr)
205210
try:
206-
f = codecs.open(filename, 'r', encoding=encodings[curr])
211+
f = codecs.open(filename, 'r', encoding=encoding)
207212
except UnicodeDecodeError:
208213
if not self.quiet_level & QuietLevels.ENCODING:
209214
print("WARNING: Decoding file using encoding=%s failed: %s"
210-
% (encodings[curr], filename,), file=sys.stderr)
211-
try:
212-
print("WARNING: Trying next encoding %s"
213-
% encodings[curr + 1], file=sys.stderr)
214-
except IndexError:
215-
pass
216-
217-
curr += 1
215+
% (encoding, filename,), file=sys.stderr)
218216
else:
219217
lines = f.readlines()
220218
f.close()
221219
break
222-
if not lines:
220+
else:
223221
raise Exception('Unknown encoding')
224222

225-
encoding = encodings[curr]
226-
227223
return lines, encoding
228224

229225
# -.-:-.-:-.-:-.:-.-:-.-:-.-:-.-:-.:-.-:-.-:-.-:-.-:-.:-.-:-

0 commit comments

Comments
 (0)