34 #if MICROPY_PY_COLLECTIONS 36 typedef struct _mp_obj_namedtuple_type_t {
40 } mp_obj_namedtuple_type_t;
42 typedef struct _mp_obj_namedtuple_t {
44 } mp_obj_namedtuple_t;
46 STATIC size_t namedtuple_find_field(
const mp_obj_namedtuple_type_t *type,
qstr name) {
47 for (
size_t i = 0; i < type->n_fields; i++) {
48 if (type->fields[i] == name) {
58 mp_printf(print,
"%q", o->tuple.base.type->name);
59 const qstr *fields = ((mp_obj_namedtuple_type_t*)o->tuple.base.type)->fields;
60 mp_obj_attrtuple_print_helper(print, fields, &o->tuple);
67 size_t id = namedtuple_find_field((mp_obj_namedtuple_type_t*)self->tuple.base.type,
attr);
68 if (
id == (
size_t)-1) {
71 dest[0] =
self->tuple.items[id];
80 const mp_obj_namedtuple_type_t *type = (
const mp_obj_namedtuple_type_t*)type_in;
81 size_t num_fields = type->n_fields;
82 if (n_args + n_kw != num_fields) {
87 "function takes %d positional arguments but %d were given",
88 num_fields, n_args + n_kw));
91 "%q() takes %d positional arguments but %d were given",
92 type->
base.name, num_fields, n_args + n_kw));
98 tuple->
base.type = type_in;
105 for (
size_t i = n_args; i < n_args + 2 * n_kw; i += 2) {
107 size_t id = namedtuple_find_field(type, kw);
108 if (
id == (
size_t)-1) {
113 "unexpected keyword argument '%q'", kw));
121 "function got multiple values for argument '%q'", kw));
131 mp_obj_namedtuple_type_t *o =
m_new_obj_var(mp_obj_namedtuple_type_t,
qstr, n_fields);
132 memset(&o->base, 0,
sizeof(o->base));
135 o->base.print = namedtuple_print;
136 o->base.make_new = namedtuple_make_new;
139 o->base.attr = namedtuple_attr;
143 o->n_fields = n_fields;
144 for (
size_t i = 0; i < n_fields; i++) {
154 #if MICROPY_CPYTHON_COMPAT 160 return mp_obj_new_namedtuple_type(name, n_fields, fields);
164 #endif // MICROPY_PY_COLLECTIONS qstr mp_obj_str_get_qstr(mp_obj_t self_in)
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg)
void * memset(void *b, int c, size_t len)
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_new_tuple(size_t n, const mp_obj_t *items)
#define MICROPY_ERROR_REPORTING_TERSE
const mp_obj_type_t mp_type_TypeError
mp_obj_t mp_obj_str_split(size_t n_args, const mp_obj_t *args)
STATIC const uint8_t attr[]
mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt,...)
mp_obj_t mp_obj_tuple_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs)
#define MP_OBJ_FROM_PTR(p)
#define MICROPY_ERROR_REPORTING_DETAILED
void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items)
#define MICROPY_ERROR_REPORTING
#define m_new_obj_var(obj_type, var_type, var_num)
mp_obj_t mp_obj_tuple_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf)
#define MICROPY_ERROR_REPORTING_NORMAL
const mp_obj_type_t mp_type_type
const mp_obj_type_t mp_type_AttributeError
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name)
const mp_obj_type_t mp_type_tuple
NORETURN void mp_arg_error_terse_mismatch(void)
int mp_printf(const mp_print_t *print, const char *fmt,...)
mp_obj_t mp_obj_tuple_unary_op(mp_unary_op_t op, mp_obj_t self_in)
void * memcpy(void *dst, const void *src, size_t n)