Skip to content

Commit

Permalink
Raise syntax-error correctly on invalid encodings (#7553)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord authored and Pierre-Sassoulas committed Oct 10, 2022
1 parent 43ecd7d commit a258854
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/7522.bugfix
@@ -0,0 +1,3 @@
Fixed an issue where ``syntax-error`` couldn't be raised on files with invalid encodings.

Closes #7522
5 changes: 4 additions & 1 deletion pylint/lint/pylinter.py
Expand Up @@ -1013,9 +1013,12 @@ def get_ast(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
line = getattr(ex.error, "lineno", None)
if line is None:
line = 0
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
line=line,
col_offset=getattr(ex.error, "offset", None),
args=f"Parsing failed: '{ex.error}'",
confidence=HIGH,
Expand Down
1 change: 1 addition & 0 deletions tests/regrtest_data/invalid_encoding.py
@@ -0,0 +1 @@
# -*- coding: lala -*-
5 changes: 5 additions & 0 deletions tests/test_self.py
Expand Up @@ -1191,6 +1191,11 @@ def test_ignore_path_recursive_current_dir(self) -> None:
code=0,
)

def test_syntax_error_invalid_encoding(self) -> None:
module = join(HERE, "regrtest_data", "invalid_encoding.py")
expected_output = "unknown encoding"
self._test_output([module, "-E"], expected_output=expected_output)


class TestCallbackOptions:
"""Test for all callback options we support."""
Expand Down

0 comments on commit a258854

Please sign in to comment.