Skip to content

Commit 36a6723

Browse files
[syntax-error] Fix a crash when the line and column can't be retrieved (#7097)
Closes #3860
1 parent b176645 commit 36a6723

File tree

6 files changed

+19
-16
lines changed

6 files changed

+19
-16
lines changed

doc/whatsnew/fragments/3860.bugfix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fixed a syntax-error crash that was not handled properly when the declared encoding of a file
2+
was ``utf-9``.
3+
4+
Closes #3860

pylint/checkers/imports.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
)
2929
from pylint.exceptions import EmptyReportError
3030
from pylint.graph import DotBackend, get_cycles
31+
from pylint.interfaces import HIGH
3132
from pylint.reporters.ureports.nodes import Paragraph, Section, VerbatimText
3233
from pylint.typing import MessageDefinitionTuple
3334
from pylint.utils import IsortDriver
@@ -800,10 +801,10 @@ def _get_imported_module(
800801
return None
801802
self.add_message("relative-beyond-top-level", node=importnode)
802803
except astroid.AstroidSyntaxError as exc:
803-
message = (
804-
f"Cannot import {modname!r} due to syntax error {str(exc.error)!r}"
804+
message = f"Cannot import {modname!r} due to '{exc.error}'"
805+
self.add_message(
806+
"syntax-error", line=importnode.lineno, args=message, confidence=HIGH
805807
)
806-
self.add_message("syntax-error", line=importnode.lineno, args=message)
807808

808809
except astroid.AstroidBuildingError:
809810
if not self.linter.is_message_enabled("import-error"):

pylint/lint/pylinter.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -692,11 +692,11 @@ def _check_files(
692692
)
693693
msg = get_fatal_error_message(file.filepath, template_path)
694694
if isinstance(ex, AstroidError):
695-
symbol = "astroid-error"
696-
self.add_message(symbol, args=(file.filepath, msg))
695+
self.add_message(
696+
"astroid-error", args=(file.filepath, msg), confidence=HIGH
697+
)
697698
else:
698-
symbol = "fatal"
699-
self.add_message(symbol, args=msg)
699+
self.add_message("fatal", args=msg, confidence=HIGH)
700700

701701
def _check_file(
702702
self,
@@ -918,7 +918,8 @@ def get_ast(
918918
"syntax-error",
919919
line=getattr(ex.error, "lineno", 0),
920920
col_offset=getattr(ex.error, "offset", None),
921-
args=str(ex.error),
921+
args=f"Parsing failed: '{ex.error}'",
922+
confidence=HIGH,
922923
)
923924
except astroid.AstroidBuildingError as ex:
924925
self.add_message("parse-error", args=ex)

tests/functional/i/import_error.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import-error:3:0:3:22::Unable to import 'totally_missing':UNDEFINED
22
import-error:21:4:21:26::Unable to import 'maybe_missing_2':UNDEFINED
33
no-name-in-module:33:0:33:49::No name 'syntax_error' in module 'functional.s.syntax':UNDEFINED
4-
syntax-error:33:0:None:None::Cannot import 'functional.s.syntax.syntax_error' due to syntax error 'invalid syntax (<unknown>, line 1)':UNDEFINED
4+
syntax-error:33:0:None:None::Cannot import 'functional.s.syntax.syntax_error' due to 'invalid syntax (<unknown>, line 1)':HIGH
55
multiple-imports:78:0:78:15::Multiple imports on one line (foo, bar):UNDEFINED
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
syntax-error:1:5:None:None::invalid syntax (<unknown>, line 1):UNDEFINED
1+
syntax-error:1:5:None:None::"Parsing failed: 'invalid syntax (<unknown>, line 1)'":HIGH

tests/test_self.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -569,12 +569,9 @@ def foobar(arg):
569569
expected_output=expected,
570570
)
571571

572-
def test_stdin_syntaxerror(self) -> None:
573-
expected_output = (
574-
"************* Module a\n"
575-
"a.py:1:4: E0001: invalid syntax (<unknown>, line 1) (syntax-error)"
576-
)
577-
572+
def test_stdin_syntax_error(self) -> None:
573+
expected_output = """************* Module a
574+
a.py:1:4: E0001: Parsing failed: 'invalid syntax (<unknown>, line 1)' (syntax-error)"""
578575
with mock.patch(
579576
"pylint.lint.pylinter._read_stdin", return_value="for\n"
580577
) as mock_stdin:

0 commit comments

Comments
 (0)