Skip to content

Commit 9fbd12f

Browse files
pfalcondpgeorge
authored andcommitted
extmod/moductypes: Accept OrderedDict as a structure description.
Using OrderedDict (i.e. stable order of fields) would for example allow to automatically calculate field offsets in structures.
1 parent 6bda951 commit 9fbd12f

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

extmod/moductypes.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2014 Paul Sokolovsky
6+
* Copyright (c) 2014-2018 Paul Sokolovsky
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -137,7 +137,11 @@ STATIC void uctypes_struct_print(const mp_print_t *print, mp_obj_t self_in, mp_p
137137
(void)kind;
138138
mp_obj_uctypes_struct_t *self = MP_OBJ_TO_PTR(self_in);
139139
const char *typen = "unk";
140-
if (MP_OBJ_IS_TYPE(self->desc, &mp_type_dict)) {
140+
if (MP_OBJ_IS_TYPE(self->desc, &mp_type_dict)
141+
#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
142+
|| MP_OBJ_IS_TYPE(self->desc, &mp_type_ordereddict)
143+
#endif
144+
) {
141145
typen = "STRUCT";
142146
} else if (MP_OBJ_IS_TYPE(self->desc, &mp_type_tuple)) {
143147
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(self->desc);
@@ -206,7 +210,11 @@ STATIC mp_uint_t uctypes_struct_agg_size(mp_obj_tuple_t *t, int layout_type, mp_
206210
}
207211

208212
STATIC mp_uint_t uctypes_struct_size(mp_obj_t desc_in, int layout_type, mp_uint_t *max_field_size) {
209-
if (!MP_OBJ_IS_TYPE(desc_in, &mp_type_dict)) {
213+
if (!MP_OBJ_IS_TYPE(desc_in, &mp_type_dict)
214+
#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
215+
&& !MP_OBJ_IS_TYPE(desc_in, &mp_type_ordereddict)
216+
#endif
217+
) {
210218
if (MP_OBJ_IS_TYPE(desc_in, &mp_type_tuple)) {
211219
return uctypes_struct_agg_size((mp_obj_tuple_t*)MP_OBJ_TO_PTR(desc_in), layout_type, max_field_size);
212220
} else if (MP_OBJ_IS_SMALL_INT(desc_in)) {
@@ -390,8 +398,11 @@ STATIC void set_aligned(uint val_type, void *p, mp_int_t index, mp_obj_t val) {
390398
STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set_val) {
391399
mp_obj_uctypes_struct_t *self = MP_OBJ_TO_PTR(self_in);
392400

393-
// TODO: Support at least OrderedDict in addition
394-
if (!MP_OBJ_IS_TYPE(self->desc, &mp_type_dict)) {
401+
if (!MP_OBJ_IS_TYPE(self->desc, &mp_type_dict)
402+
#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
403+
&& !MP_OBJ_IS_TYPE(self->desc, &mp_type_ordereddict)
404+
#endif
405+
) {
395406
mp_raise_TypeError("struct: no fields");
396407
}
397408

0 commit comments

Comments
 (0)