blob: 1996b412194131a9a427869b5f345a875d54e77c [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
stijn12340142014-12-20 16:37:40 +010047STATIC mp_uint_t namedtuple_find_field(mp_obj_namedtuple_type_t *type, qstr name) {
48 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
56STATIC void namedtuple_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000057 (void)kind;
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020058 mp_obj_namedtuple_t *o = o_in;
59 print(env, "%s(", qstr_str(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 George42f3de92014-10-03 17:44:14 +000061 for (mp_uint_t i = 0; i < o->tuple.len; i++) {
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020062 if (i > 0) {
Damien George42f3de92014-10-03 17:44:14 +000063 print(env, ", ");
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020064 }
stijn6b636732015-01-21 13:22:41 +010065 print(env, "%s=", qstr_str(fields[i]));
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020066 mp_obj_print_helper(print, env, o->tuple.items[i], PRINT_REPR);
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020067 }
68 print(env, ")");
69}
70
71STATIC void namedtuple_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
72 mp_obj_namedtuple_t *self = self_in;
stijn12340142014-12-20 16:37:40 +010073 int id = namedtuple_find_field((mp_obj_namedtuple_type_t*)self->tuple.base.type, attr);
74 if (id == -1) {
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020075 return;
76 }
77 dest[0] = self->tuple.items[id];
78}
79
80STATIC bool namedtuple_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000081 (void)self_in;
82 (void)attr;
83 (void)value;
Damien Georgeea13f402014-04-05 18:32:08 +010084 nlr_raise(mp_obj_new_exception_msg(&mp_type_AttributeError, "can't set attribute"));
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020085}
86
Damien Georgeecc88e92014-08-30 00:35:11 +010087STATIC mp_obj_t namedtuple_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020088 mp_obj_namedtuple_type_t *type = type_in;
stijn12340142014-12-20 16:37:40 +010089 mp_uint_t num_fields = type->n_fields;
90 if (n_args + n_kw != num_fields) {
Damien George7f233842015-01-01 15:33:50 +000091 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
92 mp_arg_error_terse_mismatch();
Damien George84e0cf02015-01-01 15:43:25 +000093 } else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL) {
Damien George7f233842015-01-01 15:33:50 +000094 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Damien George84e0cf02015-01-01 15:43:25 +000095 "function takes %d positional arguments but %d were given",
96 num_fields, n_args + n_kw));
97 } else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED) {
98 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
99 "%s() takes %d positional arguments but %d were given",
100 qstr_str(type->base.name), num_fields, n_args + n_kw));
Damien George7f233842015-01-01 15:33:50 +0000101 }
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200102 }
stijn021dc442014-12-20 16:38:40 +0100103
104 mp_obj_t *arg_objects;
105 if (n_args == num_fields) {
106 arg_objects = (mp_obj_t*)args;
107 } else {
108 size_t alloc_size = sizeof(mp_obj_t) * num_fields;
109 arg_objects = alloca(alloc_size);
110 memset(arg_objects, 0, alloc_size);
111
112 for (mp_uint_t i = 0; i < n_args; i++) {
113 arg_objects[i] = args[i];
114 }
115
116 for (mp_uint_t i = n_args; i < n_args + 2 * n_kw; i += 2) {
stijn6b636732015-01-21 13:22:41 +0100117 qstr kw = mp_obj_str_get_qstr(args[i]);
stijn021dc442014-12-20 16:38:40 +0100118 int id = namedtuple_find_field(type, kw);
119 if (id == -1) {
Damien George7f233842015-01-01 15:33:50 +0000120 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
121 mp_arg_error_terse_mismatch();
122 } else {
123 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Damien George84e0cf02015-01-01 15:43:25 +0000124 "unexpected keyword argument '%s'",
Damien George7f233842015-01-01 15:33:50 +0000125 qstr_str(kw)));
126 }
stijn021dc442014-12-20 16:38:40 +0100127 }
128 if (arg_objects[id] != NULL) {
Damien George7f233842015-01-01 15:33:50 +0000129 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
130 mp_arg_error_terse_mismatch();
131 } else {
132 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Damien George84e0cf02015-01-01 15:43:25 +0000133 "function got multiple values for argument '%s'",
Damien George7f233842015-01-01 15:33:50 +0000134 qstr_str(kw)));
135 }
stijn021dc442014-12-20 16:38:40 +0100136 }
137 arg_objects[id] = args[i + 1];
138 }
139 }
140
141 mp_obj_tuple_t *tuple = mp_obj_new_tuple(num_fields, arg_objects);
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200142 tuple->base.type = type_in;
143 return tuple;
144}
145
Damien George07ddab52014-03-29 13:15:08 +0000146STATIC const mp_obj_tuple_t namedtuple_base_tuple = {{&mp_type_tuple}, 1, {(mp_obj_t)&mp_type_tuple}};
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200147
stijn12340142014-12-20 16:37:40 +0100148STATIC 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 +0100149 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 +0200150 memset(&o->base, 0, sizeof(o->base));
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200151 o->base.base.type = &mp_type_type;
152 o->base.name = name;
153 o->base.print = namedtuple_print;
154 o->base.make_new = namedtuple_make_new;
Damien George2323ef92014-05-11 18:00:45 +0100155 o->base.unary_op = mp_obj_tuple_unary_op;
156 o->base.binary_op = mp_obj_tuple_binary_op;
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200157 o->base.load_attr = namedtuple_load_attr;
158 o->base.store_attr = namedtuple_store_attr;
Damien George2323ef92014-05-11 18:00:45 +0100159 o->base.subscr = mp_obj_tuple_subscr;
160 o->base.getiter = mp_obj_tuple_getiter;
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200161 o->base.bases_tuple = (mp_obj_t)&namedtuple_base_tuple;
stijn12340142014-12-20 16:37:40 +0100162 o->n_fields = n_fields;
stijn6b636732015-01-21 13:22:41 +0100163 for (mp_uint_t i = 0; i < n_fields; i++) {
164 o->fields[i] = mp_obj_str_get_qstr(fields[i]);
165 }
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200166 return o;
167}
168
169STATIC mp_obj_t new_namedtuple_type(mp_obj_t name_in, mp_obj_t fields_in) {
stijn6b636732015-01-21 13:22:41 +0100170 qstr name = mp_obj_str_get_qstr(name_in);
stijn12340142014-12-20 16:37:40 +0100171 mp_uint_t n_fields;
172 mp_obj_t *fields;
Paul Sokolovsky44cd46a2015-03-23 22:44:16 +0200173 #if MICROPY_CPYTHON_COMPAT
174 if (MP_OBJ_IS_STR(fields_in)) {
175 fields_in = mp_obj_str_split(1, &fields_in);
176 }
177 #endif
Paul Sokolovskye38b8922015-03-22 22:10:03 +0200178 if (!MP_OBJ_IS_TYPE(fields_in, &mp_type_list)) {
179 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "list required"));
180 }
stijn12340142014-12-20 16:37:40 +0100181 mp_obj_list_get(fields_in, &n_fields, &fields);
182 return mp_obj_new_namedtuple_type(name, n_fields, fields);
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200183}
184MP_DEFINE_CONST_FUN_OBJ_2(mp_namedtuple_obj, new_namedtuple_type);
Damien George107c9fb2014-04-26 10:31:15 +0100185
Damien Georgeee3fd462014-05-24 23:03:12 +0100186#endif // MICROPY_PY_COLLECTIONS