objnamedtuple.c 6.3 KB

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