Skip to content

Commit 0090c71

Browse files
committed
objdict: Cast mp_obj_t to concrete types explicitly.
Continuation of refactoring applied previously to objlist.
1 parent 2d717ad commit 0090c71

1 file changed

Lines changed: 58 additions & 51 deletions

File tree

py/objdict.c

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ STATIC mp_map_elem_t *dict_iter_next(mp_obj_dict_t *dict, mp_uint_t *cur) {
5656
}
5757

5858
STATIC void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
59-
mp_obj_dict_t *self = self_in;
59+
mp_obj_dict_t *self = MP_OBJ_CAST(self_in);
6060
bool first = true;
6161
if (!(MICROPY_PY_UJSON && kind == PRINT_JSON)) {
6262
kind = PRINT_REPR;
@@ -83,24 +83,25 @@ STATIC void dict_print(void (*print)(void *env, const char *fmt, ...), void *env
8383
}
8484

8585
STATIC mp_obj_t dict_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
86-
mp_obj_dict_t *dict = mp_obj_new_dict(0);
87-
dict->base.type = type_in;
86+
mp_obj_t dict_out = mp_obj_new_dict(0);
87+
mp_obj_dict_t *dict = MP_OBJ_CAST(dict_out);
88+
dict->base.type = MP_OBJ_CAST(type_in);
8889
#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
89-
if (type_in == &mp_type_ordereddict) {
90+
if (MP_OBJ_CAST(type_in) == &mp_type_ordereddict) {
9091
dict->map.is_ordered = 1;
9192
}
9293
#endif
9394
if (n_args > 0 || n_kw > 0) {
94-
mp_obj_t args2[2] = {dict, args[0]}; // args[0] is always valid, even if it's not a positional arg
95+
mp_obj_t args2[2] = {dict_out, args[0]}; // args[0] is always valid, even if it's not a positional arg
9596
mp_map_t kwargs;
9697
mp_map_init_fixed_table(&kwargs, n_kw, args + n_args);
9798
dict_update(n_args + 1, args2, &kwargs); // dict_update will check that n_args + 1 == 1 or 2
9899
}
99-
return dict;
100+
return dict_out;
100101
}
101102

102103
STATIC mp_obj_t dict_unary_op(mp_uint_t op, mp_obj_t self_in) {
103-
mp_obj_dict_t *self = self_in;
104+
mp_obj_dict_t *self = MP_OBJ_CAST(self_in);
104105
switch (op) {
105106
case MP_UNARY_OP_BOOL: return MP_BOOL(self->map.used != 0);
106107
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->map.used);
@@ -109,7 +110,7 @@ STATIC mp_obj_t dict_unary_op(mp_uint_t op, mp_obj_t self_in) {
109110
}
110111

111112
STATIC mp_obj_t dict_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
112-
mp_obj_dict_t *o = lhs_in;
113+
mp_obj_dict_t *o = MP_OBJ_CAST(lhs_in);
113114
switch (op) {
114115
case MP_BINARY_OP_IN: {
115116
mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
@@ -123,7 +124,7 @@ STATIC mp_obj_t dict_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
123124
} else
124125
#endif
125126
if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_dict)) {
126-
mp_obj_dict_t *rhs = rhs_in;
127+
mp_obj_dict_t *rhs = MP_OBJ_CAST(rhs_in);
127128
if (o->map.used != rhs->map.used) {
128129
return mp_const_false;
129130
}
@@ -150,7 +151,7 @@ STATIC mp_obj_t dict_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
150151

151152
// TODO: Make sure this is inlined in dict_subscr() below.
152153
mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index) {
153-
mp_obj_dict_t *self = self_in;
154+
mp_obj_dict_t *self = MP_OBJ_CAST(self_in);
154155
mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
155156
if (elem == NULL) {
156157
nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
@@ -166,7 +167,7 @@ STATIC mp_obj_t dict_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
166167
return mp_const_none;
167168
} else if (value == MP_OBJ_SENTINEL) {
168169
// load
169-
mp_obj_dict_t *self = self_in;
170+
mp_obj_dict_t *self = MP_OBJ_CAST(self_in);
170171
mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
171172
if (elem == NULL) {
172173
nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
@@ -185,13 +186,13 @@ STATIC mp_obj_t dict_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
185186

186187
typedef struct _mp_obj_dict_it_t {
187188
mp_obj_base_t base;
188-
mp_obj_dict_t *dict;
189+
mp_obj_t dict;
189190
mp_uint_t cur;
190191
} mp_obj_dict_it_t;
191192

192193
STATIC mp_obj_t dict_it_iternext(mp_obj_t self_in) {
193-
mp_obj_dict_it_t *self = self_in;
194-
mp_map_elem_t *next = dict_iter_next(self->dict, &self->cur);
194+
mp_obj_dict_it_t *self = MP_OBJ_CAST(self_in);
195+
mp_map_elem_t *next = dict_iter_next(MP_OBJ_CAST(self->dict), &self->cur);
195196

196197
if (next == NULL) {
197198
return MP_OBJ_STOP_ITERATION;
@@ -207,20 +208,21 @@ STATIC const mp_obj_type_t mp_type_dict_it = {
207208
.iternext = dict_it_iternext,
208209
};
209210

210-
STATIC mp_obj_t dict_getiter(mp_obj_t o_in) {
211-
mp_obj_dict_it_t *o = m_new_obj(mp_obj_dict_it_t);
211+
STATIC mp_obj_t dict_getiter(mp_obj_t self_in) {
212+
mp_obj_t o_out = m_new_obj(mp_obj_dict_it_t);
213+
mp_obj_dict_it_t *o = MP_OBJ_CAST(o_out);
212214
o->base.type = &mp_type_dict_it;
213-
o->dict = o_in;
215+
o->dict = self_in;
214216
o->cur = 0;
215-
return o;
217+
return o_out;
216218
}
217219

218220
/******************************************************************************/
219221
/* dict methods */
220222

221223
STATIC mp_obj_t dict_clear(mp_obj_t self_in) {
222224
assert(MP_OBJ_IS_DICT_TYPE(self_in));
223-
mp_obj_dict_t *self = self_in;
225+
mp_obj_dict_t *self = MP_OBJ_CAST(self_in);
224226

225227
mp_map_clear(&self->map);
226228

@@ -230,15 +232,16 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear);
230232

231233
STATIC mp_obj_t dict_copy(mp_obj_t self_in) {
232234
assert(MP_OBJ_IS_DICT_TYPE(self_in));
233-
mp_obj_dict_t *self = self_in;
234-
mp_obj_dict_t *other = mp_obj_new_dict(self->map.alloc);
235+
mp_obj_dict_t *self = MP_OBJ_CAST(self_in);
236+
mp_obj_t other_out = mp_obj_new_dict(self->map.alloc);
237+
mp_obj_dict_t *other = MP_OBJ_CAST(other_out);
235238
other->base.type = self->base.type;
236239
other->map.used = self->map.used;
237240
other->map.all_keys_are_qstrs = self->map.all_keys_are_qstrs;
238241
other->map.is_fixed = 0;
239242
other->map.is_ordered = self->map.is_ordered;
240243
memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
241-
return other;
244+
return other_out;
242245
}
243246
STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy);
244247

@@ -249,24 +252,25 @@ STATIC mp_obj_t dict_fromkeys(mp_uint_t n_args, const mp_obj_t *args) {
249252
mp_obj_t len = mp_obj_len_maybe(iter);
250253
mp_obj_t value = mp_const_none;
251254
mp_obj_t next = NULL;
252-
mp_obj_dict_t *self = NULL;
255+
mp_obj_t self_out;
253256

254257
if (n_args > 2) {
255258
value = args[2];
256259
}
257260

258261
if (len == MP_OBJ_NULL) {
259262
/* object's type doesn't have a __len__ slot */
260-
self = mp_obj_new_dict(0);
263+
self_out = mp_obj_new_dict(0);
261264
} else {
262-
self = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
265+
self_out = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
263266
}
264267

268+
mp_obj_dict_t *self = MP_OBJ_CAST(self_out);
265269
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
266270
mp_map_lookup(&self->map, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
267271
}
268272

269-
return self;
273+
return self_out;
270274
}
271275
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_fromkeys_fun_obj, 2, 3, dict_fromkeys);
272276
STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(dict_fromkeys_obj, (const mp_obj_t)&dict_fromkeys_fun_obj);
@@ -333,7 +337,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setde
333337

334338
STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
335339
assert(MP_OBJ_IS_DICT_TYPE(self_in));
336-
mp_obj_dict_t *self = self_in;
340+
mp_obj_dict_t *self = MP_OBJ_CAST(self_in);
337341
mp_uint_t cur = 0;
338342
mp_map_elem_t *next = dict_iter_next(self, &cur);
339343
if (next == NULL) {
@@ -351,7 +355,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem);
351355

352356
STATIC mp_obj_t dict_update(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
353357
assert(MP_OBJ_IS_DICT_TYPE(args[0]));
354-
mp_obj_dict_t *self = args[0];
358+
mp_obj_dict_t *self = MP_OBJ_CAST(args[0]);
355359

356360
mp_arg_check_num(n_args, kwargs->used, 1, 2, true);
357361

@@ -360,7 +364,7 @@ STATIC mp_obj_t dict_update(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw
360364

361365
if (MP_OBJ_IS_DICT_TYPE(args[1])) {
362366
// update from other dictionary (make sure other is not self)
363-
if (args[1] != self) {
367+
if (args[1] != args[0]) {
364368
mp_uint_t cur = 0;
365369
mp_map_elem_t *elem = NULL;
366370
while ((elem = dict_iter_next((mp_obj_dict_t*)args[1], &cur)) != NULL) {
@@ -418,20 +422,20 @@ STATIC char *mp_dict_view_names[] = {"dict_items", "dict_keys", "dict_values"};
418422
typedef struct _mp_obj_dict_view_it_t {
419423
mp_obj_base_t base;
420424
mp_dict_view_kind_t kind;
421-
mp_obj_dict_t *dict;
425+
mp_obj_t dict;
422426
mp_uint_t cur;
423427
} mp_obj_dict_view_it_t;
424428

425429
typedef struct _mp_obj_dict_view_t {
426430
mp_obj_base_t base;
427-
mp_obj_dict_t *dict;
431+
mp_obj_t dict;
428432
mp_dict_view_kind_t kind;
429433
} mp_obj_dict_view_t;
430434

431435
STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
432436
assert(MP_OBJ_IS_TYPE(self_in, &dict_view_it_type));
433-
mp_obj_dict_view_it_t *self = self_in;
434-
mp_map_elem_t *next = dict_iter_next(self->dict, &self->cur);
437+
mp_obj_dict_view_it_t *self = MP_OBJ_CAST(self_in);
438+
mp_map_elem_t *next = dict_iter_next(MP_OBJ_CAST(self->dict), &self->cur);
435439

436440
if (next == NULL) {
437441
return MP_OBJ_STOP_ITERATION;
@@ -459,24 +463,25 @@ STATIC const mp_obj_type_t dict_view_it_type = {
459463

460464
STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in) {
461465
assert(MP_OBJ_IS_TYPE(view_in, &dict_view_type));
462-
mp_obj_dict_view_t *view = view_in;
463-
mp_obj_dict_view_it_t *o = m_new_obj(mp_obj_dict_view_it_t);
466+
mp_obj_dict_view_t *view = MP_OBJ_CAST(view_in);
467+
mp_obj_t o_out = m_new_obj(mp_obj_dict_view_it_t);
468+
mp_obj_dict_view_it_t *o = MP_OBJ_CAST(o_out);
464469
o->base.type = &dict_view_it_type;
465470
o->kind = view->kind;
466471
o->dict = view->dict;
467472
o->cur = 0;
468-
return o;
473+
return o_out;
469474
}
470475

471476
STATIC void dict_view_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
472477
(void)kind;
473478
assert(MP_OBJ_IS_TYPE(self_in, &dict_view_type));
474-
mp_obj_dict_view_t *self = self_in;
479+
mp_obj_dict_view_t *self = MP_OBJ_CAST(self_in);
475480
bool first = true;
476481
print(env, mp_dict_view_names[self->kind]);
477482
print(env, "([");
478-
mp_obj_t *self_iter = dict_view_getiter(self);
479-
mp_obj_t *next = NULL;
483+
mp_obj_t self_iter = dict_view_getiter(self_in);
484+
mp_obj_t next = MP_OBJ_NULL;
480485
while ((next = dict_view_it_iternext(self_iter)) != MP_OBJ_STOP_ITERATION) {
481486
if (!first) {
482487
print(env, ", ");
@@ -489,7 +494,7 @@ STATIC void dict_view_print(void (*print)(void *env, const char *fmt, ...), void
489494

490495
STATIC mp_obj_t dict_view_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
491496
// only supported for the 'keys' kind until sets and dicts are refactored
492-
mp_obj_dict_view_t *o = lhs_in;
497+
mp_obj_dict_view_t *o = MP_OBJ_CAST(lhs_in);
493498
if (o->kind != MP_DICT_VIEW_KEYS) {
494499
return MP_OBJ_NULL; // op not supported
495500
}
@@ -507,18 +512,18 @@ STATIC const mp_obj_type_t dict_view_type = {
507512
.getiter = dict_view_getiter,
508513
};
509514

510-
STATIC mp_obj_t mp_obj_new_dict_view(mp_obj_dict_t *dict, mp_dict_view_kind_t kind) {
511-
mp_obj_dict_view_t *o = m_new_obj(mp_obj_dict_view_t);
515+
STATIC mp_obj_t mp_obj_new_dict_view(mp_obj_t dict, mp_dict_view_kind_t kind) {
516+
mp_obj_t o_out = m_new_obj(mp_obj_dict_view_t);
517+
mp_obj_dict_view_t *o = MP_OBJ_CAST(o_out);
512518
o->base.type = &dict_view_type;
513519
o->dict = dict;
514520
o->kind = kind;
515-
return o;
521+
return o_out;
516522
}
517523

518524
STATIC mp_obj_t dict_view(mp_obj_t self_in, mp_dict_view_kind_t kind) {
519525
assert(MP_OBJ_IS_DICT_TYPE(self_in));
520-
mp_obj_dict_t *self = self_in;
521-
return mp_obj_new_dict_view(self, kind);
526+
return mp_obj_new_dict_view(self_in, kind);
522527
}
523528

524529
STATIC mp_obj_t dict_items(mp_obj_t self_in) {
@@ -593,31 +598,33 @@ void mp_obj_dict_init(mp_obj_dict_t *dict, mp_uint_t n_args) {
593598
}
594599

595600
mp_obj_t mp_obj_new_dict(mp_uint_t n_args) {
596-
mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
601+
mp_obj_t o_out = m_new_obj(mp_obj_dict_t);
602+
mp_obj_dict_t *o = MP_OBJ_CAST(o_out);
597603
mp_obj_dict_init(o, n_args);
598-
return o;
604+
return o_out;
599605
}
600606

601607
mp_uint_t mp_obj_dict_len(mp_obj_t self_in) {
602-
return ((mp_obj_dict_t *)self_in)->map.used;
608+
mp_obj_dict_t *self = MP_OBJ_CAST(self_in);
609+
return self->map.used;
603610
}
604611

605612
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
606613
assert(MP_OBJ_IS_DICT_TYPE(self_in));
607-
mp_obj_dict_t *self = self_in;
614+
mp_obj_dict_t *self = MP_OBJ_CAST(self_in);
608615
mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
609616
return self_in;
610617
}
611618

612619
mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key) {
613620
assert(MP_OBJ_IS_DICT_TYPE(self_in));
614-
mp_obj_dict_t *self = self_in;
621+
mp_obj_dict_t *self = MP_OBJ_CAST(self_in);
615622
dict_get_helper(&self->map, key, MP_OBJ_NULL, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
616623
return self_in;
617624
}
618625

619626
mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in) {
620627
assert(MP_OBJ_IS_DICT_TYPE(self_in));
621-
mp_obj_dict_t *self = self_in;
628+
mp_obj_dict_t *self = MP_OBJ_CAST(self_in);
622629
return &self->map;
623630
}

0 commit comments

Comments
 (0)