Skip to content

Commit 51f16dc

Browse files
committed
Copy the rest of Python/ast*.c from original branch
1 parent b93caf0 commit 51f16dc

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed

Python/ast.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,8 @@ validate_expr(struct validator *state, expr_ty exp, expr_context_ty ctx)
345345
ret = validate_exprs(state, exp->v.JoinedStr.values, Load, 0);
346346
break;
347347
case TagString_kind:
348-
ret = validate_expr(state, exp->v.TagString.str, Load);
348+
ret = validate_expr(state, exp->v.TagString.tag, Load) &&
349+
validate_expr(state, exp->v.TagString.str, Load);
349350
break;
350351
case FormattedValue_kind:
351352
if (validate_expr(state, exp->v.FormattedValue.value, Load) == 0)

Python/ast_opt.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,10 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
788788
case JoinedStr_kind:
789789
CALL_SEQ(astfold_expr, expr, node_->v.JoinedStr.values);
790790
break;
791+
case TagString_kind:
792+
CALL(astfold_expr, expr_ty, node_->v.TagString.tag);
793+
CALL(astfold_expr, expr_ty, node_->v.TagString.str);
794+
break;
791795
case Attribute_kind:
792796
CALL(astfold_expr, expr_ty, node_->v.Attribute.value);
793797
break;

Python/ast_unparse.c

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ expr_as_unicode(expr_ty e, int level);
2525
static int
2626
append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level);
2727
static int
28-
append_joinedstr(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec);
28+
append_joinedstr(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec, bool is_tag_str);
2929
static int
3030
append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e);
3131
static int
@@ -602,16 +602,53 @@ append_fstring_unicode(_PyUnicodeWriter *writer, PyObject *unicode)
602602
return result;
603603
}
604604

605+
static int
606+
append_interpolation(_PyUnicodeWriter *writer, expr_ty e)
607+
{
608+
APPEND_STR("{");
609+
if (e->kind == Tuple_kind) {
610+
asdl_expr_seq *elts = e->v.Tuple.elts;
611+
if (asdl_seq_LEN(elts) == 4) {
612+
expr_ty raw = asdl_seq_GET(elts, 1);
613+
if (raw->kind == Constant_kind) {
614+
constant c = raw->v.Constant.value;
615+
if (PyUnicode_CheckExact(c)) {
616+
if (-1 == _PyUnicodeWriter_WriteStr(writer, c))
617+
return -1;
618+
}
619+
}
620+
expr_ty conv = asdl_seq_GET(elts, 2);
621+
if (conv->kind == Constant_kind) {
622+
constant c = conv->v.Constant.value;
623+
if (PyUnicode_CheckExact(c)) {
624+
APPEND_STR("!");
625+
if (-1 == _PyUnicodeWriter_WriteStr(writer, c))
626+
return -1;
627+
}
628+
}
629+
expr_ty spec = asdl_seq_GET(elts, 3);
630+
if (spec->kind == JoinedStr_kind) {
631+
APPEND_STR(":");
632+
if (-1 == append_joinedstr(writer, spec, true, false))
633+
return -1;
634+
}
635+
}
636+
}
637+
APPEND_STR_FINISH("}");
638+
}
639+
605640
static int
606641
append_fstring_element(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec)
607642
{
608643
switch (e->kind) {
609644
case Constant_kind:
610645
return append_fstring_unicode(writer, e->v.Constant.value);
611646
case JoinedStr_kind:
612-
return append_joinedstr(writer, e, is_format_spec);
647+
return append_joinedstr(writer, e, is_format_spec, false);
613648
case FormattedValue_kind:
614649
return append_formattedvalue(writer, e);
650+
case Tuple_kind:
651+
return append_interpolation(writer, e);
615652
default:
616653
PyErr_SetString(PyExc_SystemError,
617654
"unknown expression kind inside f-string");
@@ -645,16 +682,19 @@ build_fstring_body(asdl_expr_seq *values, bool is_format_spec)
645682
}
646683

647684
static int
648-
append_joinedstr(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec)
685+
append_joinedstr(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec, bool is_tag_str)
649686
{
650-
int result = -1;
651687
PyObject *body = build_fstring_body(e->v.JoinedStr.values, is_format_spec);
652688
if (!body) {
653689
return -1;
654690
}
655691

692+
int result = 0;
656693
if (!is_format_spec) {
657-
if (-1 != append_charp(writer, "f") &&
694+
if (!is_tag_str) {
695+
result = append_charp(writer, "f");
696+
}
697+
if (-1 != result &&
658698
-1 != append_repr(writer, body))
659699
{
660700
result = 0;
@@ -667,6 +707,18 @@ append_joinedstr(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec)
667707
return result;
668708
}
669709

710+
static int
711+
append_tagstring(_PyUnicodeWriter *writer, expr_ty e)
712+
{
713+
APPEND_EXPR(e->v.TagString.tag, 0);
714+
expr_ty str = e->v.TagString.str;
715+
if (str->kind == JoinedStr_kind) {
716+
if (-1 == append_joinedstr(writer, str, false, true))
717+
return -1;
718+
}
719+
return 0;
720+
}
721+
670722
static int
671723
append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e)
672724
{
@@ -891,7 +943,9 @@ append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level)
891943
}
892944
return append_ast_constant(writer, e->v.Constant.value);
893945
case JoinedStr_kind:
894-
return append_joinedstr(writer, e, false);
946+
return append_joinedstr(writer, e, false, false);
947+
case TagString_kind:
948+
return append_tagstring(writer, e);
895949
case FormattedValue_kind:
896950
return append_formattedvalue(writer, e);
897951
/* The following exprs can be assignment targets. */

0 commit comments

Comments
 (0)