Skip to content

Commit

Permalink
[astroid syntax error] Fix a crash when the line and column can't be …
Browse files Browse the repository at this point in the history
…retrieved

Closes #3860
  • Loading branch information
Pierre-Sassoulas committed Jun 30, 2022
1 parent e876f04 commit 865b1b8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
16 changes: 7 additions & 9 deletions pylint/lint/pylinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,11 +692,9 @@ def _check_files(
)
msg = get_fatal_error_message(file.filepath, template_path)
if isinstance(ex, AstroidError):
symbol = "astroid-error"
self.add_message(symbol, args=(file.filepath, msg))
self.add_message("astroid-error", args=(file.filepath, msg))
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
self.add_message("fatal", args=msg)

def _check_file(
self,
Expand Down Expand Up @@ -914,12 +912,12 @@ def get_ast(
data, modname, filepath
)
except astroid.AstroidSyntaxError as ex:
# pylint: disable=no-member
error, line, col = getattr(ex, "error", None), 0, 0
if error:
line = error.lineno or 0
col = error.offset or 0
self.add_message(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
"syntax-error", line=line, col_offset=col, args=ex, confidence=HIGH
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
Expand Down
1 change: 1 addition & 0 deletions tests/functional/r/regression_02/regression_3860.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# encoding=UTF-9 # [syntax-error]
2 changes: 2 additions & 0 deletions tests/functional/r/regression_02/regression_3860.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
syntax-error:1:0:None:None::"Python 3 encoding specification error or unknown encoding:
unknown encoding for '/home/pierre/pylint/tests/functional/r/regression_02/regression_3860.py': UTF-9":HIGH
3 changes: 2 additions & 1 deletion tests/functional/s/syntax/syntax_error.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
syntax-error:1:10:None:None::invalid syntax (<unknown>, line 1):UNDEFINED
syntax-error:1:10:None:None::"Parsing Python code failed:
invalid syntax (<unknown>, line 1)":HIGH
3 changes: 2 additions & 1 deletion tests/test_self.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,8 @@ def foobar(arg):
def test_stdin_syntaxerror(self) -> None:
expected_output = (
"************* Module a\n"
"a.py:1:4: E0001: invalid syntax (<unknown>, line 1) (syntax-error)"
"a.py:1:4: E0001: Parsing Python code failed:\n"
"invalid syntax (<unknown>, line 1) (syntax-error)"
)

with mock.patch(
Expand Down

0 comments on commit 865b1b8

Please sign in to comment.