From d23691e654d05eb153095d7e753009c19ad19b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Sun, 2 Oct 2022 10:08:09 +0200 Subject: [PATCH 1/2] Raise ``syntax-error`` correctly on invalid encodings --- doc/whatsnew/fragments/7522.bugfix | 3 +++ pylint/lint/pylinter.py | 5 ++++- tests/functional/s/syntax/syntax_error_invalid_encoding.py | 1 + tests/functional/s/syntax/syntax_error_invalid_encoding.txt | 1 + 4 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 doc/whatsnew/fragments/7522.bugfix create mode 100644 tests/functional/s/syntax/syntax_error_invalid_encoding.py create mode 100644 tests/functional/s/syntax/syntax_error_invalid_encoding.txt diff --git a/doc/whatsnew/fragments/7522.bugfix b/doc/whatsnew/fragments/7522.bugfix new file mode 100644 index 0000000000..f4fa9da1af --- /dev/null +++ b/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 diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index f6385fa086..d542e29cad 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -1016,9 +1016,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, diff --git a/tests/functional/s/syntax/syntax_error_invalid_encoding.py b/tests/functional/s/syntax/syntax_error_invalid_encoding.py new file mode 100644 index 0000000000..e39b687c3d --- /dev/null +++ b/tests/functional/s/syntax/syntax_error_invalid_encoding.py @@ -0,0 +1 @@ +# -*- coding: lala -*- # [syntax-error] diff --git a/tests/functional/s/syntax/syntax_error_invalid_encoding.txt b/tests/functional/s/syntax/syntax_error_invalid_encoding.txt new file mode 100644 index 0000000000..0a3caf889f --- /dev/null +++ b/tests/functional/s/syntax/syntax_error_invalid_encoding.txt @@ -0,0 +1 @@ +syntax-error:1:0:None:None::"Parsing failed: 'unknown encoding for '/Users/daniel/DocumentenLaptop/Programming/Github/pylint/tests/functional/s/syntax/syntax_error_invalid_encoding.py': lala'":HIGH From 5e575539c2a6c7d59165835c55c56735d8ae42dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Sun, 2 Oct 2022 21:50:48 +0200 Subject: [PATCH 2/2] Fix test --- tests/functional/s/syntax/syntax_error_invalid_encoding.py | 1 - tests/functional/s/syntax/syntax_error_invalid_encoding.txt | 1 - tests/regrtest_data/invalid_encoding.py | 1 + tests/test_self.py | 5 +++++ 4 files changed, 6 insertions(+), 2 deletions(-) delete mode 100644 tests/functional/s/syntax/syntax_error_invalid_encoding.py delete mode 100644 tests/functional/s/syntax/syntax_error_invalid_encoding.txt create mode 100644 tests/regrtest_data/invalid_encoding.py diff --git a/tests/functional/s/syntax/syntax_error_invalid_encoding.py b/tests/functional/s/syntax/syntax_error_invalid_encoding.py deleted file mode 100644 index e39b687c3d..0000000000 --- a/tests/functional/s/syntax/syntax_error_invalid_encoding.py +++ /dev/null @@ -1 +0,0 @@ -# -*- coding: lala -*- # [syntax-error] diff --git a/tests/functional/s/syntax/syntax_error_invalid_encoding.txt b/tests/functional/s/syntax/syntax_error_invalid_encoding.txt deleted file mode 100644 index 0a3caf889f..0000000000 --- a/tests/functional/s/syntax/syntax_error_invalid_encoding.txt +++ /dev/null @@ -1 +0,0 @@ -syntax-error:1:0:None:None::"Parsing failed: 'unknown encoding for '/Users/daniel/DocumentenLaptop/Programming/Github/pylint/tests/functional/s/syntax/syntax_error_invalid_encoding.py': lala'":HIGH diff --git a/tests/regrtest_data/invalid_encoding.py b/tests/regrtest_data/invalid_encoding.py new file mode 100644 index 0000000000..d508d19df8 --- /dev/null +++ b/tests/regrtest_data/invalid_encoding.py @@ -0,0 +1 @@ +# -*- coding: lala -*- diff --git a/tests/test_self.py b/tests/test_self.py index d5167cfe67..324b913cdf 100644 --- a/tests/test_self.py +++ b/tests/test_self.py @@ -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."""