@@ -1002,34 +1002,35 @@ _PyPegen_setup_full_format_spec(Parser *p, Token *colon, asdl_expr_seq *spec, in
1002
1002
// spec is parsed as an *empty* JoinedStr node, instead of having an empty
1003
1003
// constant in it.
1004
1004
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 ;
1010
1006
for (Py_ssize_t i = 0 ; i < n_items ; i ++ ) {
1011
1007
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 ) {
1023
1016
return NULL ;
1024
1017
}
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 );
1027
1027
}
1028
+ assert (j == non_empty_count );
1028
1029
} else {
1029
- resized_specs = seq ;
1030
+ resized_spec = spec ;
1030
1031
}
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 );
1033
1034
if (!res ) {
1034
1035
return NULL ;
1035
1036
}
0 commit comments