Skip to content

Commit

Permalink
Modify test conversions to fix failing tests (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
lysnikolaou authored Oct 31, 2022
1 parent 947d210 commit de6ac55
Show file tree
Hide file tree
Showing 3 changed files with 1,247 additions and 1,222 deletions.
7 changes: 4 additions & 3 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -852,16 +852,17 @@ lambda_param[arg_ty]: a=NAME { _PyAST_arg(a->v.Name.id, NULL, NULL, EXTRA) }
# LITERALS
# ========

gstring_conversion[expr_ty]:
| "!" conv=NAME { conv }
| "!" !NAME { RAISE_SYNTAX_ERROR("f-string: invalid conversion character: expected 's', 'r', or 'a'") }
gstring_middle[expr_ty]:
| gstring_replacement_field
| t=FSTRING_MIDDLE { _PyPegen_constant_from_token2(p, t) }
# There are some shenanigans with the gstring_format_spec: Don't try to put it in its own rule
# or otherwise it will try to parse the first token with the regular tokenizer mode (due to the EXTRA).
# TODO: (Ideally we need a way similar to 'memo' so the parser can set the tokenize mode on fstring/normal)
gstring_replacement_field[expr_ty]:
| expr_start='{' a=(yield_expr | star_expressions) debug_expr="="? conversion=[
"!" conv=NAME {conv}
] format=[
| expr_start='{' a=(yield_expr | star_expressions) debug_expr="="? conversion=[gstring_conversion] format=[
':' spec=gstring_format_spec* { spec ? _PyAST_JoinedStr((asdl_expr_seq*)spec, EXTRA) : NULL }
] &&'}' {
_PyPegen_formatted_value(p, a, debug_expr, conversion, format, EXTRA)
Expand Down
8 changes: 3 additions & 5 deletions Lib/test/test_fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,16 +993,14 @@ def test_conversions(self):
"f'{3!G}'",
"f'{3!!}'",
"f'{3!:}'",
"f'{3! s}'", # no space before conversion char
])

self.assertAllRaise(SyntaxError, "f-string: expecting '}'",
["f'{x!s{y}}'",
"f'{3!ss}'",
"f'{3!ss:}'",
"f'{3!ss:s}'",
])

with self.assertRaisesRegex(SyntaxError, "expected '}'"):
eval("f'{x!s{y}}'")

def test_assignment(self):
self.assertAllRaise(SyntaxError, r'invalid syntax',
["f'' = 3",
Expand Down
Loading

0 comments on commit de6ac55

Please sign in to comment.