blob: c81aebb50c30ea05cc038bdd4e80f3b520e3ee54 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
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 Georgeae8d8672016-01-09 23:14:54 +0000144 mp_obj_iter_buf_t iter_buf;
145 mp_obj_t iterable = mp_getiter(initializer, &iter_buf);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200146 mp_obj_t item;
Damien Georgeccc52542017-02-16 16:31:43 +1100147 size_t i = 0;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100148 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200149 if (len == 0) {
Damien George999cedb2015-11-27 17:01:44 +0000150 array_append(MP_OBJ_FROM_PTR(array), item);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200151 } else {
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300152 mp_binary_set_val_array(typecode, array->items, i++, item);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200153 }
154 }
155
Damien George999cedb2015-11-27 17:01:44 +0000156 return MP_OBJ_FROM_PTR(array);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200157}
Damien Georgedd4f4532014-10-23 13:34:35 +0100158#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200159
Damien Georgedd4f4532014-10-23 13:34:35 +0100160#if MICROPY_PY_ARRAY
Damien George5b3f0b72016-01-03 15:55:55 +0000161STATIC 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 +0000162 (void)type_in;
Damien Georgea3f94e02014-04-20 00:13:22 +0100163 mp_arg_check_num(n_args, n_kw, 1, 2, false);
Damien George32ca1642014-04-18 22:04:06 +0100164
165 // get typecode
Damien Georged182b982014-08-30 14:19:41 +0100166 mp_uint_t l;
Damien George698ec212014-02-08 18:17:23 +0000167 const char *typecode = mp_obj_str_get_data(args[0], &l);
Paul Sokolovsky11973b42014-01-28 02:31:52 +0200168
Damien George32ca1642014-04-18 22:04:06 +0100169 if (n_args == 1) {
170 // 1 arg: make an empty array
Damien George999cedb2015-11-27 17:01:44 +0000171 return MP_OBJ_FROM_PTR(array_new(*typecode, 0));
Damien George32ca1642014-04-18 22:04:06 +0100172 } else {
Damien George32ef3a32014-12-04 15:46:14 +0000173 // 2 args: construct the array from the given object
Damien George32ca1642014-04-18 22:04:06 +0100174 return array_construct(*typecode, args[1]);
175 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200176}
Damien Georgedd4f4532014-10-23 13:34:35 +0100177#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200178
Damien Georgedd4f4532014-10-23 13:34:35 +0100179#if MICROPY_PY_BUILTINS_BYTEARRAY
Damien George5b3f0b72016-01-03 15:55:55 +0000180STATIC 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 +0000181 (void)type_in;
Damien Georgea3f94e02014-04-20 00:13:22 +0100182 mp_arg_check_num(n_args, n_kw, 0, 1, false);
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300183
Damien George32ca1642014-04-18 22:04:06 +0100184 if (n_args == 0) {
185 // no args: construct an empty bytearray
Damien George999cedb2015-11-27 17:01:44 +0000186 return MP_OBJ_FROM_PTR(array_new(BYTEARRAY_TYPECODE, 0));
Paul Sokolovsky343ca1e2015-01-04 17:19:16 +0200187 } else if (MP_OBJ_IS_INT(args[0])) {
Damien George32ca1642014-04-18 22:04:06 +0100188 // 1 arg, an integer: construct a blank bytearray of that length
Paul Sokolovsky343ca1e2015-01-04 17:19:16 +0200189 mp_uint_t len = mp_obj_get_int(args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300190 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len);
191 memset(o->items, 0, len);
Damien George999cedb2015-11-27 17:01:44 +0000192 return MP_OBJ_FROM_PTR(o);
Damien George32ca1642014-04-18 22:04:06 +0100193 } else {
Damien George32ef3a32014-12-04 15:46:14 +0000194 // 1 arg: construct the bytearray from that
Damien George32ca1642014-04-18 22:04:06 +0100195 return array_construct(BYTEARRAY_TYPECODE, args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300196 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200197}
Damien Georgedd4f4532014-10-23 13:34:35 +0100198#endif
199
200#if MICROPY_PY_BUILTINS_MEMORYVIEW
Paul Sokolovskycb0fc062015-03-06 21:35:26 +0200201
Damien Georgeccc52542017-02-16 16:31:43 +1100202mp_obj_t mp_obj_new_memoryview(byte typecode, size_t nitems, void *items) {
Paul Sokolovskycb0fc062015-03-06 21:35:26 +0200203 mp_obj_array_t *self = m_new_obj(mp_obj_array_t);
204 self->base.type = &mp_type_memoryview;
205 self->typecode = typecode;
206 self->free = 0;
207 self->len = nitems;
208 self->items = items;
Damien George999cedb2015-11-27 17:01:44 +0000209 return MP_OBJ_FROM_PTR(self);
Paul Sokolovskycb0fc062015-03-06 21:35:26 +0200210}
211
Damien George5b3f0b72016-01-03 15:55:55 +0000212STATIC 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 +0000213 (void)type_in;
214
Damien Georgedd4f4532014-10-23 13:34:35 +0100215 // TODO possibly allow memoryview constructor to take start/stop so that one
216 // can do memoryview(b, 4, 8) instead of memoryview(b)[4:8] (uses less RAM)
217
218 mp_arg_check_num(n_args, n_kw, 1, 1, false);
219
220 mp_buffer_info_t bufinfo;
221 mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
222
Damien George999cedb2015-11-27 17:01:44 +0000223 mp_obj_array_t *self = MP_OBJ_TO_PTR(mp_obj_new_memoryview(bufinfo.typecode,
Paul Sokolovskycb0fc062015-03-06 21:35:26 +0200224 bufinfo.len / mp_binary_get_size('@', bufinfo.typecode, NULL),
Damien George999cedb2015-11-27 17:01:44 +0000225 bufinfo.buf));
Damien Georgedd4f4532014-10-23 13:34:35 +0100226
227 // test if the object can be written to
228 if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_RW)) {
Damien Georgede3c8062014-10-26 13:20:50 +0000229 self->typecode |= 0x80; // used to indicate writable buffer
Damien Georgedd4f4532014-10-23 13:34:35 +0100230 }
231
Damien George999cedb2015-11-27 17:01:44 +0000232 return MP_OBJ_FROM_PTR(self);
Damien Georgedd4f4532014-10-23 13:34:35 +0100233}
234#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200235
Damien Georgeecc88e92014-08-30 00:35:11 +0100236STATIC mp_obj_t array_unary_op(mp_uint_t op, mp_obj_t o_in) {
Damien George999cedb2015-11-27 17:01:44 +0000237 mp_obj_array_t *o = MP_OBJ_TO_PTR(o_in);
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200238 switch (op) {
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300239 case MP_UNARY_OP_BOOL: return mp_obj_new_bool(o->len != 0);
Damien Georged17926d2014-03-30 13:35:08 +0100240 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
Damien George6ac5dce2014-05-21 19:42:43 +0100241 default: return MP_OBJ_NULL; // op not supported
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200242 }
243}
244
Damien Georgeecc88e92014-08-30 00:35:11 +0100245STATIC 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 +0000246 mp_obj_array_t *lhs = MP_OBJ_TO_PTR(lhs_in);
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300247 switch (op) {
Damien Georgeb2e73112014-11-30 00:00:55 +0000248 case MP_BINARY_OP_ADD: {
Damien Georgec7ca01a2014-11-30 14:01:33 +0000249 // allow to add anything that has the buffer protocol (extension to CPython)
250 mp_buffer_info_t lhs_bufinfo;
251 mp_buffer_info_t rhs_bufinfo;
252 array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ);
253 mp_get_buffer_raise(rhs_in, &rhs_bufinfo, MP_BUFFER_READ);
254
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200255 size_t sz = mp_binary_get_size('@', lhs_bufinfo.typecode, NULL);
Damien Georgec7ca01a2014-11-30 14:01:33 +0000256
257 // convert byte count to element count (in case rhs is not multiple of sz)
Damien Georgeccc52542017-02-16 16:31:43 +1100258 size_t rhs_len = rhs_bufinfo.len / sz;
Damien Georgec7ca01a2014-11-30 14:01:33 +0000259
260 // note: lhs->len is element count of lhs, lhs_bufinfo.len is byte count
261 mp_obj_array_t *res = array_new(lhs_bufinfo.typecode, lhs->len + rhs_len);
262 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 +0000263 return MP_OBJ_FROM_PTR(res);
Damien Georgeb2e73112014-11-30 00:00:55 +0000264 }
265
266 case MP_BINARY_OP_INPLACE_ADD: {
267 #if MICROPY_PY_BUILTINS_MEMORYVIEW
268 if (lhs->base.type == &mp_type_memoryview) {
269 return MP_OBJ_NULL; // op not supported
270 }
271 #endif
Damien George999cedb2015-11-27 17:01:44 +0000272 array_extend(lhs_in, rhs_in);
273 return lhs_in;
Damien Georgeb2e73112014-11-30 00:00:55 +0000274 }
275
Paul Sokolovskyc38809e2016-02-14 18:55:16 +0200276 case MP_BINARY_OP_IN: {
277 /* NOTE `a in b` is `b.__contains__(a)` */
278 mp_buffer_info_t lhs_bufinfo;
279 mp_buffer_info_t rhs_bufinfo;
280
281 // Can search string only in bytearray
282 if (mp_get_buffer(rhs_in, &rhs_bufinfo, MP_BUFFER_READ)) {
283 if (!MP_OBJ_IS_TYPE(lhs_in, &mp_type_bytearray)) {
284 return mp_const_false;
285 }
286 array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ);
287 return mp_obj_new_bool(
288 find_subbytes(lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_bufinfo.len, 1) != NULL);
289 }
290
291 // Otherwise, can only look for a scalar numeric value in an array
292 if (MP_OBJ_IS_INT(rhs_in) || mp_obj_is_float(rhs_in)) {
293 mp_not_implemented("");
294 }
295
296 return mp_const_false;
297 }
298
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300299 case MP_BINARY_OP_EQUAL: {
300 mp_buffer_info_t lhs_bufinfo;
301 mp_buffer_info_t rhs_bufinfo;
302 array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ);
303 if (!mp_get_buffer(rhs_in, &rhs_bufinfo, MP_BUFFER_READ)) {
Damien Georgede3c8062014-10-26 13:20:50 +0000304 return mp_const_false;
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300305 }
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300306 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 +0300307 }
Damien Georgeb2e73112014-11-30 00:00:55 +0000308
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300309 default:
310 return MP_OBJ_NULL; // op not supported
311 }
312}
313
Damien Georgeb2e73112014-11-30 00:00:55 +0000314#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200315STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
Damien Georgeb2e73112014-11-30 00:00:55 +0000316 // self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
Damien George035deae2015-07-02 16:26:57 +0100317 assert((MICROPY_PY_BUILTINS_BYTEARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray))
318 || (MICROPY_PY_ARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_array)));
Damien George999cedb2015-11-27 17:01:44 +0000319 mp_obj_array_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgeb2e73112014-11-30 00:00:55 +0000320
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200321 if (self->free == 0) {
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200322 size_t item_sz = mp_binary_get_size('@', self->typecode, NULL);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200323 // TODO: alloc policy
324 self->free = 8;
Damien George4d77e1a2015-02-27 09:34:51 +0000325 self->items = m_renew(byte, self->items, item_sz * self->len, item_sz * (self->len + self->free));
Paul Sokolovskya2240672014-04-28 00:16:57 +0300326 mp_seq_clear(self->items, self->len + 1, self->len + self->free, item_sz);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200327 }
Damien George04d5e642016-04-07 09:03:33 +0100328 mp_binary_set_val_array(self->typecode, self->items, self->len, arg);
329 // only update length/free if set succeeded
330 self->len++;
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200331 self->free--;
332 return mp_const_none; // return None, as per CPython
333}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200334STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200335
Damien Georgeb2e73112014-11-30 00:00:55 +0000336STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in) {
337 // self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
Damien George035deae2015-07-02 16:26:57 +0100338 assert((MICROPY_PY_BUILTINS_BYTEARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray))
339 || (MICROPY_PY_ARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_array)));
Damien George999cedb2015-11-27 17:01:44 +0000340 mp_obj_array_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgeb2e73112014-11-30 00:00:55 +0000341
Damien Georgec7ca01a2014-11-30 14:01:33 +0000342 // allow to extend by anything that has the buffer protocol (extension to CPython)
343 mp_buffer_info_t arg_bufinfo;
344 mp_get_buffer_raise(arg_in, &arg_bufinfo, MP_BUFFER_READ);
Damien Georgeb2e73112014-11-30 00:00:55 +0000345
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200346 size_t sz = mp_binary_get_size('@', self->typecode, NULL);
Damien Georgeb2e73112014-11-30 00:00:55 +0000347
Damien Georgec7ca01a2014-11-30 14:01:33 +0000348 // convert byte count to element count
Damien Georgeccc52542017-02-16 16:31:43 +1100349 size_t len = arg_bufinfo.len / sz;
Damien Georgec7ca01a2014-11-30 14:01:33 +0000350
Damien Georgeb2e73112014-11-30 00:00:55 +0000351 // make sure we have enough room to extend
Damien Georgec7ca01a2014-11-30 14:01:33 +0000352 // TODO: alloc policy; at the moment we go conservative
353 if (self->free < len) {
Damien George4d77e1a2015-02-27 09:34:51 +0000354 self->items = m_renew(byte, self->items, (self->len + self->free) * sz, (self->len + len) * sz);
Damien Georgec7ca01a2014-11-30 14:01:33 +0000355 self->free = 0;
356 } else {
357 self->free -= len;
Damien Georgeb2e73112014-11-30 00:00:55 +0000358 }
359
360 // extend
Damien Georgec7ca01a2014-11-30 14:01:33 +0000361 mp_seq_copy((byte*)self->items + self->len * sz, arg_bufinfo.buf, len * sz, byte);
362 self->len += len;
Damien Georgeb2e73112014-11-30 00:00:55 +0000363
364 return mp_const_none;
365}
366STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_extend_obj, array_extend);
367#endif
368
Damien George729f7b42014-04-17 22:10:53 +0100369STATIC 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 +0100370 if (value == MP_OBJ_NULL) {
Damien George32ca1642014-04-18 22:04:06 +0100371 // delete item
372 // TODO implement
Paul Sokolovsky26905252014-04-20 20:58:33 +0300373 // TODO: confirmed that both bytearray and array.array support
374 // slice deletion
Damien George6ac5dce2014-05-21 19:42:43 +0100375 return MP_OBJ_NULL; // op not supported
Damien George729f7b42014-04-17 22:10:53 +0100376 } else {
Damien George999cedb2015-11-27 17:01:44 +0000377 mp_obj_array_t *o = MP_OBJ_TO_PTR(self_in);
Damien Georgec49ddb92014-06-01 13:49:35 +0100378 if (0) {
379#if MICROPY_PY_BUILTINS_SLICE
380 } else if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300381 mp_bound_slice_t slice;
382 if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) {
Damien George821b7f22015-09-03 23:14:06 +0100383 mp_not_implemented("only slices with step=1 (aka None) are supported");
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300384 }
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200385 if (value != MP_OBJ_SENTINEL) {
386 #if MICROPY_PY_ARRAY_SLICE_ASSIGN
387 // Assign
Damien Georgeccc52542017-02-16 16:31:43 +1100388 size_t src_len;
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300389 void *src_items;
Delio Brignoli32aba402015-06-03 07:07:35 +0200390 size_t item_sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
Damien George999cedb2015-11-27 17:01:44 +0000391 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 +0100392 // value is array, bytearray or memoryview
Damien George999cedb2015-11-27 17:01:44 +0000393 mp_obj_array_t *src_slice = MP_OBJ_TO_PTR(value);
Delio Brignoli32aba402015-06-03 07:07:35 +0200394 if (item_sz != mp_binary_get_size('@', src_slice->typecode & TYPECODE_MASK, NULL)) {
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300395 compat_error:
Damien George7d0d7212016-10-17 12:17:37 +1100396 mp_raise_msg(&mp_type_ValueError, "lhs and rhs should be compatible");
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300397 }
398 src_len = src_slice->len;
399 src_items = src_slice->items;
Damien George4915c2b2015-07-20 16:12:26 +0100400 #if MICROPY_PY_BUILTINS_MEMORYVIEW
401 if (MP_OBJ_IS_TYPE(value, &mp_type_memoryview)) {
402 src_items = (uint8_t*)src_items + (src_slice->free * item_sz);
403 }
404 #endif
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300405 } else if (MP_OBJ_IS_TYPE(value, &mp_type_bytes)) {
406 if (item_sz != 1) {
407 goto compat_error;
408 }
409 mp_buffer_info_t bufinfo;
410 mp_get_buffer_raise(value, &bufinfo, MP_BUFFER_READ);
411 src_len = bufinfo.len;
412 src_items = bufinfo.buf;
413 } else {
414 mp_not_implemented("array/bytes required on right side");
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200415 }
416
417 // TODO: check src/dst compat
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300418 mp_int_t len_adj = src_len - (slice.stop - slice.start);
Delio Brignoli6a388aa2015-06-06 20:14:08 +0200419 uint8_t* dest_items = o->items;
420 #if MICROPY_PY_BUILTINS_MEMORYVIEW
421 if (o->base.type == &mp_type_memoryview) {
422 if (len_adj != 0) {
Delio Brignoli32aba402015-06-03 07:07:35 +0200423 goto compat_error;
424 }
Delio Brignoli6a388aa2015-06-06 20:14:08 +0200425 dest_items += o->free * item_sz;
426 }
427 #endif
428 if (len_adj > 0) {
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200429 if (len_adj > o->free) {
430 // TODO: alloc policy; at the moment we go conservative
Damien Georged8914522015-02-27 09:54:12 +0000431 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 +0200432 o->free = 0;
Damien George77f85db2016-03-14 23:12:54 +0000433 dest_items = o->items;
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200434 }
Delio Brignoli6a388aa2015-06-06 20:14:08 +0200435 mp_seq_replace_slice_grow_inplace(dest_items, o->len,
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300436 slice.start, slice.stop, src_items, src_len, len_adj, item_sz);
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200437 } else {
Delio Brignoli6a388aa2015-06-06 20:14:08 +0200438 mp_seq_replace_slice_no_grow(dest_items, o->len,
439 slice.start, slice.stop, src_items, src_len, item_sz);
440 // Clear "freed" elements at the end of list
441 // TODO: This is actually only needed for typecode=='O'
442 mp_seq_clear(dest_items, o->len + len_adj, o->len, item_sz);
443 // TODO: alloc policy after shrinking
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200444 }
445 o->len += len_adj;
446 return mp_const_none;
447 #else
448 return MP_OBJ_NULL; // op not supported
449 #endif
450 }
451
Damien Georgedd4f4532014-10-23 13:34:35 +0100452 mp_obj_array_t *res;
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200453 size_t sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300454 assert(sz > 0);
Damien Georgedd4f4532014-10-23 13:34:35 +0100455 if (0) {
456 // dummy
457 #if MICROPY_PY_BUILTINS_MEMORYVIEW
458 } else if (o->base.type == &mp_type_memoryview) {
459 res = m_new_obj(mp_obj_array_t);
460 *res = *o;
Damien Georgede3c8062014-10-26 13:20:50 +0000461 res->free += slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100462 res->len = slice.stop - slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100463 #endif
464 } else {
465 res = array_new(o->typecode, slice.stop - slice.start);
stijn49c47da2014-10-28 17:20:52 +0100466 memcpy(res->items, (uint8_t*)o->items + slice.start * sz, (slice.stop - slice.start) * sz);
Damien Georgedd4f4532014-10-23 13:34:35 +0100467 }
Damien George999cedb2015-11-27 17:01:44 +0000468 return MP_OBJ_FROM_PTR(res);
Damien Georgec49ddb92014-06-01 13:49:35 +0100469#endif
Damien George729f7b42014-04-17 22:10:53 +0100470 } else {
Damien George42f3de92014-10-03 17:44:14 +0000471 mp_uint_t index = mp_get_index(o->base.type, o->len, index_in, false);
Damien Georgede3c8062014-10-26 13:20:50 +0000472 #if MICROPY_PY_BUILTINS_MEMORYVIEW
473 if (o->base.type == &mp_type_memoryview) {
474 index += o->free;
475 if (value != MP_OBJ_SENTINEL && (o->typecode & 0x80) == 0) {
476 // store to read-only memoryview
Damien Georgedd4f4532014-10-23 13:34:35 +0100477 return MP_OBJ_NULL;
478 }
Damien Georgede3c8062014-10-26 13:20:50 +0000479 }
480 #endif
481 if (value == MP_OBJ_SENTINEL) {
482 // load
483 return mp_binary_get_val_array(o->typecode & TYPECODE_MASK, o->items, index);
484 } else {
485 // store
486 mp_binary_set_val_array(o->typecode & TYPECODE_MASK, o->items, index, value);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300487 return mp_const_none;
488 }
Damien George729f7b42014-04-17 22:10:53 +0100489 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100490 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200491}
492
Damien George4d917232014-08-30 14:28:06 +0100493STATIC 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 +0000494 mp_obj_array_t *o = MP_OBJ_TO_PTR(o_in);
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200495 size_t sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
Damien Georgede3c8062014-10-26 13:20:50 +0000496 bufinfo->buf = o->items;
497 bufinfo->len = o->len * sz;
498 bufinfo->typecode = o->typecode & TYPECODE_MASK;
Damien Georgedd4f4532014-10-23 13:34:35 +0100499 #if MICROPY_PY_BUILTINS_MEMORYVIEW
Damien Georgede3c8062014-10-26 13:20:50 +0000500 if (o->base.type == &mp_type_memoryview) {
501 if ((o->typecode & 0x80) == 0 && (flags & MP_BUFFER_WRITE)) {
502 // read-only memoryview
503 return 1;
504 }
Damien Georgeccc52542017-02-16 16:31:43 +1100505 bufinfo->buf = (uint8_t*)bufinfo->buf + (size_t)o->free * sz;
Damien Georgedd4f4532014-10-23 13:34:35 +0100506 }
Damien George3a2171e2015-09-04 16:53:46 +0100507 #else
508 (void)flags;
Damien Georgedd4f4532014-10-23 13:34:35 +0100509 #endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200510 return 0;
511}
512
Damien Georgedd4f4532014-10-23 13:34:35 +0100513#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Damien Georgecbf76742015-11-27 13:38:15 +0000514STATIC const mp_rom_map_elem_t array_locals_dict_table[] = {
515 { MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&array_append_obj) },
516 { MP_ROM_QSTR(MP_QSTR_extend), MP_ROM_PTR(&array_extend_obj) },
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200517};
518
Damien George9b196cd2014-03-26 21:47:19 +0000519STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
Damien Georgedd4f4532014-10-23 13:34:35 +0100520#endif
Damien George9b196cd2014-03-26 21:47:19 +0000521
Damien Georgedd4f4532014-10-23 13:34:35 +0100522#if MICROPY_PY_ARRAY
Damien Georgecaac5422014-03-25 14:18:18 +0000523const mp_obj_type_t mp_type_array = {
Damien Georgec5966122014-02-15 16:10:44 +0000524 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000525 .name = MP_QSTR_array,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200526 .print = array_print,
527 .make_new = array_make_new,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200528 .getiter = array_iterator_new,
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200529 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300530 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100531 .subscr = array_subscr,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200532 .buffer_p = { .get_buffer = array_get_buffer },
Damien George999cedb2015-11-27 17:01:44 +0000533 .locals_dict = (mp_obj_dict_t*)&array_locals_dict,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200534};
Damien Georgedd4f4532014-10-23 13:34:35 +0100535#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200536
Damien Georgedd4f4532014-10-23 13:34:35 +0100537#if MICROPY_PY_BUILTINS_BYTEARRAY
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300538const mp_obj_type_t mp_type_bytearray = {
539 { &mp_type_type },
540 .name = MP_QSTR_bytearray,
541 .print = array_print,
542 .make_new = bytearray_make_new,
543 .getiter = array_iterator_new,
544 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300545 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100546 .subscr = array_subscr,
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300547 .buffer_p = { .get_buffer = array_get_buffer },
Damien George999cedb2015-11-27 17:01:44 +0000548 .locals_dict = (mp_obj_dict_t*)&array_locals_dict,
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300549};
Damien Georgedd4f4532014-10-23 13:34:35 +0100550#endif
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300551
Damien Georgedd4f4532014-10-23 13:34:35 +0100552#if MICROPY_PY_BUILTINS_MEMORYVIEW
553const mp_obj_type_t mp_type_memoryview = {
554 { &mp_type_type },
555 .name = MP_QSTR_memoryview,
556 .make_new = memoryview_make_new,
557 .getiter = array_iterator_new,
558 .unary_op = array_unary_op,
559 .binary_op = array_binary_op,
560 .subscr = array_subscr,
561 .buffer_p = { .get_buffer = array_get_buffer },
562};
563#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200564
Damien Georgedd4f4532014-10-23 13:34:35 +0100565/* unused
Damien Georgeccc52542017-02-16 16:31:43 +1100566size_t mp_obj_array_len(mp_obj_t self_in) {
Paul Sokolovsky33996682014-01-21 23:30:10 +0200567 return ((mp_obj_array_t *)self_in)->len;
568}
Damien Georgedd4f4532014-10-23 13:34:35 +0100569*/
Paul Sokolovsky33996682014-01-21 23:30:10 +0200570
Damien Georgedd4f4532014-10-23 13:34:35 +0100571#if MICROPY_PY_BUILTINS_BYTEARRAY
Damien Georgeccc52542017-02-16 16:31:43 +1100572mp_obj_t mp_obj_new_bytearray(size_t n, void *items) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200573 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
574 memcpy(o->items, items, n);
Damien George999cedb2015-11-27 17:01:44 +0000575 return MP_OBJ_FROM_PTR(o);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200576}
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200577
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200578// Create bytearray which references specified memory area
Damien Georgeccc52542017-02-16 16:31:43 +1100579mp_obj_t mp_obj_new_bytearray_by_ref(size_t n, void *items) {
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200580 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgedd4f4532014-10-23 13:34:35 +0100581 o->base.type = &mp_type_bytearray;
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200582 o->typecode = BYTEARRAY_TYPECODE;
583 o->free = 0;
584 o->len = n;
585 o->items = items;
Damien George999cedb2015-11-27 17:01:44 +0000586 return MP_OBJ_FROM_PTR(o);
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200587}
Damien Georgedd4f4532014-10-23 13:34:35 +0100588#endif
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200589
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200590/******************************************************************************/
Damien Georgedd4f4532014-10-23 13:34:35 +0100591// array iterator
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200592
593typedef struct _mp_obj_array_it_t {
594 mp_obj_base_t base;
595 mp_obj_array_t *array;
Damien Georgeccc52542017-02-16 16:31:43 +1100596 size_t offset;
597 size_t cur;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200598} mp_obj_array_it_t;
599
Damien Georgecaac5422014-03-25 14:18:18 +0000600STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000601 mp_obj_array_it_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200602 if (self->cur < self->array->len) {
Damien Georgede3c8062014-10-26 13:20:50 +0000603 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 +0200604 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100605 return MP_OBJ_STOP_ITERATION;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200606 }
607}
608
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200609STATIC const mp_obj_type_t array_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000610 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000611 .name = MP_QSTR_iterator,
Damien Georgeae8d8672016-01-09 23:14:54 +0000612 .getiter = mp_identity_getiter,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200613 .iternext = array_it_iternext,
614};
615
Damien Georgeae8d8672016-01-09 23:14:54 +0000616STATIC mp_obj_t array_iterator_new(mp_obj_t array_in, mp_obj_iter_buf_t *iter_buf) {
617 assert(sizeof(mp_obj_array_t) <= sizeof(mp_obj_iter_buf_t));
Damien George999cedb2015-11-27 17:01:44 +0000618 mp_obj_array_t *array = MP_OBJ_TO_PTR(array_in);
Damien Georgeae8d8672016-01-09 23:14:54 +0000619 mp_obj_array_it_t *o = (mp_obj_array_it_t*)iter_buf;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200620 o->base.type = &array_it_type;
621 o->array = array;
Damien Georgeae8d8672016-01-09 23:14:54 +0000622 o->offset = 0;
623 o->cur = 0;
Damien Georgede3c8062014-10-26 13:20:50 +0000624 #if MICROPY_PY_BUILTINS_MEMORYVIEW
625 if (array->base.type == &mp_type_memoryview) {
626 o->offset = array->free;
627 }
628 #endif
Damien George999cedb2015-11-27 17:01:44 +0000629 return MP_OBJ_FROM_PTR(o);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200630}
Paul Sokolovskycb78f862014-06-27 20:39:09 +0300631
Damien Georgedd4f4532014-10-23 13:34:35 +0100632#endif // MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW