Numworks Epsilon  1.4.1
Graphing Calculator Operating System
objnamedtuple.c
Go to the documentation of this file.
1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2013, 2014 Damien P. George
7  * Copyright (c) 2014 Paul Sokolovsky
8  *
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 
28 #include <string.h>
29 
30 #include "py/objtuple.h"
31 #include "py/runtime.h"
32 #include "py/objstr.h"
33 
34 #if MICROPY_PY_COLLECTIONS
35 
36 typedef struct _mp_obj_namedtuple_type_t {
37  mp_obj_type_t base;
38  size_t n_fields;
39  qstr fields[];
40 } mp_obj_namedtuple_type_t;
41 
42 typedef struct _mp_obj_namedtuple_t {
43  mp_obj_tuple_t tuple;
44 } mp_obj_namedtuple_t;
45 
46 STATIC size_t namedtuple_find_field(const mp_obj_namedtuple_type_t *type, qstr name) {
47  for (size_t i = 0; i < type->n_fields; i++) {
48  if (type->fields[i] == name) {
49  return i;
50  }
51  }
52  return (size_t)-1;
53 }
54 
55 STATIC void namedtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
56  (void)kind;
57  mp_obj_namedtuple_t *o = MP_OBJ_TO_PTR(o_in);
58  mp_printf(print, "%q", o->tuple.base.type->name);
59  const qstr *fields = ((mp_obj_namedtuple_type_t*)o->tuple.base.type)->fields;
60  mp_obj_attrtuple_print_helper(print, fields, &o->tuple);
61 }
62 
63 STATIC void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
64  if (dest[0] == MP_OBJ_NULL) {
65  // load attribute
66  mp_obj_namedtuple_t *self = MP_OBJ_TO_PTR(self_in);
67  size_t id = namedtuple_find_field((mp_obj_namedtuple_type_t*)self->tuple.base.type, attr);
68  if (id == (size_t)-1) {
69  return;
70  }
71  dest[0] = self->tuple.items[id];
72  } else {
73  // delete/store attribute
74  // provide more detailed error message than we'd get by just returning
75  mp_raise_msg(&mp_type_AttributeError, "can't set attribute");
76  }
77 }
78 
79 STATIC 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) {
80  const mp_obj_namedtuple_type_t *type = (const mp_obj_namedtuple_type_t*)type_in;
81  size_t num_fields = type->n_fields;
82  if (n_args + n_kw != num_fields) {
87  "function takes %d positional arguments but %d were given",
88  num_fields, n_args + n_kw));
91  "%q() takes %d positional arguments but %d were given",
92  type->base.name, num_fields, n_args + n_kw));
93  }
94  }
95 
96  // Create a tuple and set the type to this namedtuple
97  mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(num_fields, NULL));
98  tuple->base.type = type_in;
99 
100  // Copy the positional args into the first slots of the namedtuple
101  memcpy(&tuple->items[0], args, sizeof(mp_obj_t) * n_args);
102 
103  // Fill in the remaining slots with the keyword args
104  memset(&tuple->items[n_args], 0, sizeof(mp_obj_t) * n_kw);
105  for (size_t i = n_args; i < n_args + 2 * n_kw; i += 2) {
106  qstr kw = mp_obj_str_get_qstr(args[i]);
107  size_t id = namedtuple_find_field(type, kw);
108  if (id == (size_t)-1) {
111  } else {
113  "unexpected keyword argument '%q'", kw));
114  }
115  }
116  if (tuple->items[id] != MP_OBJ_NULL) {
119  } else {
121  "function got multiple values for argument '%q'", kw));
122  }
123  }
124  tuple->items[id] = args[i + 1];
125  }
126 
127  return MP_OBJ_FROM_PTR(tuple);
128 }
129 
130 STATIC mp_obj_t mp_obj_new_namedtuple_type(qstr name, size_t n_fields, mp_obj_t *fields) {
131  mp_obj_namedtuple_type_t *o = m_new_obj_var(mp_obj_namedtuple_type_t, qstr, n_fields);
132  memset(&o->base, 0, sizeof(o->base));
133  o->base.base.type = &mp_type_type;
134  o->base.name = name;
135  o->base.print = namedtuple_print;
136  o->base.make_new = namedtuple_make_new;
137  o->base.unary_op = mp_obj_tuple_unary_op;
138  o->base.binary_op = mp_obj_tuple_binary_op;
139  o->base.attr = namedtuple_attr;
140  o->base.subscr = mp_obj_tuple_subscr;
141  o->base.getiter = mp_obj_tuple_getiter;
142  o->base.parent = &mp_type_tuple;
143  o->n_fields = n_fields;
144  for (size_t i = 0; i < n_fields; i++) {
145  o->fields[i] = mp_obj_str_get_qstr(fields[i]);
146  }
147  return MP_OBJ_FROM_PTR(o);
148 }
149 
150 STATIC mp_obj_t new_namedtuple_type(mp_obj_t name_in, mp_obj_t fields_in) {
151  qstr name = mp_obj_str_get_qstr(name_in);
152  size_t n_fields;
153  mp_obj_t *fields;
154  #if MICROPY_CPYTHON_COMPAT
155  if (MP_OBJ_IS_STR(fields_in)) {
156  fields_in = mp_obj_str_split(1, &fields_in);
157  }
158  #endif
159  mp_obj_get_array(fields_in, &n_fields, &fields);
160  return mp_obj_new_namedtuple_type(name, n_fields, fields);
161 }
162 MP_DEFINE_CONST_FUN_OBJ_2(mp_namedtuple_obj, new_namedtuple_type);
163 
164 #endif // MICROPY_PY_COLLECTIONS
qstr mp_obj_str_get_qstr(mp_obj_t self_in)
Definition: objstr.c:2082
mp_obj_base_t base
Definition: objtuple.h:32
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg)
Definition: runtime.c:1448
void * memset(void *b, int c, size_t len)
Definition: memset.c:3
mp_obj_t mp_obj_tuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value)
Definition: objtuple.c:177
mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items)
Definition: objtuple.c:235
#define MICROPY_ERROR_REPORTING_TERSE
Definition: mpconfig.h:521
const mp_obj_type_t mp_type_TypeError
mp_obj_t mp_obj_str_split(size_t n_args, const mp_obj_t *args)
Definition: objstr.c:484
STATIC const uint8_t attr[]
Definition: unicode.c:51
mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt,...)
Definition: objexcept.c:380
mp_obj_t mp_obj_tuple_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs)
Definition: objtuple.c:139
#define MP_OBJ_FROM_PTR(p)
Definition: obj.h:233
mp_obj_base_t base
Definition: obj.h:475
#define MICROPY_ERROR_REPORTING_DETAILED
Definition: mpconfig.h:525
#define STATIC
Definition: mpconfig.h:1178
mp_obj_t items[]
Definition: objtuple.h:34
void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items)
Definition: obj.c:346
mp_print_kind_t
Definition: obj.h:412
#define MICROPY_ERROR_REPORTING
Definition: mpconfigport.h:32
#define m_new_obj_var(obj_type, var_type, var_num)
Definition: misc.h:62
mp_obj_t mp_obj_tuple_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf)
Definition: objtuple.c:284
#define MICROPY_ERROR_REPORTING_NORMAL
Definition: mpconfig.h:523
#define NULL
Definition: stddef.h:4
#define MP_OBJ_NULL
Definition: obj.h:73
size_t qstr
Definition: qstr.h:48
args
Definition: i18n.py:175
const mp_obj_type_t mp_type_type
Definition: objtype.c:969
const mp_obj_type_t mp_type_AttributeError
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name)
Definition: obj.h:288
#define MP_OBJ_TO_PTR(o)
Definition: obj.h:228
const mp_obj_type_t mp_type_tuple
Definition: objtuple.c:220
#define nlr_raise(val)
Definition: nlr.h:89
NORETURN void mp_arg_error_terse_mismatch(void)
Definition: argcheck.c:136
uint64_t mp_obj_t
Definition: obj.h:39
int mp_printf(const mp_print_t *print, const char *fmt,...)
Definition: mpprint.c:380
#define MP_OBJ_IS_STR(o)
Definition: obj.h:256
mp_obj_t mp_obj_tuple_unary_op(mp_unary_op_t op, mp_obj_t self_in)
Definition: objtuple.c:122
void * memcpy(void *dst, const void *src, size_t n)
Definition: memcpy.c:3