py/objnamedtuple: Allow to reuse namedtuple basic functionality.
By declaring interface in objnamedtuple.h and introducing a helper allocation function.
This commit is contained in:
parent
da154fdaf9
commit
8667a5f053
|
@ -30,20 +30,11 @@
|
|||
#include "py/objtuple.h"
|
||||
#include "py/runtime.h"
|
||||
#include "py/objstr.h"
|
||||
#include "py/objnamedtuple.h"
|
||||
|
||||
#if MICROPY_PY_COLLECTIONS
|
||||
|
||||
typedef struct _mp_obj_namedtuple_type_t {
|
||||
mp_obj_type_t base;
|
||||
size_t n_fields;
|
||||
qstr fields[];
|
||||
} mp_obj_namedtuple_type_t;
|
||||
|
||||
typedef struct _mp_obj_namedtuple_t {
|
||||
mp_obj_tuple_t tuple;
|
||||
} mp_obj_namedtuple_t;
|
||||
|
||||
STATIC size_t 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) {
|
||||
for (size_t i = 0; i < type->n_fields; i++) {
|
||||
if (type->fields[i] == name) {
|
||||
return i;
|
||||
|
@ -88,7 +79,7 @@ STATIC void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
|
|||
return;
|
||||
}
|
||||
#endif
|
||||
size_t id = namedtuple_find_field((mp_obj_namedtuple_type_t*)self->tuple.base.type, attr);
|
||||
size_t id = mp_obj_namedtuple_find_field((mp_obj_namedtuple_type_t*)self->tuple.base.type, attr);
|
||||
if (id == (size_t)-1) {
|
||||
return;
|
||||
}
|
||||
|
@ -128,7 +119,7 @@ STATIC mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args,
|
|||
memset(&tuple->items[n_args], 0, sizeof(mp_obj_t) * n_kw);
|
||||
for (size_t i = n_args; i < n_args + 2 * n_kw; i += 2) {
|
||||
qstr kw = mp_obj_str_get_qstr(args[i]);
|
||||
size_t id = namedtuple_find_field(type, kw);
|
||||
size_t id = mp_obj_namedtuple_find_field(type, kw);
|
||||
if (id == (size_t)-1) {
|
||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||
mp_arg_error_terse_mismatch();
|
||||
|
@ -151,9 +142,18 @@ STATIC mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args,
|
|||
return MP_OBJ_FROM_PTR(tuple);
|
||||
}
|
||||
|
||||
STATIC mp_obj_t mp_obj_new_namedtuple_type(qstr name, 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_namedtuple_type_t *o = m_new_obj_var(mp_obj_namedtuple_type_t, qstr, n_fields);
|
||||
memset(&o->base, 0, sizeof(o->base));
|
||||
o->n_fields = n_fields;
|
||||
for (size_t i = 0; i < n_fields; i++) {
|
||||
o->fields[i] = mp_obj_str_get_qstr(fields[i]);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
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 = mp_obj_new_namedtuple_base(n_fields, fields);
|
||||
o->base.base.type = &mp_type_type;
|
||||
o->base.name = name;
|
||||
o->base.print = namedtuple_print;
|
||||
|
@ -164,10 +164,6 @@ STATIC mp_obj_t mp_obj_new_namedtuple_type(qstr name, size_t n_fields, mp_obj_t
|
|||
o->base.subscr = mp_obj_tuple_subscr;
|
||||
o->base.getiter = mp_obj_tuple_getiter;
|
||||
o->base.parent = &mp_type_tuple;
|
||||
o->n_fields = n_fields;
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2017 Paul Sokolovsky
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
#ifndef MICROPY_INCLUDED_PY_OBJNAMEDTUPLE_H
|
||||
#define MICROPY_INCLUDED_PY_OBJNAMEDTUPLE_H
|
||||
|
||||
#include "py/objtuple.h"
|
||||
|
||||
typedef struct _mp_obj_namedtuple_type_t {
|
||||
mp_obj_type_t base;
|
||||
size_t n_fields;
|
||||
qstr fields[];
|
||||
} mp_obj_namedtuple_type_t;
|
||||
|
||||
typedef struct _mp_obj_namedtuple_t {
|
||||
mp_obj_tuple_t tuple;
|
||||
} mp_obj_namedtuple_t;
|
||||
|
||||
size_t mp_obj_namedtuple_find_field(const mp_obj_namedtuple_type_t *type, qstr name);
|
||||
mp_obj_namedtuple_type_t *mp_obj_new_namedtuple_base(size_t n_fields, mp_obj_t *fields);
|
||||
|
||||
#endif // MICROPY_INCLUDED_PY_OBJNAMEDTUPLE_H
|
Loading…
Reference in New Issue