allow definition of namedtuples in core

This commit is contained in:
Jeff Epler 2023-09-22 13:56:16 -05:00
parent a181172336
commit e48984872b
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
4 changed files with 32 additions and 51 deletions

View File

@ -52,6 +52,35 @@ 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_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args);
// CIRCUITPY yikes
#define NAMEDTUPLE_TYPE_BASE_AND_SLOTS_MAKE_NEW(type_name, make_new_fun) \
.base = { \
.base = { .type = &mp_type_type }, \
.flags = MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, \
.name = type_name, \
.slot_index_make_new = 1, \
.slot_index_print = 2, \
.slot_index_unary_op = 3, \
.slot_index_binary_op = 4, \
.slot_index_attr = 5, \
.slot_index_subscr = 6, \
.slot_index_iter = 7, \
.slot_index_parent = 8, \
}, \
.slots = { \
make_new_fun, \
namedtuple_print, \
mp_obj_tuple_unary_op, \
mp_obj_tuple_binary_op, \
namedtuple_attr, \
mp_obj_tuple_subscr, \
mp_obj_tuple_getiter, \
(void *)&mp_type_tuple, \
}
#define NAMEDTUPLE_TYPE_BASE_AND_SLOTS(type_name) \
NAMEDTUPLE_TYPE_BASE_AND_SLOTS_MAKE_NEW(type_name, namedtuple_make_new)
#endif // MICROPY_PY_COLLECTIONS
#endif // MICROPY_INCLUDED_PY_OBJNAMEDTUPLE_H

View File

@ -40,23 +40,7 @@
//|
const mp_obj_namedtuple_type_t qrio_qrinfo_type_obj = {
.base = {
.base = {
.type = &mp_type_type
},
.flags = MP_TYPE_FLAG_EXTENDED,
.name = MP_QSTR_QRInfo,
.print = namedtuple_print,
.parent = &mp_type_tuple,
.make_new = namedtuple_make_new,
.attr = namedtuple_attr,
MP_TYPE_EXTENDED_FIELDS(
.unary_op = mp_obj_tuple_unary_op,
.binary_op = mp_obj_tuple_binary_op,
.subscr = mp_obj_tuple_subscr,
.getiter = mp_obj_tuple_getiter,
),
},
NAMEDTUPLE_TYPE_BASE_AND_SLOTS(MP_QSTR_QRInfo),
.n_fields = 2,
.fields = {
MP_QSTR_payload,

View File

@ -82,23 +82,7 @@ STATIC mp_obj_t synthio_biquad_make_new(const mp_obj_type_t *type_in, size_t n_a
}
const mp_obj_namedtuple_type_t synthio_biquad_type_obj = {
.base = {
.base = {
.type = &mp_type_type
},
.flags = MP_TYPE_FLAG_EXTENDED,
.name = MP_QSTR_Biquad,
.print = namedtuple_print,
.parent = &mp_type_tuple,
.make_new = synthio_biquad_make_new,
.attr = namedtuple_attr,
MP_TYPE_EXTENDED_FIELDS(
.unary_op = mp_obj_tuple_unary_op,
.binary_op = mp_obj_tuple_binary_op,
.subscr = mp_obj_tuple_subscr,
.getiter = mp_obj_tuple_getiter,
),
},
NAMEDTUPLE_TYPE_BASE_AND_SLOTS_MAKE_NEW(MP_QSTR_Biquad, synthio_biquad_make_new),
.n_fields = 5,
.fields = {
MP_QSTR_a1,

View File

@ -171,23 +171,7 @@ STATIC mp_obj_t synthio_envelope_make_new(const mp_obj_type_t *type_in, size_t n
};
const mp_obj_namedtuple_type_t synthio_envelope_type_obj = {
.base = {
.base = {
.type = &mp_type_type
},
.flags = MP_TYPE_FLAG_EXTENDED,
.name = MP_QSTR_Envelope,
.print = namedtuple_print,
.parent = &mp_type_tuple,
.make_new = synthio_envelope_make_new,
.attr = namedtuple_attr,
MP_TYPE_EXTENDED_FIELDS(
.unary_op = mp_obj_tuple_unary_op,
.binary_op = mp_obj_tuple_binary_op,
.subscr = mp_obj_tuple_subscr,
.getiter = mp_obj_tuple_getiter,
),
},
NAMEDTUPLE_TYPE_BASE_AND_SLOTS_MAKE_NEW(MP_QSTR_Envelope, synthio_envelope_make_new),
.n_fields = 5,
.fields = {
MP_QSTR_attack_time,