objtuple.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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-2017 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 <assert.h>
  29. #include "py/objtuple.h"
  30. #include "py/runtime.h"
  31. // type check is done on getiter method to allow tuple, namedtuple, attrtuple
  32. #define mp_obj_is_tuple_compatible(o) (mp_obj_get_type(o)->getiter == mp_obj_tuple_getiter)
  33. /******************************************************************************/
  34. /* tuple */
  35. void mp_obj_tuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
  36. mp_obj_tuple_t *o = MP_OBJ_TO_PTR(o_in);
  37. if (MICROPY_PY_UJSON && kind == PRINT_JSON) {
  38. mp_print_str(print, "[");
  39. } else {
  40. mp_print_str(print, "(");
  41. kind = PRINT_REPR;
  42. }
  43. for (size_t i = 0; i < o->len; i++) {
  44. if (i > 0) {
  45. mp_print_str(print, ", ");
  46. }
  47. mp_obj_print_helper(print, o->items[i], kind);
  48. }
  49. if (MICROPY_PY_UJSON && kind == PRINT_JSON) {
  50. mp_print_str(print, "]");
  51. } else {
  52. if (o->len == 1) {
  53. mp_print_str(print, ",");
  54. }
  55. mp_print_str(print, ")");
  56. }
  57. }
  58. STATIC mp_obj_t mp_obj_tuple_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  59. (void)type_in;
  60. mp_arg_check_num(n_args, n_kw, 0, 1, false);
  61. switch (n_args) {
  62. case 0:
  63. // return a empty tuple
  64. return mp_const_empty_tuple;
  65. case 1:
  66. default: {
  67. // 1 argument, an iterable from which we make a new tuple
  68. if (mp_obj_is_type(args[0], &mp_type_tuple)) {
  69. return args[0];
  70. }
  71. // TODO optimise for cases where we know the length of the iterator
  72. size_t alloc = 4;
  73. size_t len = 0;
  74. mp_obj_t *items = m_new(mp_obj_t, alloc);
  75. mp_obj_t iterable = mp_getiter(args[0], NULL);
  76. mp_obj_t item;
  77. while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
  78. if (len >= alloc) {
  79. items = m_renew(mp_obj_t, items, alloc, alloc * 2);
  80. alloc *= 2;
  81. }
  82. items[len++] = item;
  83. }
  84. mp_obj_t tuple = mp_obj_new_tuple(len, items);
  85. m_del(mp_obj_t, items, alloc);
  86. return tuple;
  87. }
  88. }
  89. }
  90. // Don't pass MP_BINARY_OP_NOT_EQUAL here
  91. STATIC mp_obj_t tuple_cmp_helper(mp_uint_t op, mp_obj_t self_in, mp_obj_t another_in) {
  92. mp_check_self(mp_obj_is_tuple_compatible(self_in));
  93. mp_obj_type_t *another_type = mp_obj_get_type(another_in);
  94. mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
  95. if (another_type->getiter != mp_obj_tuple_getiter) {
  96. // Slow path for user subclasses
  97. another_in = mp_instance_cast_to_native_base(another_in, MP_OBJ_FROM_PTR(&mp_type_tuple));
  98. if (another_in == MP_OBJ_NULL) {
  99. if (op == MP_BINARY_OP_EQUAL) {
  100. return mp_const_false;
  101. }
  102. return MP_OBJ_NULL;
  103. }
  104. }
  105. mp_obj_tuple_t *another = MP_OBJ_TO_PTR(another_in);
  106. return mp_obj_new_bool(mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len));
  107. }
  108. mp_obj_t mp_obj_tuple_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
  109. mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
  110. switch (op) {
  111. case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->len != 0);
  112. case MP_UNARY_OP_HASH: {
  113. // start hash with pointer to empty tuple, to make it fairly unique
  114. mp_int_t hash = (mp_int_t)mp_const_empty_tuple;
  115. for (size_t i = 0; i < self->len; i++) {
  116. hash += MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, self->items[i]));
  117. }
  118. return MP_OBJ_NEW_SMALL_INT(hash);
  119. }
  120. case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
  121. default: return MP_OBJ_NULL; // op not supported
  122. }
  123. }
  124. mp_obj_t mp_obj_tuple_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) {
  125. mp_obj_tuple_t *o = MP_OBJ_TO_PTR(lhs);
  126. switch (op) {
  127. case MP_BINARY_OP_ADD:
  128. case MP_BINARY_OP_INPLACE_ADD: {
  129. if (!mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(rhs)), MP_OBJ_FROM_PTR(&mp_type_tuple))) {
  130. return MP_OBJ_NULL; // op not supported
  131. }
  132. mp_obj_tuple_t *p = MP_OBJ_TO_PTR(rhs);
  133. mp_obj_tuple_t *s = MP_OBJ_TO_PTR(mp_obj_new_tuple(o->len + p->len, NULL));
  134. mp_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
  135. return MP_OBJ_FROM_PTR(s);
  136. }
  137. case MP_BINARY_OP_MULTIPLY:
  138. case MP_BINARY_OP_INPLACE_MULTIPLY: {
  139. mp_int_t n;
  140. if (!mp_obj_get_int_maybe(rhs, &n)) {
  141. return MP_OBJ_NULL; // op not supported
  142. }
  143. if (n <= 0) {
  144. return mp_const_empty_tuple;
  145. }
  146. mp_obj_tuple_t *s = MP_OBJ_TO_PTR(mp_obj_new_tuple(o->len * n, NULL));
  147. mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items);
  148. return MP_OBJ_FROM_PTR(s);
  149. }
  150. case MP_BINARY_OP_EQUAL:
  151. case MP_BINARY_OP_LESS:
  152. case MP_BINARY_OP_LESS_EQUAL:
  153. case MP_BINARY_OP_MORE:
  154. case MP_BINARY_OP_MORE_EQUAL:
  155. return tuple_cmp_helper(op, lhs, rhs);
  156. default:
  157. return MP_OBJ_NULL; // op not supported
  158. }
  159. }
  160. mp_obj_t mp_obj_tuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
  161. if (value == MP_OBJ_SENTINEL) {
  162. // load
  163. mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
  164. #if MICROPY_PY_BUILTINS_SLICE
  165. if (mp_obj_is_type(index, &mp_type_slice)) {
  166. mp_bound_slice_t slice;
  167. if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) {
  168. mp_raise_NotImplementedError("only slices with step=1 (aka None) are supported");
  169. }
  170. mp_obj_tuple_t *res = MP_OBJ_TO_PTR(mp_obj_new_tuple(slice.stop - slice.start, NULL));
  171. mp_seq_copy(res->items, self->items + slice.start, res->len, mp_obj_t);
  172. return MP_OBJ_FROM_PTR(res);
  173. }
  174. #endif
  175. size_t index_value = mp_get_index(self->base.type, self->len, index, false);
  176. return self->items[index_value];
  177. } else {
  178. return MP_OBJ_NULL; // op not supported
  179. }
  180. }
  181. STATIC mp_obj_t tuple_count(mp_obj_t self_in, mp_obj_t value) {
  182. mp_check_self(mp_obj_is_type(self_in, &mp_type_tuple));
  183. mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
  184. return mp_seq_count_obj(self->items, self->len, value);
  185. }
  186. STATIC MP_DEFINE_CONST_FUN_OBJ_2(tuple_count_obj, tuple_count);
  187. STATIC mp_obj_t tuple_index(size_t n_args, const mp_obj_t *args) {
  188. mp_check_self(mp_obj_is_type(args[0], &mp_type_tuple));
  189. mp_obj_tuple_t *self = MP_OBJ_TO_PTR(args[0]);
  190. return mp_seq_index_obj(self->items, self->len, n_args, args);
  191. }
  192. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tuple_index_obj, 2, 4, tuple_index);
  193. STATIC const mp_rom_map_elem_t tuple_locals_dict_table[] = {
  194. { MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&tuple_count_obj) },
  195. { MP_ROM_QSTR(MP_QSTR_index), MP_ROM_PTR(&tuple_index_obj) },
  196. };
  197. STATIC MP_DEFINE_CONST_DICT(tuple_locals_dict, tuple_locals_dict_table);
  198. const mp_obj_type_t mp_type_tuple = {
  199. { &mp_type_type },
  200. .name = MP_QSTR_tuple,
  201. .print = mp_obj_tuple_print,
  202. .make_new = mp_obj_tuple_make_new,
  203. .unary_op = mp_obj_tuple_unary_op,
  204. .binary_op = mp_obj_tuple_binary_op,
  205. .subscr = mp_obj_tuple_subscr,
  206. .getiter = mp_obj_tuple_getiter,
  207. .locals_dict = (mp_obj_dict_t*)&tuple_locals_dict,
  208. };
  209. // the zero-length tuple
  210. const mp_obj_tuple_t mp_const_empty_tuple_obj = {{&mp_type_tuple}, 0};
  211. mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items) {
  212. if (n == 0) {
  213. return mp_const_empty_tuple;
  214. }
  215. mp_obj_tuple_t *o = m_new_obj_var(mp_obj_tuple_t, mp_obj_t, n);
  216. o->base.type = &mp_type_tuple;
  217. o->len = n;
  218. if (items) {
  219. for (size_t i = 0; i < n; i++) {
  220. o->items[i] = items[i];
  221. }
  222. }
  223. return MP_OBJ_FROM_PTR(o);
  224. }
  225. void mp_obj_tuple_get(mp_obj_t self_in, size_t *len, mp_obj_t **items) {
  226. assert(mp_obj_is_tuple_compatible(self_in));
  227. mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
  228. *len = self->len;
  229. *items = &self->items[0];
  230. }
  231. void mp_obj_tuple_del(mp_obj_t self_in) {
  232. assert(mp_obj_is_type(self_in, &mp_type_tuple));
  233. mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
  234. m_del_var(mp_obj_tuple_t, mp_obj_t, self->len, self);
  235. }
  236. /******************************************************************************/
  237. /* tuple iterator */
  238. typedef struct _mp_obj_tuple_it_t {
  239. mp_obj_base_t base;
  240. mp_fun_1_t iternext;
  241. mp_obj_tuple_t *tuple;
  242. size_t cur;
  243. } mp_obj_tuple_it_t;
  244. STATIC mp_obj_t tuple_it_iternext(mp_obj_t self_in) {
  245. mp_obj_tuple_it_t *self = MP_OBJ_TO_PTR(self_in);
  246. if (self->cur < self->tuple->len) {
  247. mp_obj_t o_out = self->tuple->items[self->cur];
  248. self->cur += 1;
  249. return o_out;
  250. } else {
  251. return MP_OBJ_STOP_ITERATION;
  252. }
  253. }
  254. mp_obj_t mp_obj_tuple_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
  255. assert(sizeof(mp_obj_tuple_it_t) <= sizeof(mp_obj_iter_buf_t));
  256. mp_obj_tuple_it_t *o = (mp_obj_tuple_it_t*)iter_buf;
  257. o->base.type = &mp_type_polymorph_iter;
  258. o->iternext = tuple_it_iternext;
  259. o->tuple = MP_OBJ_TO_PTR(o_in);
  260. o->cur = 0;
  261. return MP_OBJ_FROM_PTR(o);
  262. }