@@ -998,18 +998,38 @@ _PyPegen_setup_full_format_spec(Parser *p, Token *colon, asdl_expr_seq *spec, in
998998 return NULL ;
999999 }
10001000
1001- // This is needed to keep compatibility with 3.11, where an empty format spec is parsed
1002- // as an *empty* JoinedStr node, instead of having an empty constant in it.
1003- if (asdl_seq_LEN (spec ) == 1 ) {
1004- expr_ty e = asdl_seq_GET (spec , 0 );
1005- if (e -> kind == Constant_kind
1006- && PyUnicode_Check (e -> v .Constant .value )
1007- && PyUnicode_GetLength (e -> v .Constant .value ) == 0 ) {
1008- spec = _Py_asdl_expr_seq_new (0 , arena );
1001+ // This is needed to keep compatibility with 3.11, where an empty format
1002+ // spec is parsed as an *empty* JoinedStr node, instead of having an empty
1003+ // constant in it.
1004+ Py_ssize_t n_items = asdl_seq_LEN (spec );
1005+ Py_ssize_t non_empty_count = 0 ;
1006+ for (Py_ssize_t i = 0 ; i < n_items ; i ++ ) {
1007+ expr_ty item = asdl_seq_GET (spec , i );
1008+ non_empty_count += !(item -> kind == Constant_kind &&
1009+ PyUnicode_CheckExact (item -> v .Constant .value ) &&
1010+ PyUnicode_GET_LENGTH (item -> v .Constant .value ) == 0 );
1011+ }
1012+ if (non_empty_count != n_items ) {
1013+ asdl_expr_seq * resized_spec =
1014+ _Py_asdl_expr_seq_new (non_empty_count , p -> arena );
1015+ if (resized_spec == NULL ) {
1016+ return NULL ;
1017+ }
1018+ Py_ssize_t j = 0 ;
1019+ for (Py_ssize_t i = 0 ; i < n_items ; i ++ ) {
1020+ expr_ty item = asdl_seq_GET (spec , i );
1021+ if (item -> kind == Constant_kind &&
1022+ PyUnicode_CheckExact (item -> v .Constant .value ) &&
1023+ PyUnicode_GET_LENGTH (item -> v .Constant .value ) == 0 ) {
1024+ continue ;
1025+ }
1026+ asdl_seq_SET (resized_spec , j ++ , item );
10091027 }
1028+ assert (j == non_empty_count );
1029+ spec = resized_spec ;
10101030 }
1011-
1012- expr_ty res = _PyAST_JoinedStr ( spec , lineno , col_offset , end_lineno , end_col_offset , p -> arena );
1031+ expr_ty res = _PyAST_JoinedStr ( spec , lineno , col_offset , end_lineno ,
1032+ end_col_offset , p -> arena );
10131033 if (!res ) {
10141034 return NULL ;
10151035 }
0 commit comments