try 2. make namedtuple types handle subclasses

This commit is contained in:
Scott Shawcroft 2019-12-13 16:00:04 -08:00
parent 7f7105d36d
commit d8e66a5f32
No known key found for this signature in database
GPG Key ID: 9349BC7E64B1921E
3 changed files with 14 additions and 2 deletions

View File

@ -31,6 +31,7 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "py/objstr.h" #include "py/objstr.h"
#include "py/objnamedtuple.h" #include "py/objnamedtuple.h"
#include "py/objtype.h"
#include "supervisor/shared/translate.h" #include "supervisor/shared/translate.h"
@ -70,6 +71,15 @@ void namedtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t ki
mp_obj_attrtuple_print_helper(print, fields, &o->tuple); mp_obj_attrtuple_print_helper(print, fields, &o->tuple);
} }
mp_obj_t namedtuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
mp_obj_type_t *type = mp_obj_get_type(self_in);
// Check for subclasses of namedtuple and unpack if needed.
if (type->parent != &mp_type_tuple) {
self_in = ((mp_obj_instance_t*) self_in)->subobj[0];
}
return mp_obj_tuple_subscr(self_in, index, value);
}
void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
if (dest[0] == MP_OBJ_NULL) { if (dest[0] == MP_OBJ_NULL) {
// load attribute // load attribute
@ -167,7 +177,7 @@ STATIC mp_obj_t mp_obj_new_namedtuple_type(qstr name, size_t n_fields, mp_obj_t
o->base.unary_op = mp_obj_tuple_unary_op; o->base.unary_op = mp_obj_tuple_unary_op;
o->base.binary_op = mp_obj_tuple_binary_op; o->base.binary_op = mp_obj_tuple_binary_op;
o->base.attr = namedtuple_attr; o->base.attr = namedtuple_attr;
o->base.subscr = mp_obj_tuple_subscr; o->base.subscr = namedtuple_subscr;
o->base.getiter = mp_obj_tuple_getiter; o->base.getiter = mp_obj_tuple_getiter;
o->base.parent = &mp_type_tuple; o->base.parent = &mp_type_tuple;
return MP_OBJ_FROM_PTR(o); return MP_OBJ_FROM_PTR(o);

View File

@ -49,6 +49,7 @@ typedef struct _mp_obj_namedtuple_t {
void namedtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind); void namedtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind);
size_t mp_obj_namedtuple_find_field(const mp_obj_namedtuple_type_t *type, qstr name); size_t mp_obj_namedtuple_find_field(const mp_obj_namedtuple_type_t *type, qstr name);
mp_obj_t namedtuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest); void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
mp_obj_namedtuple_type_t *mp_obj_new_namedtuple_base(size_t n_fields, mp_obj_t *fields); mp_obj_namedtuple_type_t *mp_obj_new_namedtuple_base(size_t n_fields, mp_obj_t *fields);
mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args); mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args);

View File

@ -179,9 +179,10 @@ mp_obj_t mp_obj_tuple_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) {
#pragma GCC diagnostic ignored "-Wunused-parameter" #pragma GCC diagnostic ignored "-Wunused-parameter"
mp_obj_t mp_obj_tuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { mp_obj_t mp_obj_tuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
if (value == MP_OBJ_SENTINEL) { if (value == MP_OBJ_SENTINEL) {
// load // load
mp_obj_tuple_t *self = mp_instance_cast_to_native_base(self_in, MP_OBJ_FROM_PTR(&mp_type_tuple)); mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
#if MICROPY_PY_BUILTINS_SLICE #if MICROPY_PY_BUILTINS_SLICE
if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) { if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
mp_bound_slice_t slice; mp_bound_slice_t slice;