|
|
@ -36,7 +36,7 @@ |
|
|
|
|
|
|
|
typedef struct _mp_obj_namedtuple_type_t { |
|
|
|
mp_obj_type_t base; |
|
|
|
mp_uint_t n_fields; |
|
|
|
size_t n_fields; |
|
|
|
qstr fields[]; |
|
|
|
} mp_obj_namedtuple_type_t; |
|
|
|
|
|
|
@ -44,13 +44,13 @@ typedef struct _mp_obj_namedtuple_t { |
|
|
|
mp_obj_tuple_t tuple; |
|
|
|
} mp_obj_namedtuple_t; |
|
|
|
|
|
|
|
STATIC mp_uint_t namedtuple_find_field(const mp_obj_namedtuple_type_t *type, qstr name) { |
|
|
|
for (mp_uint_t i = 0; i < type->n_fields; i++) { |
|
|
|
STATIC size_t namedtuple_find_field(const mp_obj_namedtuple_type_t *type, qstr name) { |
|
|
|
for (size_t i = 0; i < type->n_fields; i++) { |
|
|
|
if (type->fields[i] == name) { |
|
|
|
return i; |
|
|
|
} |
|
|
|
} |
|
|
|
return -1; |
|
|
|
return (size_t)-1; |
|
|
|
} |
|
|
|
|
|
|
|
STATIC void namedtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) { |
|
|
@ -65,8 +65,8 @@ STATIC void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { |
|
|
|
if (dest[0] == MP_OBJ_NULL) { |
|
|
|
// load attribute
|
|
|
|
mp_obj_namedtuple_t *self = MP_OBJ_TO_PTR(self_in); |
|
|
|
int id = namedtuple_find_field((mp_obj_namedtuple_type_t*)self->tuple.base.type, attr); |
|
|
|
if (id == -1) { |
|
|
|
size_t id = namedtuple_find_field((mp_obj_namedtuple_type_t*)self->tuple.base.type, attr); |
|
|
|
if (id == (size_t)-1) { |
|
|
|
return; |
|
|
|
} |
|
|
|
dest[0] = self->tuple.items[id]; |
|
|
@ -102,14 +102,14 @@ STATIC mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, |
|
|
|
arg_objects = alloca(alloc_size); |
|
|
|
memset(arg_objects, 0, alloc_size); |
|
|
|
|
|
|
|
for (mp_uint_t i = 0; i < n_args; i++) { |
|
|
|
for (size_t i = 0; i < n_args; i++) { |
|
|
|
arg_objects[i] = args[i]; |
|
|
|
} |
|
|
|
|
|
|
|
for (mp_uint_t i = n_args; i < n_args + 2 * n_kw; i += 2) { |
|
|
|
for (size_t i = n_args; i < n_args + 2 * n_kw; i += 2) { |
|
|
|
qstr kw = mp_obj_str_get_qstr(args[i]); |
|
|
|
int id = namedtuple_find_field(type, kw); |
|
|
|
if (id == -1) { |
|
|
|
size_t id = namedtuple_find_field(type, kw); |
|
|
|
if (id == (size_t)-1) { |
|
|
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
|
|
|
mp_arg_error_terse_mismatch(); |
|
|
|
} else { |
|
|
@ -136,7 +136,7 @@ STATIC mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, |
|
|
|
|
|
|
|
STATIC const mp_rom_obj_tuple_t namedtuple_base_tuple = {{&mp_type_tuple}, 1, {MP_ROM_PTR(&mp_type_tuple)}}; |
|
|
|
|
|
|
|
STATIC mp_obj_t mp_obj_new_namedtuple_type(qstr name, mp_uint_t n_fields, mp_obj_t *fields) { |
|
|
|
STATIC mp_obj_t mp_obj_new_namedtuple_type(qstr name, size_t n_fields, mp_obj_t *fields) { |
|
|
|
mp_obj_namedtuple_type_t *o = m_new_obj_var(mp_obj_namedtuple_type_t, qstr, n_fields); |
|
|
|
memset(&o->base, 0, sizeof(o->base)); |
|
|
|
o->base.base.type = &mp_type_type; |
|
|
@ -150,7 +150,7 @@ STATIC mp_obj_t mp_obj_new_namedtuple_type(qstr name, mp_uint_t n_fields, mp_obj |
|
|
|
o->base.getiter = mp_obj_tuple_getiter; |
|
|
|
o->base.bases_tuple = (mp_obj_tuple_t*)(mp_rom_obj_tuple_t*)&namedtuple_base_tuple; |
|
|
|
o->n_fields = n_fields; |
|
|
|
for (mp_uint_t i = 0; i < n_fields; i++) { |
|
|
|
for (size_t i = 0; i < n_fields; i++) { |
|
|
|
o->fields[i] = mp_obj_str_get_qstr(fields[i]); |
|
|
|
} |
|
|
|
return MP_OBJ_FROM_PTR(o); |
|
|
|