objnamedtuple.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #include <string.h>
  28. #include "py/objtuple.h"
  29. #include "py/runtime.h"
  30. #include "py/objstr.h"
  31. #include "py/objnamedtuple.h"
  32. #if MICROPY_PY_COLLECTIONS
  33. size_t mp_obj_namedtuple_find_field(const mp_obj_namedtuple_type_t *type, qstr name) {
  34. for (size_t i = 0; i < type->n_fields; i++) {
  35. if (type->fields[i] == name) {
  36. return i;
  37. }
  38. }
  39. return (size_t)-1;
  40. }
  41. #if MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT
  42. STATIC mp_obj_t namedtuple_asdict(mp_obj_t self_in) {
  43. mp_obj_namedtuple_t *self = MP_OBJ_TO_PTR(self_in);
  44. const qstr *fields = ((mp_obj_namedtuple_type_t*)self->tuple.base.type)->fields;
  45. mp_obj_t dict = mp_obj_new_dict(self->tuple.len);
  46. //make it an OrderedDict
  47. mp_obj_dict_t *dictObj = MP_OBJ_TO_PTR(dict);
  48. dictObj->base.type = &mp_type_ordereddict;
  49. dictObj->map.is_ordered = 1;
  50. for (size_t i = 0; i < self->tuple.len; ++i) {
  51. mp_obj_dict_store(dict, MP_OBJ_NEW_QSTR(fields[i]), self->tuple.items[i]);
  52. }
  53. return dict;
  54. }
  55. MP_DEFINE_CONST_FUN_OBJ_1(namedtuple_asdict_obj, namedtuple_asdict);
  56. #endif
  57. STATIC void namedtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
  58. (void)kind;
  59. mp_obj_namedtuple_t *o = MP_OBJ_TO_PTR(o_in);
  60. mp_printf(print, "%q", o->tuple.base.type->name);
  61. const qstr *fields = ((mp_obj_namedtuple_type_t*)o->tuple.base.type)->fields;
  62. mp_obj_attrtuple_print_helper(print, fields, &o->tuple);
  63. }
  64. STATIC void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  65. if (dest[0] == MP_OBJ_NULL) {
  66. // load attribute
  67. mp_obj_namedtuple_t *self = MP_OBJ_TO_PTR(self_in);
  68. #if MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT
  69. if (attr == MP_QSTR__asdict) {
  70. dest[0] = MP_OBJ_FROM_PTR(&namedtuple_asdict_obj);
  71. dest[1] = self_in;
  72. return;
  73. }
  74. #endif
  75. size_t id = mp_obj_namedtuple_find_field((mp_obj_namedtuple_type_t*)self->tuple.base.type, attr);
  76. if (id == (size_t)-1) {
  77. return;
  78. }
  79. dest[0] = self->tuple.items[id];
  80. } else {
  81. // delete/store attribute
  82. // provide more detailed error message than we'd get by just returning
  83. mp_raise_msg(&mp_type_AttributeError, "can't set attribute");
  84. }
  85. }
  86. 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) {
  87. const mp_obj_namedtuple_type_t *type = (const mp_obj_namedtuple_type_t*)type_in;
  88. size_t num_fields = type->n_fields;
  89. if (n_args + n_kw != num_fields) {
  90. if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
  91. mp_arg_error_terse_mismatch();
  92. } else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL) {
  93. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
  94. "function takes %d positional arguments but %d were given",
  95. num_fields, n_args + n_kw));
  96. } else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED) {
  97. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
  98. "%q() takes %d positional arguments but %d were given",
  99. type->base.name, num_fields, n_args + n_kw));
  100. }
  101. }
  102. // Create a tuple and set the type to this namedtuple
  103. mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(num_fields, NULL));
  104. tuple->base.type = type_in;
  105. // Copy the positional args into the first slots of the namedtuple
  106. memcpy(&tuple->items[0], args, sizeof(mp_obj_t) * n_args);
  107. // Fill in the remaining slots with the keyword args
  108. memset(&tuple->items[n_args], 0, sizeof(mp_obj_t) * n_kw);
  109. for (size_t i = n_args; i < n_args + 2 * n_kw; i += 2) {
  110. qstr kw = mp_obj_str_get_qstr(args[i]);
  111. size_t id = mp_obj_namedtuple_find_field(type, kw);
  112. if (id == (size_t)-1) {
  113. 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,
  117. "unexpected keyword argument '%q'", kw));
  118. }
  119. }
  120. if (tuple->items[id] != MP_OBJ_NULL) {
  121. 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,
  125. "function got multiple values for argument '%q'", kw));
  126. }
  127. }
  128. tuple->items[id] = args[i + 1];
  129. }
  130. return MP_OBJ_FROM_PTR(tuple);
  131. }
  132. mp_obj_namedtuple_type_t *mp_obj_new_namedtuple_base(size_t n_fields, mp_obj_t *fields) {
  133. mp_obj_namedtuple_type_t *o = m_new_obj_var(mp_obj_namedtuple_type_t, qstr, n_fields);
  134. memset(&o->base, 0, sizeof(o->base));
  135. o->n_fields = n_fields;
  136. for (size_t i = 0; i < n_fields; i++) {
  137. o->fields[i] = mp_obj_str_get_qstr(fields[i]);
  138. }
  139. return o;
  140. }
  141. STATIC mp_obj_t mp_obj_new_namedtuple_type(qstr name, size_t n_fields, mp_obj_t *fields) {
  142. mp_obj_namedtuple_type_t *o = mp_obj_new_namedtuple_base(n_fields, fields);
  143. o->base.base.type = &mp_type_type;
  144. o->base.name = name;
  145. o->base.print = namedtuple_print;
  146. o->base.make_new = namedtuple_make_new;
  147. o->base.unary_op = mp_obj_tuple_unary_op;
  148. o->base.binary_op = mp_obj_tuple_binary_op;
  149. o->base.attr = namedtuple_attr;
  150. o->base.subscr = mp_obj_tuple_subscr;
  151. o->base.getiter = mp_obj_tuple_getiter;
  152. o->base.parent = &mp_type_tuple;
  153. return MP_OBJ_FROM_PTR(o);
  154. }
  155. STATIC mp_obj_t new_namedtuple_type(mp_obj_t name_in, mp_obj_t fields_in) {
  156. qstr name = mp_obj_str_get_qstr(name_in);
  157. size_t n_fields;
  158. mp_obj_t *fields;
  159. #if MICROPY_CPYTHON_COMPAT
  160. if (MP_OBJ_IS_STR(fields_in)) {
  161. fields_in = mp_obj_str_split(1, &fields_in);
  162. }
  163. #endif
  164. mp_obj_get_array(fields_in, &n_fields, &fields);
  165. return mp_obj_new_namedtuple_type(name, n_fields, fields);
  166. }
  167. MP_DEFINE_CONST_FUN_OBJ_2(mp_namedtuple_obj, new_namedtuple_type);
  168. #endif // MICROPY_PY_COLLECTIONS