blob: 18931a16c29d37950c6a40b0dfe74fad604a156e [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
Paul Sokolovskyda9f0922014-05-13 08:44:45 +03007 * Copyright (c) 2014 Paul Sokolovsky
Damien George04b91472014-05-03 23:27:38 +01008 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020028#include <string.h>
29
Damien George51dfcb42015-01-01 20:27:54 +000030#include "py/nlr.h"
31#include "py/objtuple.h"
32#include "py/runtime.h"
Paul Sokolovsky44cd46a2015-03-23 22:44:16 +020033#include "py/objstr.h"
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020034
Damien Georgeee3fd462014-05-24 23:03:12 +010035#if MICROPY_PY_COLLECTIONS
Damien George107c9fb2014-04-26 10:31:15 +010036
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020037typedef struct _mp_obj_namedtuple_type_t {
38 mp_obj_type_t base;
stijn12340142014-12-20 16:37:40 +010039 mp_uint_t n_fields;
stijn6b636732015-01-21 13:22:41 +010040 qstr fields[];
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020041} mp_obj_namedtuple_type_t;
42
43typedef struct _mp_obj_namedtuple_t {
44 mp_obj_tuple_t tuple;
45} mp_obj_namedtuple_t;
46
Damien George5b3f0b72016-01-03 15:55:55 +000047STATIC mp_uint_t namedtuple_find_field(const mp_obj_namedtuple_type_t *type, qstr name) {
stijn12340142014-12-20 16:37:40 +010048 for (mp_uint_t i = 0; i < type->n_fields; i++) {
stijn6b636732015-01-21 13:22:41 +010049 if (type->fields[i] == name) {
stijn12340142014-12-20 16:37:40 +010050 return i;
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020051 }
52 }
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020053 return -1;
54}
55
Damien George7f9d1d62015-04-09 23:56:15 +010056STATIC void namedtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000057 (void)kind;
Damien George999cedb2015-11-27 17:01:44 +000058 mp_obj_namedtuple_t *o = MP_OBJ_TO_PTR(o_in);
Damien George5aa311d2015-04-21 14:14:24 +000059 mp_printf(print, "%q", o->tuple.base.type->name);
stijn6b636732015-01-21 13:22:41 +010060 const qstr *fields = ((mp_obj_namedtuple_type_t*)o->tuple.base.type)->fields;
Damien George5aa311d2015-04-21 14:14:24 +000061 mp_obj_attrtuple_print_helper(print, fields, &o->tuple);
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020062}
63
Damien Georgeb1bbe962015-04-01 14:10:50 +000064STATIC void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
65 if (dest[0] == MP_OBJ_NULL) {
66 // load attribute
Damien George999cedb2015-11-27 17:01:44 +000067 mp_obj_namedtuple_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgeb1bbe962015-04-01 14:10:50 +000068 int id = namedtuple_find_field((mp_obj_namedtuple_type_t*)self->tuple.base.type, attr);
69 if (id == -1) {
70 return;
71 }
72 dest[0] = self->tuple.items[id];
73 } else {
74 // delete/store attribute
75 // provide more detailed error message than we'd get by just returning
Damien George7d0d7212016-10-17 12:17:37 +110076 mp_raise_msg(&mp_type_AttributeError, "can't set attribute");
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020077 }
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020078}
79
Damien George5b3f0b72016-01-03 15:55:55 +000080STATIC 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) {
81 const mp_obj_namedtuple_type_t *type = (const mp_obj_namedtuple_type_t*)type_in;
Damien George2a1cca22016-03-14 22:40:39 +000082 size_t num_fields = type->n_fields;
stijn12340142014-12-20 16:37:40 +010083 if (n_args + n_kw != num_fields) {
Damien George7f233842015-01-01 15:33:50 +000084 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
85 mp_arg_error_terse_mismatch();
Damien George84e0cf02015-01-01 15:43:25 +000086 } else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL) {
Damien George7f233842015-01-01 15:33:50 +000087 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Damien George84e0cf02015-01-01 15:43:25 +000088 "function takes %d positional arguments but %d were given",
89 num_fields, n_args + n_kw));
90 } else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED) {
91 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Damien George044c4732015-04-11 13:03:37 +010092 "%q() takes %d positional arguments but %d were given",
93 type->base.name, num_fields, n_args + n_kw));
Damien George7f233842015-01-01 15:33:50 +000094 }
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020095 }
stijn021dc442014-12-20 16:38:40 +010096
97 mp_obj_t *arg_objects;
98 if (n_args == num_fields) {
99 arg_objects = (mp_obj_t*)args;
100 } else {
101 size_t alloc_size = sizeof(mp_obj_t) * num_fields;
102 arg_objects = alloca(alloc_size);
103 memset(arg_objects, 0, alloc_size);
104
105 for (mp_uint_t i = 0; i < n_args; i++) {
106 arg_objects[i] = args[i];
107 }
108
109 for (mp_uint_t i = n_args; i < n_args + 2 * n_kw; i += 2) {
stijn6b636732015-01-21 13:22:41 +0100110 qstr kw = mp_obj_str_get_qstr(args[i]);
stijn021dc442014-12-20 16:38:40 +0100111 int id = namedtuple_find_field(type, kw);
112 if (id == -1) {
Damien George7f233842015-01-01 15:33:50 +0000113 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
114 mp_arg_error_terse_mismatch();
115 } else {
116 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Damien George044c4732015-04-11 13:03:37 +0100117 "unexpected keyword argument '%q'", kw));
Damien George7f233842015-01-01 15:33:50 +0000118 }
stijn021dc442014-12-20 16:38:40 +0100119 }
Damien George999cedb2015-11-27 17:01:44 +0000120 if (arg_objects[id] != MP_OBJ_NULL) {
Damien George7f233842015-01-01 15:33:50 +0000121 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
122 mp_arg_error_terse_mismatch();
123 } else {
124 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Damien George044c4732015-04-11 13:03:37 +0100125 "function got multiple values for argument '%q'", kw));
Damien George7f233842015-01-01 15:33:50 +0000126 }
stijn021dc442014-12-20 16:38:40 +0100127 }
128 arg_objects[id] = args[i + 1];
129 }
130 }
131
Damien George999cedb2015-11-27 17:01:44 +0000132 mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(num_fields, arg_objects));
Damien George5b3f0b72016-01-03 15:55:55 +0000133 tuple->base.type = type_in;
Damien George999cedb2015-11-27 17:01:44 +0000134 return MP_OBJ_FROM_PTR(tuple);
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200135}
136
Damien Georgecbf76742015-11-27 13:38:15 +0000137STATIC const mp_rom_obj_tuple_t namedtuple_base_tuple = {{&mp_type_tuple}, 1, {MP_ROM_PTR(&mp_type_tuple)}};
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200138
stijn12340142014-12-20 16:37:40 +0100139STATIC mp_obj_t mp_obj_new_namedtuple_type(qstr name, mp_uint_t n_fields, mp_obj_t *fields) {
stijn6b636732015-01-21 13:22:41 +0100140 mp_obj_namedtuple_type_t *o = m_new_obj_var(mp_obj_namedtuple_type_t, qstr, n_fields);
Paul Sokolovsky276159e2015-01-01 15:31:51 +0200141 memset(&o->base, 0, sizeof(o->base));
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200142 o->base.base.type = &mp_type_type;
143 o->base.name = name;
144 o->base.print = namedtuple_print;
145 o->base.make_new = namedtuple_make_new;
Damien George2323ef92014-05-11 18:00:45 +0100146 o->base.unary_op = mp_obj_tuple_unary_op;
147 o->base.binary_op = mp_obj_tuple_binary_op;
Damien Georgeb1bbe962015-04-01 14:10:50 +0000148 o->base.attr = namedtuple_attr;
Damien George2323ef92014-05-11 18:00:45 +0100149 o->base.subscr = mp_obj_tuple_subscr;
150 o->base.getiter = mp_obj_tuple_getiter;
Damien George999cedb2015-11-27 17:01:44 +0000151 o->base.bases_tuple = (mp_obj_tuple_t*)(mp_rom_obj_tuple_t*)&namedtuple_base_tuple;
stijn12340142014-12-20 16:37:40 +0100152 o->n_fields = n_fields;
stijn6b636732015-01-21 13:22:41 +0100153 for (mp_uint_t i = 0; i < n_fields; i++) {
154 o->fields[i] = mp_obj_str_get_qstr(fields[i]);
155 }
Damien George999cedb2015-11-27 17:01:44 +0000156 return MP_OBJ_FROM_PTR(o);
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200157}
158
159STATIC mp_obj_t new_namedtuple_type(mp_obj_t name_in, mp_obj_t fields_in) {
stijn6b636732015-01-21 13:22:41 +0100160 qstr name = mp_obj_str_get_qstr(name_in);
stijn12340142014-12-20 16:37:40 +0100161 mp_uint_t n_fields;
162 mp_obj_t *fields;
Paul Sokolovsky44cd46a2015-03-23 22:44:16 +0200163 #if MICROPY_CPYTHON_COMPAT
164 if (MP_OBJ_IS_STR(fields_in)) {
165 fields_in = mp_obj_str_split(1, &fields_in);
166 }
167 #endif
Antonin ENFRUNca41dc22016-05-22 19:28:04 +0200168 mp_obj_get_array(fields_in, &n_fields, &fields);
stijn12340142014-12-20 16:37:40 +0100169 return mp_obj_new_namedtuple_type(name, n_fields, fields);
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200170}
171MP_DEFINE_CONST_FUN_OBJ_2(mp_namedtuple_obj, new_namedtuple_type);
Damien George107c9fb2014-04-26 10:31:15 +0100172
Damien Georgeee3fd462014-05-24 23:03:12 +0100173#endif // MICROPY_PY_COLLECTIONS