Skip to content

Commit

Permalink
fix: adapt to new tokenize error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed May 22, 2023
1 parent 2a0cee9 commit 009b529
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion coverage/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def parse_source(self) -> None:
"""
try:
self._raw_parse()
except (tokenize.TokenError, IndentationError) as err:
except (tokenize.TokenError, IndentationError, SyntaxError) as err:
if hasattr(err, "lineno"):
lineno = err.lineno # IndentationError
else:
Expand Down
21 changes: 15 additions & 6 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def foo():
def test_indentation_error(self) -> None:
msg = (
"Couldn't parse '<code>' as Python source: " +
"'unindent does not match any outer indentation level' at line 3"
"'unindent does not match any outer indentation level.*' at line 3"
)
with pytest.raises(NotPython, match=msg):
_ = self.parse_source("""\
Expand All @@ -138,11 +138,17 @@ def test_indentation_error(self) -> None:
""")

def test_token_error(self) -> None:
msg = "Couldn't parse '<code>' as Python source: 'EOF in multi-line string' at line 1"
submsgs = [
r"EOF in multi-line string", # before 3.12.0b1
r"unterminated triple-quoted string literal .detected at line 1.", # after 3.12.0b1
]
msg = (
r"Couldn't parse '<code>' as Python source: '"
+ r"(" + "|".join(submsgs) + ")"
+ r"' at line 1"
)
with pytest.raises(NotPython, match=msg):
_ = self.parse_source("""\
'''
""")
_ = self.parse_source("'''")

@xfail_pypy38
def test_decorator_pragmas(self) -> None:
Expand Down Expand Up @@ -254,7 +260,10 @@ def bar(self):
def test_fuzzed_double_parse(self) -> None:
# https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=50381
# The second parse used to raise `TypeError: 'NoneType' object is not iterable`
msg = "EOF in multi-line statement"
msg = (
r"(EOF in multi-line statement)" # before 3.12.0b1
+ r"|(unmatched ']')" # after 3.12.0b1
)
with pytest.raises(NotPython, match=msg):
self.parse_source("]")
with pytest.raises(NotPython, match=msg):
Expand Down

0 comments on commit 009b529

Please sign in to comment.