Skip to content

Commit

Permalink
Merge pull request #61 from isidentical/solve-conversion-handle
Browse files Browse the repository at this point in the history
  • Loading branch information
pablogsal authored Mar 29, 2023
2 parents 41ba177 + b80d9ea commit 59ec33d
Show file tree
Hide file tree
Showing 3 changed files with 749 additions and 512 deletions.
12 changes: 8 additions & 4 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ yield_stmt[stmt_ty]: y=yield_expr { _PyAST_Expr(y, EXTRA) }

assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }

import_stmt[stmt_ty]:
import_stmt[stmt_ty]:
| invalid_import
| import_name
| import_from
Expand Down Expand Up @@ -415,8 +415,8 @@ try_stmt[stmt_ty]:
| invalid_try_stmt
| 'try' &&':' b=block f=finally_block { _PyAST_Try(b, NULL, NULL, f, EXTRA) }
| 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_block+ el=[else_block] f=[finally_block] { _PyAST_Try(b, ex, el, f, EXTRA) }
| 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_star_block+ el=[else_block] f=[finally_block] {
CHECK_VERSION(stmt_ty, 11, "Exception groups are",
| 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_star_block+ el=[else_block] f=[finally_block] {
CHECK_VERSION(stmt_ty, 11, "Exception groups are",
_PyAST_TryStar(b, ex, el, f, EXTRA)) }


Expand Down Expand Up @@ -1263,7 +1263,7 @@ invalid_group:
invalid_import:
| a='import' dotted_name 'from' dotted_name {
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "Did you mean to use 'from ... import ...' instead?") }

invalid_import_from_targets:
| import_from_as_names ',' NEWLINE {
RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
Expand Down Expand Up @@ -1362,3 +1362,7 @@ invalid_replacement_field:
| '{' a=':' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: expression required before ':'") }
| '{' a='!' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: expression required before '!'") }
| '{' a='}' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: empty expression not allowed") }
| '{' (yield_expr | star_expressions) "="? invalid_conversion_character
invalid_conversion_character:
| a="!" &(':'|'}') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: missed conversion character") }
| a="!" !NAME { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: invalid conversion character") }
11 changes: 8 additions & 3 deletions Lib/test/test_fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,11 +1120,16 @@ def test_conversions(self):
"f'{3!:}'",
])

for conv in 'g', 'A', '3', 'G', '!', 'ä', 'ɐ', 'ª':
for conv_identifier in 'g', 'A', 'G', 'ä', 'ɐ', 'ª':
self.assertAllRaise(SyntaxError,
"f-string: invalid conversion character %r: "
"expected 's', 'r', or 'a'" % conv,
["f'{3!" + conv + "}'"])
"expected 's', 'r', or 'a'" % conv_identifier,
["f'{3!" + conv_identifier + "}'"])

for conv_non_identifier in '3', '!':
self.assertAllRaise(SyntaxError,
"f-string: invalid conversion character",
["f'{3!" + conv_non_identifier + "}'"])

for conv in ' s', ' s ':
self.assertAllRaise(SyntaxError,
Expand Down
Loading

0 comments on commit 59ec33d

Please sign in to comment.