From ac44af3974d66090f9a8181b793839280d2993c5 Mon Sep 17 00:00:00 2001 From: Liam DeVoe Date: Mon, 20 May 2024 11:16:07 -0400 Subject: [PATCH 1/3] catch TokenError on parse --- coverage/parser.py | 8 ++++++-- tests/test_parser.py | 9 +++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/coverage/parser.py b/coverage/parser.py index 2b4e60aa8..9c9ffbf0a 100644 --- a/coverage/parser.py +++ b/coverage/parser.py @@ -263,10 +263,14 @@ def parse_source(self) -> None: try: self._ast_root = ast.parse(self.text) self._raw_parse() - except (IndentationError, SyntaxError) as err: + except (tokenize.TokenError, IndentationError, SyntaxError) as err: + if hasattr(err, "lineno"): + lineno = err.lineno # IndentationError + else: + lineno = err.args[1][0] # TokenError raise NotPython( f"Couldn't parse '{self.filename}' as Python source: " + - f"{err.args[0]!r} at line {err.lineno}", + f"{err.args[0]!r} at line {lineno}", ) from err ignore = self.excluded | self.raw_docstrings diff --git a/tests/test_parser.py b/tests/test_parser.py index bf3ccd28d..bc1eaf502 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -6,6 +6,7 @@ from __future__ import annotations import textwrap +import sys import pytest @@ -128,6 +129,14 @@ def foo(): pytest.param("0 spaces\n 2\n 1", id="bad_indent"), pytest.param("'''", id="string_eof"), pytest.param("$hello", id="dollar"), + # on 3.10 this passes ast.parse but fails on tokenize.generate_tokens + pytest.param( + "\r'\\\n'''", + id="ledaing_newline_eof", + marks=[ + pytest.mark.skipif(sys.version_info >= (3, 12), reason="parses fine in 3.11") + ] + ) ]) def test_not_python(self, text: str) -> None: msg = r"Couldn't parse '' as Python source: '.*' at line \d+" From 85d7954e7072d9b43280dbeae962cf14e434342a Mon Sep 17 00:00:00 2001 From: Liam DeVoe Date: Mon, 20 May 2024 11:49:04 -0400 Subject: [PATCH 2/3] typo, use env.PYVERSION --- tests/test_parser.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_parser.py b/tests/test_parser.py index bc1eaf502..24dd3cd82 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -6,7 +6,6 @@ from __future__ import annotations import textwrap -import sys import pytest @@ -132,9 +131,9 @@ def foo(): # on 3.10 this passes ast.parse but fails on tokenize.generate_tokens pytest.param( "\r'\\\n'''", - id="ledaing_newline_eof", + id="leading_newline_eof", marks=[ - pytest.mark.skipif(sys.version_info >= (3, 12), reason="parses fine in 3.11") + pytest.mark.skipif(env.PYVERSION >= (3, 12), reason="parses fine in 3.12") ] ) ]) From d6a81985f74a15ec670658f98cba23254fcd11a5 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Mon, 20 May 2024 11:49:48 -0400 Subject: [PATCH 3/3] Update tests/test_parser.py --- tests/test_parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_parser.py b/tests/test_parser.py index 24dd3cd82..f9655d160 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -133,7 +133,7 @@ def foo(): "\r'\\\n'''", id="leading_newline_eof", marks=[ - pytest.mark.skipif(env.PYVERSION >= (3, 12), reason="parses fine in 3.12") + pytest.mark.skipif(env.PYVERSION >= (3, 12), reason="parses fine in 3.12"), ] ) ])