Skip to content
Closed
Prev Previous commit
Next Next commit
Generate a (func, rawstr, conv, spec) tuple for thunks
  • Loading branch information
gvanrossum committed May 3, 2022
commit 94c73358c4c4d271ac23ae928dc7c0b3db3394cb
57 changes: 54 additions & 3 deletions Parser/action_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -891,16 +891,67 @@ _PyPegen_tag_string(Parser *p, expr_ty tag, Token *tok)
if (str == NULL)
return NULL;
if (str->kind == JoinedStr_kind) {
// Transform FormattedValue items into thunks
// Transform FormattedValue items into thunks (for now, tuples)
asdl_expr_seq *values = str->v.JoinedStr.values;
int nvalues = asdl_seq_LEN(values);
expr_ty none = NULL;
for (int i = 0; i < nvalues; i++) {
expr_ty value = asdl_seq_GET(values, i);
if (value->kind == FormattedValue_kind) {
expr_ty lambda = lambdafy(p, value->v.FormattedValue.value);
if (none == NULL) {
none = _PyAST_Constant(Py_None, NULL,
str->lineno, str->col_offset,
str->end_lineno, str->end_col_offset,
p->arena);
if (none == NULL)
return NULL;
}
expr_ty expr = value->v.FormattedValue.value;
expr_ty lambda = lambdafy(p, expr);
if (lambda == NULL)
return NULL;
asdl_seq_SET(values, i, lambda);
constant rawstr = _PyAST_ExprAsUnicode(expr);
if (rawstr == NULL)
return NULL;
expr_ty raw = _PyAST_Constant(rawstr, NULL,
expr->lineno, expr->col_offset,
expr->end_lineno, expr->end_col_offset,
p->arena);
if (raw == NULL)
return NULL;
expr_ty conv = none;
int conversion = value->v.FormattedValue.conversion;
if (conversion >= 0) {
char buf[1];
buf[0] = conversion;
constant uconv = _PyUnicode_FromASCII(buf, 1);
if (uconv == NULL)
return NULL;
conv = _PyAST_Constant(uconv, NULL,
expr->lineno, expr->col_offset,
expr->end_lineno, expr->end_col_offset,
p->arena);
if (conv == NULL)
return NULL;
}
expr_ty spec = value->v.FormattedValue.format_spec;
if (spec == NULL) {
spec = none;
}
asdl_expr_seq *elts = _Py_asdl_expr_seq_new(4, p->arena);
if (elts == NULL)
return NULL;
asdl_seq_SET(elts, 0, lambda);
asdl_seq_SET(elts, 1, raw);
asdl_seq_SET(elts, 2, conv);
asdl_seq_SET(elts, 3, spec);
expr_ty tuple = _PyAST_Tuple(elts, Load,
value->lineno, value->col_offset,
value->end_lineno, value->end_col_offset,
p->arena);
if (tuple == NULL)
return NULL;
asdl_seq_SET(values, i, tuple);
}
}
}
Expand Down