Skip to content

Commit d4cb3f8

Browse files
committed
open(encoding=None) does not do what we expect it does; more graceful handling of unexpected characters exception
1 parent 161fbc1 commit d4cb3f8

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

main.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,32 @@ def asdict(self, with_message=True):
2727
else "unexpected_character",
2828
"lineno": self.exception.line,
2929
"column": self.exception.column,
30-
"found_type": self.exception.token.type.lower(),
31-
"found_value": self.exception.token.value,
32-
"expected": sorted(x for x in self.exception.accepts if "__ANON" not in x),
3330
"line": self.filecontent.split("\n")[self.exception.line - 1],
3431
**({"message": str(self)} if with_message else {}),
32+
**({
33+
"found_type": self.exception.token.type.lower(),
34+
"found_value": self.exception.token.value,
35+
"expected": sorted(x for x in self.exception.accepts if "__ANON" not in x),
36+
} if hasattr(self.exception, 'token') and self.exception.token else {})
3537
}
3638

3739
def __str__(self):
3840
d = self.asdict(with_message=False)
39-
if len(d["expected"]) == 1:
41+
if not d.get('expected'):
42+
exp = None
43+
elif len(d["expected"]) == 1:
4044
exp = d["expected"][0]
4145
else:
4246
exp = f"one of {' '.join(d['expected'])}"
4347

4448
sth = "character" if d["type"] == "unexpected_character" else ""
4549

46-
return f"On line {d['lineno']} column {d['column']}:\nUnexpected {sth}{d['found_type']} ('{d['found_value']}')\nExpecting {exp}\n{d['lineno']:05d} | {d['line']}\n {' ' * (self.exception.column - 1)}^"
50+
if d.get('found_type') and d.get('found_value'):
51+
sth += f"{d['found_type']} ('{d['found_value']}')"
52+
53+
formatted_exp = f'Expecting {exp}\n' if exp else ''
54+
55+
return f"On line {d['lineno']} column {d['column']}:\nUnexpected {sth}\n{formatted_exp}{d['lineno']:05d} | {d['line']}\n {' ' * (self.exception.column - 1)}^"
4756

4857

4958
class DuplicateNameError(ValidationError):
@@ -292,7 +301,7 @@ def process_tree(filecontent, file_tree, with_progress):
292301
def parse(*, filename=None, filecontent=None, with_progress=False, with_tree=True):
293302
if filename:
294303
assert not filecontent
295-
filecontent = open(filename, encoding=None).read()
304+
filecontent = open(filename, encoding='latin-1').read()
296305

297306
# Match and remove the comments
298307
p = r"/\*[\s\S]*?\*/"

0 commit comments

Comments
 (0)