2015-04-21 10:14:24 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2015-04-21 10:14:24 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2015 Damien P. George
|
2015-04-21 10:14:24 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/objtuple.h"
|
|
|
|
|
|
|
|
#if MICROPY_PY_ATTRTUPLE || MICROPY_PY_COLLECTIONS
|
|
|
|
|
|
|
|
// this helper function is used by collections.namedtuple
|
|
|
|
#if !MICROPY_PY_COLLECTIONS
|
|
|
|
STATIC
|
|
|
|
#endif
|
|
|
|
void mp_obj_attrtuple_print_helper(const mp_print_t *print, const qstr *fields, mp_obj_tuple_t *o) {
|
|
|
|
mp_print_str(print, "(");
|
2017-02-16 00:09:51 -05:00
|
|
|
for (size_t i = 0; i < o->len; i++) {
|
2015-04-21 10:14:24 -04:00
|
|
|
if (i > 0) {
|
|
|
|
mp_print_str(print, ", ");
|
|
|
|
}
|
|
|
|
mp_printf(print, "%q=", fields[i]);
|
|
|
|
mp_obj_print_helper(print, o->items[i], PRINT_REPR);
|
|
|
|
}
|
|
|
|
mp_print_str(print, ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MICROPY_PY_ATTRTUPLE
|
|
|
|
|
|
|
|
STATIC void mp_obj_attrtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
|
|
|
|
(void)kind;
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_tuple_t *o = MP_OBJ_TO_PTR(o_in);
|
2021-03-15 09:57:36 -04:00
|
|
|
const qstr *fields = (const qstr *)MP_OBJ_TO_PTR(o->items[o->len]);
|
2015-04-21 10:14:24 -04:00
|
|
|
mp_obj_attrtuple_print_helper(print, fields, o);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void mp_obj_attrtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
|
|
|
|
if (dest[0] == MP_OBJ_NULL) {
|
|
|
|
// load attribute
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
|
2017-02-16 00:09:51 -05:00
|
|
|
size_t len = self->len;
|
2021-03-15 09:57:36 -04:00
|
|
|
const qstr *fields = (const qstr *)MP_OBJ_TO_PTR(self->items[len]);
|
2017-02-16 00:09:51 -05:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
2015-04-21 10:14:24 -04:00
|
|
|
if (fields[i] == attr) {
|
|
|
|
dest[0] = self->items[i];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 00:09:51 -05:00
|
|
|
mp_obj_t mp_obj_new_attrtuple(const qstr *fields, size_t n, const mp_obj_t *items) {
|
2015-04-28 19:17:48 -04:00
|
|
|
mp_obj_tuple_t *o = m_new_obj_var(mp_obj_tuple_t, mp_obj_t, n + 1);
|
2015-04-21 10:14:24 -04:00
|
|
|
o->base.type = &mp_type_attrtuple;
|
2015-04-28 19:17:48 -04:00
|
|
|
o->len = n;
|
2017-02-16 00:09:51 -05:00
|
|
|
for (size_t i = 0; i < n; i++) {
|
2015-04-21 10:14:24 -04:00
|
|
|
o->items[i] = items[i];
|
|
|
|
}
|
2015-11-27 12:01:44 -05:00
|
|
|
o->items[n] = MP_OBJ_FROM_PTR(fields);
|
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2015-04-21 10:14:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const mp_obj_type_t mp_type_attrtuple = {
|
|
|
|
{ &mp_type_type },
|
2021-07-06 10:25:56 -04:00
|
|
|
.flags = MP_TYPE_FLAG_FULL,
|
2015-04-21 10:14:24 -04:00
|
|
|
.name = MP_QSTR_tuple, // reuse tuple to save on a qstr
|
|
|
|
.print = mp_obj_attrtuple_print,
|
|
|
|
.attr = mp_obj_attrtuple_attr,
|
2021-07-06 11:37:32 -04:00
|
|
|
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,
|
|
|
|
),
|
2015-04-21 10:14:24 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MICROPY_PY_ATTRTUPLE
|