Skip to content

Commit 009b529

Browse files
committed
fix: adapt to new tokenize error messages
1 parent 2a0cee9 commit 009b529

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

Diff for: coverage/parser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def parse_source(self) -> None:
251251
"""
252252
try:
253253
self._raw_parse()
254-
except (tokenize.TokenError, IndentationError) as err:
254+
except (tokenize.TokenError, IndentationError, SyntaxError) as err:
255255
if hasattr(err, "lineno"):
256256
lineno = err.lineno # IndentationError
257257
else:

Diff for: tests/test_parser.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def foo():
128128
def test_indentation_error(self) -> None:
129129
msg = (
130130
"Couldn't parse '<code>' as Python source: " +
131-
"'unindent does not match any outer indentation level' at line 3"
131+
"'unindent does not match any outer indentation level.*' at line 3"
132132
)
133133
with pytest.raises(NotPython, match=msg):
134134
_ = self.parse_source("""\
@@ -138,11 +138,17 @@ def test_indentation_error(self) -> None:
138138
""")
139139

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

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

0 commit comments

Comments
 (0)