Skip to content

Commit 945a01c

Browse files
committed
py: Fix bug in type_store_attr, trying to store to ROM.
1 parent bdcbf0f commit 945a01c

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

py/map.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ typedef struct _mp_map_elem_t {
33
mp_obj_t value;
44
} mp_map_elem_t;
55

6+
// TODO maybe have a truncated mp_map_t for fixed tables, since alloc=used
7+
// put alloc last in the structure, so the truncated version does not need it
8+
// this would save 1 ROM word for all ROM objects that have a locals_dict
9+
// would also need a trucated dict structure
10+
611
typedef struct _mp_map_t {
712
machine_uint_t all_keys_are_qstrs : 1;
813
machine_uint_t table_is_fixed_array : 1;

py/objtype.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,16 @@ STATIC bool type_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
325325

326326
if (self->locals_dict != NULL) {
327327
assert(MP_OBJ_IS_TYPE(self->locals_dict, &dict_type)); // Micro Python restriction, for now
328-
mp_map_t *locals_map = ((void*)self->locals_dict + sizeof(mp_obj_base_t)); // XXX hack to get map object from dict object
329-
mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
330-
return true;
331-
} else {
332-
return false;
328+
mp_map_t *locals_map = mp_obj_dict_get_map(self->locals_dict);
329+
mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
330+
// note that locals_map may be in ROM, so add will fail in that case
331+
if (elem != NULL) {
332+
elem->value = value;
333+
return true;
334+
}
333335
}
336+
337+
return false;
334338
}
335339

336340
const mp_obj_type_t mp_type_type = {

0 commit comments

Comments
 (0)