blob: d9ceea817ea1df0b68171f1b6dae6209712752b8 [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
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020030#include "mpconfig.h"
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030031#include "misc.h"
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020032#include "nlr.h"
33#include "qstr.h"
34#include "obj.h"
35#include "objtuple.h"
Damien George7f233842015-01-01 15:33:50 +000036#include "runtime.h"
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020037
Damien Georgeee3fd462014-05-24 23:03:12 +010038#if MICROPY_PY_COLLECTIONS
Damien George107c9fb2014-04-26 10:31:15 +010039
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020040typedef struct _mp_obj_namedtuple_type_t {
41 mp_obj_type_t base;
stijn12340142014-12-20 16:37:40 +010042 mp_uint_t n_fields;
43 mp_obj_t fields[];
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020044} mp_obj_namedtuple_type_t;
45
46typedef struct _mp_obj_namedtuple_t {
47 mp_obj_tuple_t tuple;
48} mp_obj_namedtuple_t;
49
stijn12340142014-12-20 16:37:40 +010050STATIC mp_uint_t namedtuple_find_field(mp_obj_namedtuple_type_t *type, qstr name) {
51 for (mp_uint_t i = 0; i < type->n_fields; i++) {
52 if (MP_OBJ_QSTR_VALUE(type->fields[i]) == name) {
53 return i;
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020054 }
55 }
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020056 return -1;
57}
58
59STATIC void namedtuple_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
60 mp_obj_namedtuple_t *o = o_in;
61 print(env, "%s(", qstr_str(o->tuple.base.type->name));
stijn12340142014-12-20 16:37:40 +010062 const mp_obj_t *fields = ((mp_obj_namedtuple_type_t*)o->tuple.base.type)->fields;
Damien George42f3de92014-10-03 17:44:14 +000063 for (mp_uint_t i = 0; i < o->tuple.len; i++) {
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020064 if (i > 0) {
Damien George42f3de92014-10-03 17:44:14 +000065 print(env, ", ");
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020066 }
stijn12340142014-12-20 16:37:40 +010067 print(env, "%s=", qstr_str(MP_OBJ_QSTR_VALUE(fields[i])));
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020068 mp_obj_print_helper(print, env, o->tuple.items[i], PRINT_REPR);
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020069 }
70 print(env, ")");
71}
72
73STATIC void namedtuple_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
74 mp_obj_namedtuple_t *self = self_in;
stijn12340142014-12-20 16:37:40 +010075 int id = namedtuple_find_field((mp_obj_namedtuple_type_t*)self->tuple.base.type, attr);
76 if (id == -1) {
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020077 return;
78 }
79 dest[0] = self->tuple.items[id];
80}
81
82STATIC bool namedtuple_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
Damien Georgeea13f402014-04-05 18:32:08 +010083 nlr_raise(mp_obj_new_exception_msg(&mp_type_AttributeError, "can't set attribute"));
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020084}
85
Damien Georgeecc88e92014-08-30 00:35:11 +010086STATIC 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 +020087 mp_obj_namedtuple_type_t *type = type_in;
stijn12340142014-12-20 16:37:40 +010088 mp_uint_t num_fields = type->n_fields;
89 if (n_args + n_kw != num_fields) {
Damien George7f233842015-01-01 15:33:50 +000090 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
91 mp_arg_error_terse_mismatch();
92 } else {
93 // Counts include implicit "self"
94 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
95 "__new__() takes %d positional arguments but %d were given",
96 num_fields + 1, n_args + n_kw + 1));
97 }
Paul Sokolovskyd08fd682014-02-27 22:22:04 +020098 }
stijn021dc442014-12-20 16:38:40 +010099
100 mp_obj_t *arg_objects;
101 if (n_args == num_fields) {
102 arg_objects = (mp_obj_t*)args;
103 } else {
104 size_t alloc_size = sizeof(mp_obj_t) * num_fields;
105 arg_objects = alloca(alloc_size);
106 memset(arg_objects, 0, alloc_size);
107
108 for (mp_uint_t i = 0; i < n_args; i++) {
109 arg_objects[i] = args[i];
110 }
111
112 for (mp_uint_t i = n_args; i < n_args + 2 * n_kw; i += 2) {
113 qstr kw = MP_OBJ_QSTR_VALUE(args[i]);
114 int id = namedtuple_find_field(type, kw);
115 if (id == -1) {
Damien George7f233842015-01-01 15:33:50 +0000116 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
117 mp_arg_error_terse_mismatch();
118 } else {
119 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
120 "__new__() got an unexpected keyword argument '%s'",
121 qstr_str(kw)));
122 }
stijn021dc442014-12-20 16:38:40 +0100123 }
124 if (arg_objects[id] != NULL) {
Damien George7f233842015-01-01 15:33:50 +0000125 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
126 mp_arg_error_terse_mismatch();
127 } else {
128 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
129 "__new__() got multiple values for argument '%s'",
130 qstr_str(kw)));
131 }
stijn021dc442014-12-20 16:38:40 +0100132 }
133 arg_objects[id] = args[i + 1];
134 }
135 }
136
137 mp_obj_tuple_t *tuple = mp_obj_new_tuple(num_fields, arg_objects);
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200138 tuple->base.type = type_in;
139 return tuple;
140}
141
Damien George07ddab52014-03-29 13:15:08 +0000142STATIC 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 +0200143
stijn12340142014-12-20 16:37:40 +0100144STATIC mp_obj_t mp_obj_new_namedtuple_type(qstr name, mp_uint_t n_fields, mp_obj_t *fields) {
145 mp_obj_namedtuple_type_t *o = m_new_obj_var(mp_obj_namedtuple_type_t, mp_obj_t, n_fields);
Paul Sokolovsky276159e2015-01-01 15:31:51 +0200146 memset(&o->base, 0, sizeof(o->base));
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200147 o->base.base.type = &mp_type_type;
148 o->base.name = name;
149 o->base.print = namedtuple_print;
150 o->base.make_new = namedtuple_make_new;
Damien George2323ef92014-05-11 18:00:45 +0100151 o->base.unary_op = mp_obj_tuple_unary_op;
152 o->base.binary_op = mp_obj_tuple_binary_op;
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200153 o->base.load_attr = namedtuple_load_attr;
154 o->base.store_attr = namedtuple_store_attr;
Damien George2323ef92014-05-11 18:00:45 +0100155 o->base.subscr = mp_obj_tuple_subscr;
156 o->base.getiter = mp_obj_tuple_getiter;
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200157 o->base.bases_tuple = (mp_obj_t)&namedtuple_base_tuple;
stijn12340142014-12-20 16:37:40 +0100158 o->n_fields = n_fields;
159 memcpy(o->fields, fields, sizeof(mp_obj_t) * n_fields);
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200160 return o;
161}
162
163STATIC mp_obj_t new_namedtuple_type(mp_obj_t name_in, mp_obj_t fields_in) {
164 qstr name = MP_OBJ_QSTR_VALUE(name_in);
stijn12340142014-12-20 16:37:40 +0100165 mp_uint_t n_fields;
166 mp_obj_t *fields;
167 mp_obj_list_get(fields_in, &n_fields, &fields);
168 return mp_obj_new_namedtuple_type(name, n_fields, fields);
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200169}
170MP_DEFINE_CONST_FUN_OBJ_2(mp_namedtuple_obj, new_namedtuple_type);
Damien George107c9fb2014-04-26 10:31:15 +0100171
Damien Georgeee3fd462014-05-24 23:03:12 +0100172#endif // MICROPY_PY_COLLECTIONS