Skip to content

Commit 5e546d2

Browse files
isidenticallysnikolaou
authored andcommitted
Stricter conversion chechker (python#189)
1 parent 49c8d62 commit 5e546d2

4 files changed

Lines changed: 19 additions & 5 deletions

File tree

‎Grammar/python.gram‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,13 +883,13 @@ gstring_middle[expr_ty]:
883883
# TODO: (Ideally we need a way similar to 'memo' so the parser can set the tokenize mode on fstring/normal)
884884
gstring_replacement_field[expr_ty]:
885885
| expr_start='{' a=(yield_expr | star_expressions) debug_expr="="? conversion=[
886-
"!" conv=NAME {conv}
886+
conv_token="!" conv=NAME { _PyPegen_check_fstring_conversion(p, conv_token, conv) ? NULL : conv }
887887
] format=[
888888
':' spec=gstring_format_spec* { spec ? _PyAST_JoinedStr((asdl_expr_seq*)spec, EXTRA) : NULL }
889889
] &&'}' {
890890
_PyPegen_formatted_value(p, a, debug_expr, conversion, format, EXTRA)
891891
}
892-
gstring_format_spec[expr_ty]:
892+
gstring_format_spec[expr_ty]:
893893
| t=FSTRING_MIDDLE { _PyPegen_constant_from_token2(p, t) }
894894
| gstring_replacement_field
895895
gstring[expr_ty]:

‎Parser/action_helpers.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,19 @@ _PyPegen_check_legacy_stmt(Parser *p, expr_ty name) {
10711071
return 0;
10721072
}
10731073

1074+
int
1075+
_PyPegen_check_fstring_conversion(Parser *p, Token* symbol, expr_ty conv) {
1076+
if (symbol->lineno != conv->lineno || symbol->end_col_offset != conv->col_offset) {
1077+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
1078+
symbol, conv,
1079+
"conversion type must come right after the exclamanation mark"
1080+
);
1081+
return 1;
1082+
}
1083+
return 0;
1084+
}
1085+
1086+
10741087
const char *
10751088
_PyPegen_get_expr_name(expr_ty e)
10761089
{

‎Parser/parser.c‎

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

‎Parser/pegen.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ expr_ty _PyPegen_ensure_real(Parser *p, expr_ty);
329329
asdl_seq *_PyPegen_join_sequences(Parser *, asdl_seq *, asdl_seq *);
330330
int _PyPegen_check_barry_as_flufl(Parser *, Token *);
331331
int _PyPegen_check_legacy_stmt(Parser *p, expr_ty t);
332+
int _PyPegen_check_fstring_conversion(Parser *p, Token *, expr_ty t);
332333
mod_ty _PyPegen_make_module(Parser *, asdl_stmt_seq *);
333334
void *_PyPegen_arguments_parsing_error(Parser *, expr_ty);
334335
expr_ty _PyPegen_get_last_comprehension_item(comprehension_ty comprehension);

0 commit comments

Comments
 (0)