@@ -110,15 +110,22 @@ STATIC void dict_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_
110
110
}
111
111
}
112
112
113
- mp_obj_t mp_obj_dict_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * args ) {
114
- mp_obj_t dict_out = mp_obj_new_dict (0 );
113
+ // This is a helper function to initialize an empty, but typed dictionary with
114
+ // a given number of slots.
115
+ STATIC mp_obj_t dict_new_typed (const mp_obj_type_t * type , const size_t n ) {
116
+ mp_obj_t dict_out = mp_obj_new_dict (n );
115
117
mp_obj_dict_t * dict = MP_OBJ_TO_PTR (dict_out );
116
118
dict -> base .type = type ;
117
119
#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
118
120
if (type == & mp_type_ordereddict ) {
119
121
dict -> map .is_ordered = 1 ;
120
122
}
121
123
#endif
124
+ return dict_out ;
125
+ }
126
+
127
+ mp_obj_t mp_obj_dict_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * args ) {
128
+ mp_obj_t dict_out = dict_new_typed (type , 0 );
122
129
if (n_args > 0 || n_kw > 0 ) {
123
130
mp_obj_t args2 [2 ] = {dict_out , args [0 ]}; // args[0] is always valid, even if it's not a positional arg
124
131
mp_map_t kwargs ;
@@ -264,6 +271,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, mp_obj_dict_copy);
264
271
#if MICROPY_PY_BUILTINS_DICT_FROMKEYS
265
272
// this is a classmethod
266
273
STATIC mp_obj_t dict_fromkeys (size_t n_args , const mp_obj_t * args ) {
274
+ mp_obj_type_t * type = MP_OBJ_TO_PTR (args [0 ]);
267
275
mp_obj_t iter = mp_getiter (args [1 ], NULL );
268
276
mp_obj_t value = mp_const_none ;
269
277
mp_obj_t next = MP_OBJ_NULL ;
@@ -277,9 +285,9 @@ STATIC mp_obj_t dict_fromkeys(size_t n_args, const mp_obj_t *args) {
277
285
mp_obj_t len = mp_obj_len_maybe (args [1 ]);
278
286
if (len == MP_OBJ_NULL ) {
279
287
/* object's type doesn't have a __len__ slot */
280
- self_out = mp_obj_new_dict ( 0 );
288
+ self_out = dict_new_typed ( type , 0 );
281
289
} else {
282
- self_out = mp_obj_new_dict ( MP_OBJ_SMALL_INT_VALUE (len ));
290
+ self_out = dict_new_typed ( type , MP_OBJ_SMALL_INT_VALUE (len ));
283
291
}
284
292
285
293
mp_obj_dict_t * self = MP_OBJ_TO_PTR (self_out );
0 commit comments