Skip to content

Commit a258854

Browse files
DanielNoordPierre-Sassoulas
authored andcommitted
Raise syntax-error correctly on invalid encodings (#7553)
1 parent 43ecd7d commit a258854

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

doc/whatsnew/fragments/7522.bugfix

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed an issue where ``syntax-error`` couldn't be raised on files with invalid encodings.
2+
3+
Closes #7522

pylint/lint/pylinter.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1013,9 +1013,12 @@ def get_ast(
10131013
data, modname, filepath
10141014
)
10151015
except astroid.AstroidSyntaxError as ex:
1016+
line = getattr(ex.error, "lineno", None)
1017+
if line is None:
1018+
line = 0
10161019
self.add_message(
10171020
"syntax-error",
1018-
line=getattr(ex.error, "lineno", 0),
1021+
line=line,
10191022
col_offset=getattr(ex.error, "offset", None),
10201023
args=f"Parsing failed: '{ex.error}'",
10211024
confidence=HIGH,
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding: lala -*-

tests/test_self.py

+5
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,11 @@ def test_ignore_path_recursive_current_dir(self) -> None:
11911191
code=0,
11921192
)
11931193

1194+
def test_syntax_error_invalid_encoding(self) -> None:
1195+
module = join(HERE, "regrtest_data", "invalid_encoding.py")
1196+
expected_output = "unknown encoding"
1197+
self._test_output([module, "-E"], expected_output=expected_output)
1198+
11941199

11951200
class TestCallbackOptions:
11961201
"""Test for all callback options we support."""

0 commit comments

Comments
 (0)