wmllint and wmlindent: avoid crashing on non-UTF8 files

Fixes #2091
This commit is contained in:
Elvish_Hunter 2022-02-16 10:59:05 +01:00
parent cde990a9a0
commit 528ea56424
2 changed files with 9 additions and 2 deletions

View file

@ -274,6 +274,9 @@ def convertor(linefilter, arglist, exclude):
except bailout as e:
print('wmlindent: "%s", %d: %s' % (e.filename, e.lineno, e.msg), file=sys.stderr)
os.remove(filename + ".out")
except UnicodeDecodeError as e:
os.remove(filename + ".out")
print('wmlindent: "{}" is not a valid UTF-8 file'.format(filename), file=sys.stderr)
except KeyboardInterrupt:
os.remove(filename + ".out")
print("wmlindent: %s interrupted" % filename, file=sys.stderr)

View file

@ -2839,8 +2839,12 @@ def translator(filename, mapxforms, textxform):
with gzip.open(filename) as content:
unmodified = content.readlines()
else:
with codecs.open(filename, "r", "utf8") as content:
unmodified = content.readlines()
try:
with codecs.open(filename, "r", "utf8") as content:
unmodified = content.readlines()
except UnicodeDecodeError as e:
print('wmllint: "{}" is not a valid UTF-8 file'.format(filename), file=sys.stderr)
return
# Pull file into an array of lines, CR-stripping as needed
mfile = []
map_only = filename.endswith(".map")