Skip to content

Commit a3edeb9

Browse files
committed
py/objdict: Fix optimisation for allocating result in fromkeys.
Iterables don't respond to __len__, so call __len__ on the original argument.
1 parent e9404e5 commit a3edeb9

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

py/objdict.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,16 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy);
250250
// this is a classmethod
251251
STATIC mp_obj_t dict_fromkeys(size_t n_args, const mp_obj_t *args) {
252252
mp_obj_t iter = mp_getiter(args[1]);
253-
mp_obj_t len = mp_obj_len_maybe(iter);
254253
mp_obj_t value = mp_const_none;
255254
mp_obj_t next = MP_OBJ_NULL;
256-
mp_obj_t self_out;
257255

258256
if (n_args > 2) {
259257
value = args[2];
260258
}
261259

260+
// optimisation to allocate result based on len of argument
261+
mp_obj_t self_out;
262+
mp_obj_t len = mp_obj_len_maybe(args[1]);
262263
if (len == MP_OBJ_NULL) {
263264
/* object's type doesn't have a __len__ slot */
264265
self_out = mp_obj_new_dict(0);

0 commit comments

Comments
 (0)