From 411ae5751ba0a2965d9f4ca79a92c5ef48abe804 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 11 Dec 2021 11:37:33 +0100 Subject: [PATCH] Fix uncaught exception on empty files --- codespell_lib/_codespell.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index 86cc2c07b19..a84ddbd357b 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -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 # -.-:-.-:-.-:-.:-.-:-.-:-.-:-.-:-.:-.-:-.-:-.-:-.-:-.:-.-:-