@@ -376,6 +376,61 @@ static mp_obj_t dict_values(mp_obj_t self_in) {
376376}
377377static MP_DEFINE_CONST_FUN_OBJ_1 (dict_values_obj , dict_values ) ;
378378
379+
380+ /******************************************************************************/
381+ /* dict metaclass */
382+
383+ static mp_obj_t dict_fromkeys (int n_args , const mp_obj_t * args ) {
384+ assert (2 <= n_args && n_args <= 3 );
385+ mp_obj_t iter = rt_getiter (args [1 ]);
386+ mp_obj_t len = mp_obj_len_maybe (iter );
387+ mp_obj_t value = mp_const_none ;
388+ mp_obj_t next = NULL ;
389+ mp_obj_dict_t * self = NULL ;
390+
391+ if (n_args > 2 ) {
392+ value = args [2 ];
393+ }
394+
395+ if (len == NULL ) {
396+ /* object's type doesn't have a __len__ slot */
397+ self = mp_obj_new_dict (0 );
398+ } else {
399+ self = mp_obj_new_dict (MP_OBJ_SMALL_INT_VALUE (len ));
400+ }
401+
402+ while ((next = rt_iternext (iter )) != mp_const_stop_iteration ) {
403+ mp_map_lookup (& self -> map , next , MP_MAP_LOOKUP_ADD_IF_NOT_FOUND )-> value = value ;
404+ }
405+
406+ return self ;
407+ }
408+ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (dict_fromkeys_obj , 2 , 3 , dict_fromkeys ) ;
409+
410+ static const mp_method_t dict_class_methods [] = {
411+ { "fromkeys" , & dict_fromkeys_obj },
412+ { NULL , NULL }, // end-of-list sentinel
413+ };
414+
415+ /* this should be unnecessary when inheritance works */
416+ static void dict_class_print (void (* print )(void * env , const char * fmt , ...), void * env , mp_obj_t self_in ) {
417+ print (env , "<class 'dict'>" );
418+ }
419+
420+ /* this should be unnecessary when inheritance works */
421+ static mp_obj_t dict_class_call_n (mp_obj_t self_in , int n_args , const mp_obj_t * args ) {
422+ return rt_build_map (0 );
423+ }
424+
425+ static const mp_obj_type_t dict_class = {
426+ { & mp_const_type },
427+ "dict_class" ,
428+ .print = dict_class_print ,
429+ .methods = dict_class_methods ,
430+ .call_n = dict_class_call_n ,
431+ };
432+
433+
379434/******************************************************************************/
380435/* dict constructors & etc */
381436
@@ -394,7 +449,7 @@ static const mp_method_t dict_type_methods[] = {
394449};
395450
396451const mp_obj_type_t dict_type = {
397- { & mp_const_type },
452+ { & dict_class },
398453 "dict" ,
399454 .print = dict_print ,
400455 .make_new = dict_make_new ,
0 commit comments