@@ -832,7 +832,7 @@ STATIC NORETURN void terse_str_format_value_error(void) {
832832 nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , "bad format string" ));
833833}
834834
835- mp_obj_t mp_obj_str_format (mp_uint_t n_args , const mp_obj_t * args ) {
835+ mp_obj_t mp_obj_str_format (mp_uint_t n_args , const mp_obj_t * args , mp_map_t * kwargs ) {
836836 assert (MP_OBJ_IS_STR_OR_BYTES (args [0 ]));
837837
838838 GET_STR_DATA_LEN (args [0 ], str , len );
@@ -932,23 +932,36 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args) {
932932 mp_obj_t arg = mp_const_none ;
933933
934934 if (field_name ) {
935- if (arg_i > 0 ) {
936- if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE ) {
937- terse_str_format_value_error ();
938- } else {
939- nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError ,
940- "can't switch from automatic field numbering to manual field specification" ));
941- }
942- }
943935 int index = 0 ;
944- if (str_to_int (vstr_str (field_name ), & index ) != vstr_len (field_name ) - 1 ) {
945- nlr_raise (mp_obj_new_exception_msg (& mp_type_KeyError , "attributes not supported yet" ));
936+ const char * field = vstr_str (field_name );
937+ const char * lookup = NULL ;
938+ if (MP_LIKELY (unichar_isdigit (* field ))) {
939+ if (arg_i > 0 ) {
940+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE ) {
941+ terse_str_format_value_error ();
942+ } else {
943+ nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError ,
944+ "can't switch from automatic field numbering to manual field specification" ));
945+ }
946+ }
947+ lookup = str_to_int (vstr_str (field_name ), & index ) + field ;
948+ if (index >= n_args - 1 ) {
949+ nlr_raise (mp_obj_new_exception_msg (& mp_type_IndexError , "tuple index out of range" ));
950+ }
951+ arg = args [index + 1 ];
952+ arg_i = -1 ;
953+ } else {
954+ for (lookup = field ; * lookup && * lookup != '.' && * lookup != '[' ; lookup ++ );
955+ mp_obj_t field_q = mp_obj_new_str (field , lookup - field , true/*?*/ );
956+ mp_map_elem_t * key_elem = mp_map_lookup (kwargs , field_q , MP_MAP_LOOKUP );
957+ if (key_elem == NULL ) {
958+ nlr_raise (mp_obj_new_exception_arg1 (& mp_type_KeyError , field_q ));
959+ }
960+ arg = key_elem -> value ;
946961 }
947- if (index >= n_args - 1 ) {
948- nlr_raise (mp_obj_new_exception_msg (& mp_type_IndexError , "tuple index out of range " ));
962+ if (* lookup ) {
963+ nlr_raise (mp_obj_new_exception_msg (& mp_type_NotImplementedError , "attributes not supported yet " ));
949964 }
950- arg = args [index + 1 ];
951- arg_i = -1 ;
952965 vstr_free (field_name );
953966 field_name = NULL ;
954967 } else {
@@ -1775,7 +1788,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_endswith_obj, 2, 3, str_endswith);
17751788MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_strip_obj , 1 , 2 , str_strip );
17761789MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_lstrip_obj , 1 , 2 , str_lstrip );
17771790MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_rstrip_obj , 1 , 2 , str_rstrip );
1778- MP_DEFINE_CONST_FUN_OBJ_VAR (str_format_obj , 1 , mp_obj_str_format );
1791+ MP_DEFINE_CONST_FUN_OBJ_KW (str_format_obj , 1 , mp_obj_str_format );
17791792MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_replace_obj , 3 , 4 , str_replace );
17801793MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_count_obj , 2 , 4 , str_count );
17811794MP_DEFINE_CONST_FUN_OBJ_2 (str_partition_obj , str_partition );
0 commit comments