Skip to content

Commit 30ea09f

Browse files
authored
Merge pull request python#54 from sunmy2019/handle-invalid-expressions
2 parents 6f51141 + 684fb0b commit 30ea09f

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

Grammar/python.gram

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,11 +884,11 @@ fstring_middle[expr_ty]:
884884
# or otherwise it will try to parse the first token with the regular tokenizer mode (due to the EXTRA).
885885
# TODO: (Ideally we need a way similar to 'memo' so the parser can set the tokenize mode on fstring/normal)
886886
fstring_replacement_field[expr_ty]:
887-
| expr_start='{' a=(yield_expr | star_expressions) debug_expr="="? conversion=[
887+
| '{' a=(yield_expr | star_expressions) debug_expr="="? conversion=[
888888
conv_token="!" conv=NAME { _PyPegen_check_fstring_conversion(p, conv_token, conv) ? NULL : conv }
889889
] format=[
890890
':' spec=fstring_format_spec* { spec ? _PyAST_JoinedStr((asdl_expr_seq*)spec, EXTRA) : NULL }
891-
] &&'}' {
891+
] '}' {
892892
_PyPegen_formatted_value(p, a, debug_expr, conversion, format, EXTRA)
893893
}
894894
| invalid_replacement_field

Lib/test/test_exceptions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ def ckmsg(src, msg):
155155

156156
ckmsg(s, "'continue' not properly in loop")
157157
ckmsg("continue\n", "'continue' not properly in loop")
158+
ckmsg("f'{6 0}'", "invalid syntax. Perhaps you forgot a comma?")
158159

159160
def testSyntaxErrorMissingParens(self):
160161
def ckmsg(src, msg, exception=SyntaxError):
@@ -227,7 +228,7 @@ def testSyntaxErrorOffset(self):
227228
check('Python = "\u1e54\xfd\u0163\u0125\xf2\xf1" +', 1, 20)
228229
check(b'# -*- coding: cp1251 -*-\nPython = "\xcf\xb3\xf2\xee\xed" +',
229230
2, 19, encoding='cp1251')
230-
check(b'Python = "\xcf\xb3\xf2\xee\xed" +', 1, 18)
231+
check(b'Python = "\xcf\xb3\xf2\xee\xed" +', 1, 10)
231232
check('x = "a', 1, 5)
232233
check('lambda x: x = 2', 1, 1)
233234
check('f{a + b + c}', 1, 2)
@@ -293,7 +294,7 @@ def baz():
293294
294295
{
295296
6
296-
0="""''', 5, 13)
297+
0="""''', 6, 15)
297298

298299
# Errors thrown by symtable.c
299300
check('x = [(yield i) for i in range(3)]', 1, 7)

Parser/parser.c

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)