@@ -25,7 +25,7 @@ expr_as_unicode(expr_ty e, int level);
25
25
static int
26
26
append_ast_expr (_PyUnicodeWriter * writer , expr_ty e , int level );
27
27
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 );
29
29
static int
30
30
append_formattedvalue (_PyUnicodeWriter * writer , expr_ty e );
31
31
static int
@@ -602,16 +602,53 @@ append_fstring_unicode(_PyUnicodeWriter *writer, PyObject *unicode)
602
602
return result ;
603
603
}
604
604
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
+
605
640
static int
606
641
append_fstring_element (_PyUnicodeWriter * writer , expr_ty e , bool is_format_spec )
607
642
{
608
643
switch (e -> kind ) {
609
644
case Constant_kind :
610
645
return append_fstring_unicode (writer , e -> v .Constant .value );
611
646
case JoinedStr_kind :
612
- return append_joinedstr (writer , e , is_format_spec );
647
+ return append_joinedstr (writer , e , is_format_spec , false );
613
648
case FormattedValue_kind :
614
649
return append_formattedvalue (writer , e );
650
+ case Tuple_kind :
651
+ return append_interpolation (writer , e );
615
652
default :
616
653
PyErr_SetString (PyExc_SystemError ,
617
654
"unknown expression kind inside f-string" );
@@ -645,16 +682,19 @@ build_fstring_body(asdl_expr_seq *values, bool is_format_spec)
645
682
}
646
683
647
684
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 )
649
686
{
650
- int result = -1 ;
651
687
PyObject * body = build_fstring_body (e -> v .JoinedStr .values , is_format_spec );
652
688
if (!body ) {
653
689
return -1 ;
654
690
}
655
691
692
+ int result = 0 ;
656
693
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 &&
658
698
-1 != append_repr (writer , body ))
659
699
{
660
700
result = 0 ;
@@ -667,6 +707,18 @@ append_joinedstr(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec)
667
707
return result ;
668
708
}
669
709
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
+
670
722
static int
671
723
append_formattedvalue (_PyUnicodeWriter * writer , expr_ty e )
672
724
{
@@ -891,7 +943,9 @@ append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level)
891
943
}
892
944
return append_ast_constant (writer , e -> v .Constant .value );
893
945
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 );
895
949
case FormattedValue_kind :
896
950
return append_formattedvalue (writer , e );
897
951
/* The following exprs can be assignment targets. */
0 commit comments