Skip to content

Commit

Permalink
fix: catch TokenError on parse (#1788)
Browse files Browse the repository at this point in the history
* catch TokenError on parse

* typo, use env.PYVERSION

* Update tests/test_parser.py

---------

Co-authored-by: Ned Batchelder <ned@nedbatchelder.com>
  • Loading branch information
tybug and nedbat committed May 20, 2024
1 parent 81089de commit 364282e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions coverage/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,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="leading_newline_eof",
marks=[
pytest.mark.skipif(env.PYVERSION >= (3, 12), reason="parses fine in 3.12"),
]
)
])
def test_not_python(self, text: str) -> None:
msg = r"Couldn't parse '<code>' as Python source: '.*' at line \d+"
Expand Down

0 comments on commit 364282e

Please sign in to comment.