Skip to content

Commit 20996e1

Browse files
Add tests for invalid numeric literal error offset
1 parent 5bf3295 commit 20996e1

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

Lib/test/test_grammar.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,26 @@ def test_bad_numerical_literals(self):
140140
check("1e2_", "invalid decimal literal")
141141
check("1e+", "invalid decimal literal")
142142

143+
def test_end_of_numerical_literals_offset(self):
144+
# gh-149277: verify the error caret points at the first invalid
145+
# character, not the last valid digit.
146+
cases = [
147+
("0xfg", 4),
148+
("0x9g", 4),
149+
("0b1z", 4),
150+
("0o7q", 4),
151+
("9spam", 2),
152+
("0xfspam", 4),
153+
("1.0x", 4),
154+
("1e3w", 4),
155+
("1jz", 3),
156+
]
157+
for source, expected_offset in cases:
158+
with self.subTest(source=source):
159+
with self.assertRaises(SyntaxError) as cm:
160+
compile(source, "<test>", "eval")
161+
self.assertEqual(cm.exception.offset, expected_offset)
162+
143163
def test_end_of_numerical_literals(self):
144164
def check(test, error=False):
145165
with self.subTest(expr=test):

0 commit comments

Comments
 (0)