blob: 99146bd4c170f6e10bdf0568c68a56667ba0cb14 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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 Sokolovsky427905c2014-01-18 19:24:47 +020028#include <string.h>
Paul Sokolovsky427905c2014-01-18 19:24:47 +020029#include <assert.h>
stijn49c47da2014-10-28 17:20:52 +010030#include <stdint.h>
Paul Sokolovsky427905c2014-01-18 19:24:47 +020031
Damien George51dfcb42015-01-01 20:27:54 +000032#include "py/nlr.h"
33#include "py/runtime0.h"
34#include "py/runtime.h"
35#include "py/binary.h"
Paul Sokolovskyc38809e2016-02-14 18:55:16 +020036#include "py/objstr.h"
Paul Sokolovskyec7fe922016-07-06 18:18:46 +030037#include "py/objarray.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020038
Damien Georgedd4f4532014-10-23 13:34:35 +010039#if MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW
Paul Sokolovskycb78f862014-06-27 20:39:09 +030040
Damien Georgede3c8062014-10-26 13:20:50 +000041// About memoryview object: We want to reuse as much code as possible from
42// array, and keep the memoryview object 4 words in size so it fits in 1 GC
43// block. Also, memoryview must keep a pointer to the base of the buffer so
44// that the buffer is not GC'd if the original parent object is no longer
45// around (we are assuming that all memoryview'able objects return a pointer
46// which points to the start of a GC chunk). Given the above constraints we
47// do the following:
48// - typecode high bit is set if the buffer is read-write (else read-only)
49// - free is the offset in elements to the first item in the memoryview
50// - len is the length in elements
51// - items points to the start of the original buffer
52// Note that we don't handle the case where the original buffer might change
53// size due to a resize of the original parent object.
54
55// make (& TYPECODE_MASK) a null operation if memorview not enabled
56#if MICROPY_PY_BUILTINS_MEMORYVIEW
57#define TYPECODE_MASK (0x7f)
58#else
Damien Georgeccc52542017-02-16 16:31:43 +110059#define TYPECODE_MASK (~(size_t)0)
Damien Georgede3c8062014-10-26 13:20:50 +000060#endif
61
Damien Georgeae8d8672016-01-09 23:14:54 +000062STATIC mp_obj_t array_iterator_new(mp_obj_t array_in, mp_obj_iter_buf_t *iter_buf);
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020063STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg);
Damien Georgeb2e73112014-11-30 00:00:55 +000064STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in);
Damien George4d917232014-08-30 14:28:06 +010065STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020066
67/******************************************************************************/
Damien Georgedd4f4532014-10-23 13:34:35 +010068// array
Paul Sokolovsky427905c2014-01-18 19:24:47 +020069
Damien Georgedd4f4532014-10-23 13:34:35 +010070#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Damien George7f9d1d62015-04-09 23:56:15 +010071STATIC void array_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000072 (void)kind;
Damien George999cedb2015-11-27 17:01:44 +000073 mp_obj_array_t *o = MP_OBJ_TO_PTR(o_in);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020074 if (o->typecode == BYTEARRAY_TYPECODE) {
Damien George7f9d1d62015-04-09 23:56:15 +010075 mp_print_str(print, "bytearray(b");
76 mp_str_print_quoted(print, o->items, o->len, true);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020077 } else {
Damien George7f9d1d62015-04-09 23:56:15 +010078 mp_printf(print, "array('%c'", o->typecode);
Paul Sokolovsky18014212014-01-28 03:40:48 +020079 if (o->len > 0) {
Damien George7f9d1d62015-04-09 23:56:15 +010080 mp_print_str(print, ", [");
Damien Georgeccc52542017-02-16 16:31:43 +110081 for (size_t i = 0; i < o->len; i++) {
Paul Sokolovsky18014212014-01-28 03:40:48 +020082 if (i > 0) {
Damien George7f9d1d62015-04-09 23:56:15 +010083 mp_print_str(print, ", ");
Paul Sokolovsky18014212014-01-28 03:40:48 +020084 }
Damien George7f9d1d62015-04-09 23:56:15 +010085 mp_obj_print_helper(print, mp_binary_get_val_array(o->typecode, o->items, i), PRINT_REPR);
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020086 }
Damien George7f9d1d62015-04-09 23:56:15 +010087 mp_print_str(print, "]");
Paul Sokolovsky427905c2014-01-18 19:24:47 +020088 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +020089 }
Damien George7f9d1d62015-04-09 23:56:15 +010090 mp_print_str(print, ")");
Paul Sokolovsky427905c2014-01-18 19:24:47 +020091}
Damien Georgedd4f4532014-10-23 13:34:35 +010092#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +020093
Damien Georgedd4f4532014-10-23 13:34:35 +010094#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Damien Georgeccc52542017-02-16 16:31:43 +110095STATIC mp_obj_array_t *array_new(char typecode, size_t n) {
Damien Georgedd4f4532014-10-23 13:34:35 +010096 int typecode_size = mp_binary_get_size('@', typecode, NULL);
Damien Georgedd4f4532014-10-23 13:34:35 +010097 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
98 #if MICROPY_PY_BUILTINS_BYTEARRAY && MICROPY_PY_ARRAY
99 o->base.type = (typecode == BYTEARRAY_TYPECODE) ? &mp_type_bytearray : &mp_type_array;
100 #elif MICROPY_PY_BUILTINS_BYTEARRAY
101 o->base.type = &mp_type_bytearray;
102 #else
103 o->base.type = &mp_type_array;
104 #endif
105 o->typecode = typecode;
106 o->free = 0;
107 o->len = n;
Damien George4d77e1a2015-02-27 09:34:51 +0000108 o->items = m_new(byte, typecode_size * o->len);
Damien Georgedd4f4532014-10-23 13:34:35 +0100109 return o;
110}
111#endif
112
113#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200114STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
Damien George32ef3a32014-12-04 15:46:14 +0000115 // bytearrays can be raw-initialised from anything with the buffer protocol
116 // other arrays can only be raw-initialised from bytes and bytearray objects
117 mp_buffer_info_t bufinfo;
118 if (((MICROPY_PY_BUILTINS_BYTEARRAY
119 && typecode == BYTEARRAY_TYPECODE)
120 || (MICROPY_PY_ARRAY
121 && (MP_OBJ_IS_TYPE(initializer, &mp_type_bytes)
Damien George035deae2015-07-02 16:26:57 +0100122 || (MICROPY_PY_BUILTINS_BYTEARRAY && MP_OBJ_IS_TYPE(initializer, &mp_type_bytearray)))))
Damien George32ef3a32014-12-04 15:46:14 +0000123 && mp_get_buffer(initializer, &bufinfo, MP_BUFFER_READ)) {
124 // construct array from raw bytes
125 // we round-down the len to make it a multiple of sz (CPython raises error)
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200126 size_t sz = mp_binary_get_size('@', typecode, NULL);
Damien Georgeccc52542017-02-16 16:31:43 +1100127 size_t len = bufinfo.len / sz;
Damien George32ef3a32014-12-04 15:46:14 +0000128 mp_obj_array_t *o = array_new(typecode, len);
129 memcpy(o->items, bufinfo.buf, len * sz);
Damien George999cedb2015-11-27 17:01:44 +0000130 return MP_OBJ_FROM_PTR(o);
Damien George32ef3a32014-12-04 15:46:14 +0000131 }
132
Damien Georgeccc52542017-02-16 16:31:43 +1100133 size_t len;
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200134 // Try to create array of exact len if initializer len is known
135 mp_obj_t len_in = mp_obj_len_maybe(initializer);
136 if (len_in == MP_OBJ_NULL) {
137 len = 0;
138 } else {
139 len = MP_OBJ_SMALL_INT_VALUE(len_in);
140 }
141
142 mp_obj_array_t *array = array_new(typecode, len);
143
Damien Georgee6003f42017-02-13 15:44:31 +1100144 mp_obj_t iterable = mp_getiter(initializer, NULL);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200145 mp_obj_t item;
Damien Georgeccc52542017-02-16 16:31:43 +1100146 size_t i = 0;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100147 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200148 if (len == 0) {
Damien George999cedb2015-11-27 17:01:44 +0000149 array_append(MP_OBJ_FROM_PTR(array), item);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200150 } else {
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300151 mp_binary_set_val_array(typecode, array->items, i++, item);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200152 }
153 }
154
Damien George999cedb2015-11-27 17:01:44 +0000155 return MP_OBJ_FROM_PTR(array);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200156}
Damien Georgedd4f4532014-10-23 13:34:35 +0100157#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200158
Damien Georgedd4f4532014-10-23 13:34:35 +0100159#if MICROPY_PY_ARRAY
Damien George5b3f0b72016-01-03 15:55:55 +0000160STATIC mp_obj_t array_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000161 (void)type_in;
Damien Georgea3f94e02014-04-20 00:13:22 +0100162 mp_arg_check_num(n_args, n_kw, 1, 2, false);
Damien George32ca1642014-04-18 22:04:06 +0100163
164 // get typecode
Damien Georgeab5689b2017-03-25 19:53:31 +1100165 const char *typecode = mp_obj_str_get_str(args[0]);
Paul Sokolovsky11973b42014-01-28 02:31:52 +0200166
Damien George32ca1642014-04-18 22:04:06 +0100167 if (n_args == 1) {
168 // 1 arg: make an empty array
Damien George999cedb2015-11-27 17:01:44 +0000169 return MP_OBJ_FROM_PTR(array_new(*typecode, 0));
Damien George32ca1642014-04-18 22:04:06 +0100170 } else {
Damien George32ef3a32014-12-04 15:46:14 +0000171 // 2 args: construct the array from the given object
Damien George32ca1642014-04-18 22:04:06 +0100172 return array_construct(*typecode, args[1]);
173 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200174}
Damien Georgedd4f4532014-10-23 13:34:35 +0100175#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200176
Damien Georgedd4f4532014-10-23 13:34:35 +0100177#if MICROPY_PY_BUILTINS_BYTEARRAY
Damien George5b3f0b72016-01-03 15:55:55 +0000178STATIC mp_obj_t bytearray_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000179 (void)type_in;
Damien Georgea3f94e02014-04-20 00:13:22 +0100180 mp_arg_check_num(n_args, n_kw, 0, 1, false);
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300181
Damien George32ca1642014-04-18 22:04:06 +0100182 if (n_args == 0) {
183 // no args: construct an empty bytearray
Damien George999cedb2015-11-27 17:01:44 +0000184 return MP_OBJ_FROM_PTR(array_new(BYTEARRAY_TYPECODE, 0));
Paul Sokolovsky343ca1e2015-01-04 17:19:16 +0200185 } else if (MP_OBJ_IS_INT(args[0])) {
Damien George32ca1642014-04-18 22:04:06 +0100186 // 1 arg, an integer: construct a blank bytearray of that length
Paul Sokolovsky343ca1e2015-01-04 17:19:16 +0200187 mp_uint_t len = mp_obj_get_int(args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300188 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len);
189 memset(o->items, 0, len);
Damien George999cedb2015-11-27 17:01:44 +0000190 return MP_OBJ_FROM_PTR(o);
Damien George32ca1642014-04-18 22:04:06 +0100191 } else {
Damien George32ef3a32014-12-04 15:46:14 +0000192 // 1 arg: construct the bytearray from that
Damien George32ca1642014-04-18 22:04:06 +0100193 return array_construct(BYTEARRAY_TYPECODE, args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300194 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200195}
Damien Georgedd4f4532014-10-23 13:34:35 +0100196#endif
197
198#if MICROPY_PY_BUILTINS_MEMORYVIEW
Paul Sokolovskycb0fc062015-03-06 21:35:26 +0200199
Damien Georgeccc52542017-02-16 16:31:43 +1100200mp_obj_t mp_obj_new_memoryview(byte typecode, size_t nitems, void *items) {
Paul Sokolovskycb0fc062015-03-06 21:35:26 +0200201 mp_obj_array_t *self = m_new_obj(mp_obj_array_t);
202 self->base.type = &mp_type_memoryview;
203 self->typecode = typecode;
204 self->free = 0;
205 self->len = nitems;
206 self->items = items;
Damien George999cedb2015-11-27 17:01:44 +0000207 return MP_OBJ_FROM_PTR(self);
Paul Sokolovskycb0fc062015-03-06 21:35:26 +0200208}
209
Damien George5b3f0b72016-01-03 15:55:55 +0000210STATIC mp_obj_t memoryview_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Damien George2e2e4042015-03-19 00:21:29 +0000211 (void)type_in;
212
Damien Georgedd4f4532014-10-23 13:34:35 +0100213 // TODO possibly allow memoryview constructor to take start/stop so that one
214 // can do memoryview(b, 4, 8) instead of memoryview(b)[4:8] (uses less RAM)
215
216 mp_arg_check_num(n_args, n_kw, 1, 1, false);
217
218 mp_buffer_info_t bufinfo;
219 mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
220
Damien George999cedb2015-11-27 17:01:44 +0000221 mp_obj_array_t *self = MP_OBJ_TO_PTR(mp_obj_new_memoryview(bufinfo.typecode,
Paul Sokolovskycb0fc062015-03-06 21:35:26 +0200222 bufinfo.len / mp_binary_get_size('@', bufinfo.typecode, NULL),
Damien George999cedb2015-11-27 17:01:44 +0000223 bufinfo.buf));
Damien Georgedd4f4532014-10-23 13:34:35 +0100224
225 // test if the object can be written to
226 if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_RW)) {
Damien Georgede3c8062014-10-26 13:20:50 +0000227 self->typecode |= 0x80; // used to indicate writable buffer
Damien Georgedd4f4532014-10-23 13:34:35 +0100228 }
229
Damien George999cedb2015-11-27 17:01:44 +0000230 return MP_OBJ_FROM_PTR(self);
Damien Georgedd4f4532014-10-23 13:34:35 +0100231}
232#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200233
Damien Georgeecc88e92014-08-30 00:35:11 +0100234STATIC mp_obj_t array_unary_op(mp_uint_t op, mp_obj_t o_in) {
Damien George999cedb2015-11-27 17:01:44 +0000235 mp_obj_array_t *o = MP_OBJ_TO_PTR(o_in);
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200236 switch (op) {
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300237 case MP_UNARY_OP_BOOL: return mp_obj_new_bool(o->len != 0);
Damien Georged17926d2014-03-30 13:35:08 +0100238 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
Damien George6ac5dce2014-05-21 19:42:43 +0100239 default: return MP_OBJ_NULL; // op not supported
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200240 }
241}
242
Damien Georgeecc88e92014-08-30 00:35:11 +0100243STATIC mp_obj_t array_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien George999cedb2015-11-27 17:01:44 +0000244 mp_obj_array_t *lhs = MP_OBJ_TO_PTR(lhs_in);
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300245 switch (op) {
Damien Georgeb2e73112014-11-30 00:00:55 +0000246 case MP_BINARY_OP_ADD: {
Damien Georgec7ca01a2014-11-30 14:01:33 +0000247 // allow to add anything that has the buffer protocol (extension to CPython)
248 mp_buffer_info_t lhs_bufinfo;
249 mp_buffer_info_t rhs_bufinfo;
250 array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ);
251 mp_get_buffer_raise(rhs_in, &rhs_bufinfo, MP_BUFFER_READ);
252
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200253 size_t sz = mp_binary_get_size('@', lhs_bufinfo.typecode, NULL);
Damien Georgec7ca01a2014-11-30 14:01:33 +0000254
255 // convert byte count to element count (in case rhs is not multiple of sz)
Damien Georgeccc52542017-02-16 16:31:43 +1100256 size_t rhs_len = rhs_bufinfo.len / sz;
Damien Georgec7ca01a2014-11-30 14:01:33 +0000257
258 // note: lhs->len is element count of lhs, lhs_bufinfo.len is byte count
259 mp_obj_array_t *res = array_new(lhs_bufinfo.typecode, lhs->len + rhs_len);
260 mp_seq_cat((byte*)res->items, lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_len * sz, byte);
Damien George999cedb2015-11-27 17:01:44 +0000261 return MP_OBJ_FROM_PTR(res);
Damien Georgeb2e73112014-11-30 00:00:55 +0000262 }
263
264 case MP_BINARY_OP_INPLACE_ADD: {
265 #if MICROPY_PY_BUILTINS_MEMORYVIEW
266 if (lhs->base.type == &mp_type_memoryview) {
267 return MP_OBJ_NULL; // op not supported
268 }
269 #endif
Damien George999cedb2015-11-27 17:01:44 +0000270 array_extend(lhs_in, rhs_in);
271 return lhs_in;
Damien Georgeb2e73112014-11-30 00:00:55 +0000272 }
273
Paul Sokolovskyc38809e2016-02-14 18:55:16 +0200274 case MP_BINARY_OP_IN: {
275 /* NOTE `a in b` is `b.__contains__(a)` */
276 mp_buffer_info_t lhs_bufinfo;
277 mp_buffer_info_t rhs_bufinfo;
278
279 // Can search string only in bytearray
280 if (mp_get_buffer(rhs_in, &rhs_bufinfo, MP_BUFFER_READ)) {
281 if (!MP_OBJ_IS_TYPE(lhs_in, &mp_type_bytearray)) {
282 return mp_const_false;
283 }
284 array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ);
285 return mp_obj_new_bool(
286 find_subbytes(lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_bufinfo.len, 1) != NULL);
287 }
288
289 // Otherwise, can only look for a scalar numeric value in an array
290 if (MP_OBJ_IS_INT(rhs_in) || mp_obj_is_float(rhs_in)) {
291 mp_not_implemented("");
292 }
293
294 return mp_const_false;
295 }
296
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300297 case MP_BINARY_OP_EQUAL: {
298 mp_buffer_info_t lhs_bufinfo;
299 mp_buffer_info_t rhs_bufinfo;
300 array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ);
301 if (!mp_get_buffer(rhs_in, &rhs_bufinfo, MP_BUFFER_READ)) {
Damien Georgede3c8062014-10-26 13:20:50 +0000302 return mp_const_false;
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300303 }
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300304 return mp_obj_new_bool(mp_seq_cmp_bytes(op, lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_bufinfo.len));
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300305 }
Damien Georgeb2e73112014-11-30 00:00:55 +0000306
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300307 default:
308 return MP_OBJ_NULL; // op not supported
309 }
310}
311
Damien Georgeb2e73112014-11-30 00:00:55 +0000312#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200313STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
Damien Georgeb2e73112014-11-30 00:00:55 +0000314 // self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
Damien George035deae2015-07-02 16:26:57 +0100315 assert((MICROPY_PY_BUILTINS_BYTEARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray))
316 || (MICROPY_PY_ARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_array)));
Damien George999cedb2015-11-27 17:01:44 +0000317 mp_obj_array_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgeb2e73112014-11-30 00:00:55 +0000318
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200319 if (self->free == 0) {
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200320 size_t item_sz = mp_binary_get_size('@', self->typecode, NULL);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200321 // TODO: alloc policy
322 self->free = 8;
Damien George4d77e1a2015-02-27 09:34:51 +0000323 self->items = m_renew(byte, self->items, item_sz * self->len, item_sz * (self->len + self->free));
Paul Sokolovskya2240672014-04-28 00:16:57 +0300324 mp_seq_clear(self->items, self->len + 1, self->len + self->free, item_sz);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200325 }
Damien George04d5e642016-04-07 09:03:33 +0100326 mp_binary_set_val_array(self->typecode, self->items, self->len, arg);
327 // only update length/free if set succeeded
328 self->len++;
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200329 self->free--;
330 return mp_const_none; // return None, as per CPython
331}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200332STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200333
Damien Georgeb2e73112014-11-30 00:00:55 +0000334STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in) {
335 // self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
Damien George035deae2015-07-02 16:26:57 +0100336 assert((MICROPY_PY_BUILTINS_BYTEARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray))
337 || (MICROPY_PY_ARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_array)));
Damien George999cedb2015-11-27 17:01:44 +0000338 mp_obj_array_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgeb2e73112014-11-30 00:00:55 +0000339
Damien Georgec7ca01a2014-11-30 14:01:33 +0000340 // allow to extend by anything that has the buffer protocol (extension to CPython)
341 mp_buffer_info_t arg_bufinfo;
342 mp_get_buffer_raise(arg_in, &arg_bufinfo, MP_BUFFER_READ);
Damien Georgeb2e73112014-11-30 00:00:55 +0000343
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200344 size_t sz = mp_binary_get_size('@', self->typecode, NULL);
Damien Georgeb2e73112014-11-30 00:00:55 +0000345
Damien Georgec7ca01a2014-11-30 14:01:33 +0000346 // convert byte count to element count
Damien Georgeccc52542017-02-16 16:31:43 +1100347 size_t len = arg_bufinfo.len / sz;
Damien Georgec7ca01a2014-11-30 14:01:33 +0000348
Damien Georgeb2e73112014-11-30 00:00:55 +0000349 // make sure we have enough room to extend
Damien Georgec7ca01a2014-11-30 14:01:33 +0000350 // TODO: alloc policy; at the moment we go conservative
351 if (self->free < len) {
Damien George4d77e1a2015-02-27 09:34:51 +0000352 self->items = m_renew(byte, self->items, (self->len + self->free) * sz, (self->len + len) * sz);
Damien Georgec7ca01a2014-11-30 14:01:33 +0000353 self->free = 0;
354 } else {
355 self->free -= len;
Damien Georgeb2e73112014-11-30 00:00:55 +0000356 }
357
358 // extend
Damien Georgec7ca01a2014-11-30 14:01:33 +0000359 mp_seq_copy((byte*)self->items + self->len * sz, arg_bufinfo.buf, len * sz, byte);
360 self->len += len;
Damien Georgeb2e73112014-11-30 00:00:55 +0000361
362 return mp_const_none;
363}
364STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_extend_obj, array_extend);
365#endif
366
Damien George729f7b42014-04-17 22:10:53 +0100367STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
Damien Georgef4c9b332014-04-08 21:32:29 +0100368 if (value == MP_OBJ_NULL) {
Damien George32ca1642014-04-18 22:04:06 +0100369 // delete item
370 // TODO implement
Paul Sokolovsky26905252014-04-20 20:58:33 +0300371 // TODO: confirmed that both bytearray and array.array support
372 // slice deletion
Damien George6ac5dce2014-05-21 19:42:43 +0100373 return MP_OBJ_NULL; // op not supported
Damien George729f7b42014-04-17 22:10:53 +0100374 } else {
Damien George999cedb2015-11-27 17:01:44 +0000375 mp_obj_array_t *o = MP_OBJ_TO_PTR(self_in);
Damien Georgec49ddb92014-06-01 13:49:35 +0100376 if (0) {
377#if MICROPY_PY_BUILTINS_SLICE
378 } else if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300379 mp_bound_slice_t slice;
380 if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) {
Damien George821b7f22015-09-03 23:14:06 +0100381 mp_not_implemented("only slices with step=1 (aka None) are supported");
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300382 }
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200383 if (value != MP_OBJ_SENTINEL) {
384 #if MICROPY_PY_ARRAY_SLICE_ASSIGN
385 // Assign
Damien Georgeccc52542017-02-16 16:31:43 +1100386 size_t src_len;
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300387 void *src_items;
Delio Brignoli32aba402015-06-03 07:07:35 +0200388 size_t item_sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
Damien George999cedb2015-11-27 17:01:44 +0000389 if (MP_OBJ_IS_OBJ(value) && ((mp_obj_base_t*)MP_OBJ_TO_PTR(value))->type->subscr == array_subscr) {
Damien George4915c2b2015-07-20 16:12:26 +0100390 // value is array, bytearray or memoryview
Damien George999cedb2015-11-27 17:01:44 +0000391 mp_obj_array_t *src_slice = MP_OBJ_TO_PTR(value);
Delio Brignoli32aba402015-06-03 07:07:35 +0200392 if (item_sz != mp_binary_get_size('@', src_slice->typecode & TYPECODE_MASK, NULL)) {
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300393 compat_error:
Damien George94c41bb2017-03-28 22:37:26 +1100394 mp_raise_ValueError("lhs and rhs should be compatible");
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300395 }
396 src_len = src_slice->len;
397 src_items = src_slice->items;
Damien George4915c2b2015-07-20 16:12:26 +0100398 #if MICROPY_PY_BUILTINS_MEMORYVIEW
399 if (MP_OBJ_IS_TYPE(value, &mp_type_memoryview)) {
400 src_items = (uint8_t*)src_items + (src_slice->free * item_sz);
401 }
402 #endif
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300403 } else if (MP_OBJ_IS_TYPE(value, &mp_type_bytes)) {
404 if (item_sz != 1) {
405 goto compat_error;
406 }
407 mp_buffer_info_t bufinfo;
408 mp_get_buffer_raise(value, &bufinfo, MP_BUFFER_READ);
409 src_len = bufinfo.len;
410 src_items = bufinfo.buf;
411 } else {
412 mp_not_implemented("array/bytes required on right side");
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200413 }
414
415 // TODO: check src/dst compat
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300416 mp_int_t len_adj = src_len - (slice.stop - slice.start);
Delio Brignoli6a388aa2015-06-06 20:14:08 +0200417 uint8_t* dest_items = o->items;
418 #if MICROPY_PY_BUILTINS_MEMORYVIEW
419 if (o->base.type == &mp_type_memoryview) {
Damien Georgef4a12dc2017-02-27 16:09:57 +1100420 if ((o->typecode & 0x80) == 0) {
421 // store to read-only memoryview not allowed
422 return MP_OBJ_NULL;
423 }
Delio Brignoli6a388aa2015-06-06 20:14:08 +0200424 if (len_adj != 0) {
Delio Brignoli32aba402015-06-03 07:07:35 +0200425 goto compat_error;
426 }
Delio Brignoli6a388aa2015-06-06 20:14:08 +0200427 dest_items += o->free * item_sz;
428 }
429 #endif
430 if (len_adj > 0) {
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200431 if (len_adj > o->free) {
432 // TODO: alloc policy; at the moment we go conservative
Damien Georged8914522015-02-27 09:54:12 +0000433 o->items = m_renew(byte, o->items, (o->len + o->free) * item_sz, (o->len + len_adj) * item_sz);
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200434 o->free = 0;
Damien George77f85db2016-03-14 23:12:54 +0000435 dest_items = o->items;
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200436 }
Delio Brignoli6a388aa2015-06-06 20:14:08 +0200437 mp_seq_replace_slice_grow_inplace(dest_items, o->len,
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300438 slice.start, slice.stop, src_items, src_len, len_adj, item_sz);
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200439 } else {
Delio Brignoli6a388aa2015-06-06 20:14:08 +0200440 mp_seq_replace_slice_no_grow(dest_items, o->len,
441 slice.start, slice.stop, src_items, src_len, item_sz);
442 // Clear "freed" elements at the end of list
443 // TODO: This is actually only needed for typecode=='O'
444 mp_seq_clear(dest_items, o->len + len_adj, o->len, item_sz);
445 // TODO: alloc policy after shrinking
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200446 }
447 o->len += len_adj;
448 return mp_const_none;
449 #else
450 return MP_OBJ_NULL; // op not supported
451 #endif
452 }
453
Damien Georgedd4f4532014-10-23 13:34:35 +0100454 mp_obj_array_t *res;
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200455 size_t sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300456 assert(sz > 0);
Damien Georgedd4f4532014-10-23 13:34:35 +0100457 if (0) {
458 // dummy
459 #if MICROPY_PY_BUILTINS_MEMORYVIEW
460 } else if (o->base.type == &mp_type_memoryview) {
461 res = m_new_obj(mp_obj_array_t);
462 *res = *o;
Damien Georgede3c8062014-10-26 13:20:50 +0000463 res->free += slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100464 res->len = slice.stop - slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100465 #endif
466 } else {
467 res = array_new(o->typecode, slice.stop - slice.start);
stijn49c47da2014-10-28 17:20:52 +0100468 memcpy(res->items, (uint8_t*)o->items + slice.start * sz, (slice.stop - slice.start) * sz);
Damien Georgedd4f4532014-10-23 13:34:35 +0100469 }
Damien George999cedb2015-11-27 17:01:44 +0000470 return MP_OBJ_FROM_PTR(res);
Damien Georgec49ddb92014-06-01 13:49:35 +0100471#endif
Damien George729f7b42014-04-17 22:10:53 +0100472 } else {
Damien Georgec88cfe12017-03-23 16:17:40 +1100473 size_t index = mp_get_index(o->base.type, o->len, index_in, false);
Damien Georgede3c8062014-10-26 13:20:50 +0000474 #if MICROPY_PY_BUILTINS_MEMORYVIEW
475 if (o->base.type == &mp_type_memoryview) {
476 index += o->free;
477 if (value != MP_OBJ_SENTINEL && (o->typecode & 0x80) == 0) {
478 // store to read-only memoryview
Damien Georgedd4f4532014-10-23 13:34:35 +0100479 return MP_OBJ_NULL;
480 }
Damien Georgede3c8062014-10-26 13:20:50 +0000481 }
482 #endif
483 if (value == MP_OBJ_SENTINEL) {
484 // load
485 return mp_binary_get_val_array(o->typecode & TYPECODE_MASK, o->items, index);
486 } else {
487 // store
488 mp_binary_set_val_array(o->typecode & TYPECODE_MASK, o->items, index, value);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300489 return mp_const_none;
490 }
Damien George729f7b42014-04-17 22:10:53 +0100491 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100492 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200493}
494
Damien George4d917232014-08-30 14:28:06 +0100495STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
Damien George999cedb2015-11-27 17:01:44 +0000496 mp_obj_array_t *o = MP_OBJ_TO_PTR(o_in);
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200497 size_t sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
Damien Georgede3c8062014-10-26 13:20:50 +0000498 bufinfo->buf = o->items;
499 bufinfo->len = o->len * sz;
500 bufinfo->typecode = o->typecode & TYPECODE_MASK;
Damien Georgedd4f4532014-10-23 13:34:35 +0100501 #if MICROPY_PY_BUILTINS_MEMORYVIEW
Damien Georgede3c8062014-10-26 13:20:50 +0000502 if (o->base.type == &mp_type_memoryview) {
503 if ((o->typecode & 0x80) == 0 && (flags & MP_BUFFER_WRITE)) {
504 // read-only memoryview
505 return 1;
506 }
Damien Georgeccc52542017-02-16 16:31:43 +1100507 bufinfo->buf = (uint8_t*)bufinfo->buf + (size_t)o->free * sz;
Damien Georgedd4f4532014-10-23 13:34:35 +0100508 }
Damien George3a2171e2015-09-04 16:53:46 +0100509 #else
510 (void)flags;
Damien Georgedd4f4532014-10-23 13:34:35 +0100511 #endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200512 return 0;
513}
514
Damien Georgedd4f4532014-10-23 13:34:35 +0100515#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Damien Georgecbf76742015-11-27 13:38:15 +0000516STATIC const mp_rom_map_elem_t array_locals_dict_table[] = {
517 { MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&array_append_obj) },
518 { MP_ROM_QSTR(MP_QSTR_extend), MP_ROM_PTR(&array_extend_obj) },
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200519};
520
Damien George9b196cd2014-03-26 21:47:19 +0000521STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
Damien Georgedd4f4532014-10-23 13:34:35 +0100522#endif
Damien George9b196cd2014-03-26 21:47:19 +0000523
Damien Georgedd4f4532014-10-23 13:34:35 +0100524#if MICROPY_PY_ARRAY
Damien Georgecaac5422014-03-25 14:18:18 +0000525const mp_obj_type_t mp_type_array = {
Damien Georgec5966122014-02-15 16:10:44 +0000526 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000527 .name = MP_QSTR_array,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200528 .print = array_print,
529 .make_new = array_make_new,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200530 .getiter = array_iterator_new,
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200531 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300532 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100533 .subscr = array_subscr,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200534 .buffer_p = { .get_buffer = array_get_buffer },
Damien George999cedb2015-11-27 17:01:44 +0000535 .locals_dict = (mp_obj_dict_t*)&array_locals_dict,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200536};
Damien Georgedd4f4532014-10-23 13:34:35 +0100537#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200538
Damien Georgedd4f4532014-10-23 13:34:35 +0100539#if MICROPY_PY_BUILTINS_BYTEARRAY
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300540const mp_obj_type_t mp_type_bytearray = {
541 { &mp_type_type },
542 .name = MP_QSTR_bytearray,
543 .print = array_print,
544 .make_new = bytearray_make_new,
545 .getiter = array_iterator_new,
546 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300547 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100548 .subscr = array_subscr,
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300549 .buffer_p = { .get_buffer = array_get_buffer },
Damien George999cedb2015-11-27 17:01:44 +0000550 .locals_dict = (mp_obj_dict_t*)&array_locals_dict,
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300551};
Damien Georgedd4f4532014-10-23 13:34:35 +0100552#endif
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300553
Damien Georgedd4f4532014-10-23 13:34:35 +0100554#if MICROPY_PY_BUILTINS_MEMORYVIEW
555const mp_obj_type_t mp_type_memoryview = {
556 { &mp_type_type },
557 .name = MP_QSTR_memoryview,
558 .make_new = memoryview_make_new,
559 .getiter = array_iterator_new,
560 .unary_op = array_unary_op,
561 .binary_op = array_binary_op,
562 .subscr = array_subscr,
563 .buffer_p = { .get_buffer = array_get_buffer },
564};
565#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200566
Damien Georgedd4f4532014-10-23 13:34:35 +0100567/* unused
Damien Georgeccc52542017-02-16 16:31:43 +1100568size_t mp_obj_array_len(mp_obj_t self_in) {
Paul Sokolovsky33996682014-01-21 23:30:10 +0200569 return ((mp_obj_array_t *)self_in)->len;
570}
Damien Georgedd4f4532014-10-23 13:34:35 +0100571*/
Paul Sokolovsky33996682014-01-21 23:30:10 +0200572
Damien Georgedd4f4532014-10-23 13:34:35 +0100573#if MICROPY_PY_BUILTINS_BYTEARRAY
Damien Georgeccc52542017-02-16 16:31:43 +1100574mp_obj_t mp_obj_new_bytearray(size_t n, void *items) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200575 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
576 memcpy(o->items, items, n);
Damien George999cedb2015-11-27 17:01:44 +0000577 return MP_OBJ_FROM_PTR(o);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200578}
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200579
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200580// Create bytearray which references specified memory area
Damien Georgeccc52542017-02-16 16:31:43 +1100581mp_obj_t mp_obj_new_bytearray_by_ref(size_t n, void *items) {
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200582 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgedd4f4532014-10-23 13:34:35 +0100583 o->base.type = &mp_type_bytearray;
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200584 o->typecode = BYTEARRAY_TYPECODE;
585 o->free = 0;
586 o->len = n;
587 o->items = items;
Damien George999cedb2015-11-27 17:01:44 +0000588 return MP_OBJ_FROM_PTR(o);
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200589}
Damien Georgedd4f4532014-10-23 13:34:35 +0100590#endif
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200591
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200592/******************************************************************************/
Damien Georgedd4f4532014-10-23 13:34:35 +0100593// array iterator
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200594
595typedef struct _mp_obj_array_it_t {
596 mp_obj_base_t base;
597 mp_obj_array_t *array;
Damien Georgeccc52542017-02-16 16:31:43 +1100598 size_t offset;
599 size_t cur;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200600} mp_obj_array_it_t;
601
Damien Georgecaac5422014-03-25 14:18:18 +0000602STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000603 mp_obj_array_it_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200604 if (self->cur < self->array->len) {
Damien Georgede3c8062014-10-26 13:20:50 +0000605 return mp_binary_get_val_array(self->array->typecode & TYPECODE_MASK, self->array->items, self->offset + self->cur++);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200606 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100607 return MP_OBJ_STOP_ITERATION;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200608 }
609}
610
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200611STATIC const mp_obj_type_t array_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000612 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000613 .name = MP_QSTR_iterator,
Damien Georgeae8d8672016-01-09 23:14:54 +0000614 .getiter = mp_identity_getiter,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200615 .iternext = array_it_iternext,
616};
617
Damien Georgeae8d8672016-01-09 23:14:54 +0000618STATIC mp_obj_t array_iterator_new(mp_obj_t array_in, mp_obj_iter_buf_t *iter_buf) {
619 assert(sizeof(mp_obj_array_t) <= sizeof(mp_obj_iter_buf_t));
Damien George999cedb2015-11-27 17:01:44 +0000620 mp_obj_array_t *array = MP_OBJ_TO_PTR(array_in);
Damien Georgeae8d8672016-01-09 23:14:54 +0000621 mp_obj_array_it_t *o = (mp_obj_array_it_t*)iter_buf;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200622 o->base.type = &array_it_type;
623 o->array = array;
Damien Georgeae8d8672016-01-09 23:14:54 +0000624 o->offset = 0;
625 o->cur = 0;
Damien Georgede3c8062014-10-26 13:20:50 +0000626 #if MICROPY_PY_BUILTINS_MEMORYVIEW
627 if (array->base.type == &mp_type_memoryview) {
628 o->offset = array->free;
629 }
630 #endif
Damien George999cedb2015-11-27 17:01:44 +0000631 return MP_OBJ_FROM_PTR(o);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200632}
Paul Sokolovskycb78f862014-06-27 20:39:09 +0300633
Damien Georgedd4f4532014-10-23 13:34:35 +0100634#endif // MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW