@@ -1002,34 +1002,35 @@ _PyPegen_setup_full_format_spec(Parser *p, Token *colon, asdl_expr_seq *spec, in
10021002 // spec is parsed as an *empty* JoinedStr node, instead of having an empty
10031003 // constant in it.
10041004 Py_ssize_t n_items = asdl_seq_LEN (spec );
1005- asdl_expr_seq * seq = _Py_asdl_expr_seq_new (n_items , p -> arena );
1006- if (seq == NULL ) {
1007- return NULL ;
1008- }
1009- Py_ssize_t index = 0 ;
1005+ Py_ssize_t non_empty_count = 0 ;
10101006 for (Py_ssize_t i = 0 ; i < n_items ; i ++ ) {
10111007 expr_ty item = asdl_seq_GET (spec , i );
1012- if (item -> kind == Constant_kind &&
1013- PyUnicode_CheckExact (item -> v .Constant .value ) &&
1014- PyUnicode_GET_LENGTH (item -> v .Constant .value ) == 0 ) {
1015- continue ;
1016- }
1017- asdl_seq_SET (seq , index ++ , item );
1018- }
1019- asdl_expr_seq * resized_specs ;
1020- if (index != n_items ) {
1021- resized_specs = _Py_asdl_expr_seq_new (index , p -> arena );
1022- if (resized_specs == NULL ) {
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+ asdl_expr_seq * resized_spec ;
1013+ if (non_empty_count != n_items ) {
1014+ resized_spec = _Py_asdl_expr_seq_new (non_empty_count , p -> arena );
1015+ if (resized_spec == NULL ) {
10231016 return NULL ;
10241017 }
1025- for (Py_ssize_t i = 0 ; i < index ; i ++ ) {
1026- asdl_seq_SET (resized_specs , i , asdl_seq_GET (seq , i ));
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 );
10271027 }
1028+ assert (j == non_empty_count );
10281029 } else {
1029- resized_specs = seq ;
1030+ resized_spec = spec ;
10301031 }
1031- expr_ty res = _PyAST_JoinedStr (resized_specs , lineno , col_offset ,
1032- end_lineno , end_col_offset , p -> arena );
1032+ expr_ty res = _PyAST_JoinedStr (resized_spec , lineno , col_offset , end_lineno ,
1033+ end_col_offset , p -> arena );
10331034 if (!res ) {
10341035 return NULL ;
10351036 }
0 commit comments