Skip to content

Commit

Permalink
[syntax-error] Fix a crash when the line and column can't be retrieved
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Jul 30, 2022
1 parent c33d237 commit 1af2c02
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/3860.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fixed a syntax-error crash that was not handled properly when the declared encoding of a file
was ``utf-9``.

Closes #3860
7 changes: 4 additions & 3 deletions pylint/checkers/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)
from pylint.exceptions import EmptyReportError
from pylint.graph import DotBackend, get_cycles
from pylint.interfaces import HIGH
from pylint.reporters.ureports.nodes import Paragraph, Section, VerbatimText
from pylint.typing import MessageDefinitionTuple
from pylint.utils import IsortDriver
Expand Down Expand Up @@ -800,10 +801,10 @@ def _get_imported_module(
return None
self.add_message("relative-beyond-top-level", node=importnode)
except astroid.AstroidSyntaxError as exc:
message = (
f"Cannot import {modname!r} due to syntax error {str(exc.error)!r}"
message = f"Cannot import {modname!r} due to '{exc.error}'"
self.add_message(
"syntax-error", line=importnode.lineno, args=message, confidence=HIGH
)
self.add_message("syntax-error", line=importnode.lineno, args=message)

except astroid.AstroidBuildingError:
if not self.linter.is_message_enabled("import-error"):
Expand Down
11 changes: 6 additions & 5 deletions pylint/lint/pylinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,11 +692,11 @@ 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), confidence=HIGH
)
else:
symbol = "fatal"
self.add_message(symbol, args=msg)
self.add_message("fatal", args=msg, confidence=HIGH)

def _check_file(
self,
Expand Down Expand Up @@ -918,7 +918,8 @@ def get_ast(
"syntax-error",
line=getattr(ex.error, "lineno", 0),
col_offset=getattr(ex.error, "offset", None),
args=str(ex.error),
args=f"Parsing failed: '{ex.error}'",
confidence=HIGH,
)
except astroid.AstroidBuildingError as ex:
self.add_message("parse-error", args=ex)
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/i/import_error.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import-error:3:0:3:22::Unable to import 'totally_missing':UNDEFINED
import-error:21:4:21:26::Unable to import 'maybe_missing_2':UNDEFINED
no-name-in-module:33:0:33:49::No name 'syntax_error' in module 'functional.s.syntax':UNDEFINED
syntax-error:33:0:None:None::Cannot import 'functional.s.syntax.syntax_error' due to syntax error 'invalid syntax (<unknown>, line 1)':UNDEFINED
syntax-error:33:0:None:None::Cannot import 'functional.s.syntax.syntax_error' due to 'invalid syntax (<unknown>, line 1)':HIGH
multiple-imports:78:0:78:15::Multiple imports on one line (foo, bar):UNDEFINED
2 changes: 1 addition & 1 deletion tests/functional/s/syntax/syntax_error.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
syntax-error:1:5:None:None::invalid syntax (<unknown>, line 1):UNDEFINED
syntax-error:1:5:None:None::"Parsing failed: 'invalid syntax (<unknown>, line 1)'":HIGH
9 changes: 3 additions & 6 deletions tests/test_self.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,12 +569,9 @@ def foobar(arg):
expected_output=expected,
)

def test_stdin_syntaxerror(self) -> None:
expected_output = (
"************* Module a\n"
"a.py:1:4: E0001: invalid syntax (<unknown>, line 1) (syntax-error)"
)

def test_stdin_syntax_error(self) -> None:
expected_output = """************* Module a
a.py:1:4: E0001: Parsing failed: 'invalid syntax (<unknown>, line 1)' (syntax-error)"""
with mock.patch(
"pylint.lint.pylinter._read_stdin", return_value="for\n"
) as mock_stdin:
Expand Down

0 comments on commit 1af2c02

Please sign in to comment.