blob: 58285c86605875f8ac0df1b0188a771b9717ac00 [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 Sokolovsky427905c2014-01-18 19:24:47 +020037
Damien Georgedd4f4532014-10-23 13:34:35 +010038#if MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW
Paul Sokolovskycb78f862014-06-27 20:39:09 +030039
Damien Georgede3c8062014-10-26 13:20:50 +000040// About memoryview object: We want to reuse as much code as possible from
41// array, and keep the memoryview object 4 words in size so it fits in 1 GC
42// block. Also, memoryview must keep a pointer to the base of the buffer so
43// that the buffer is not GC'd if the original parent object is no longer
44// around (we are assuming that all memoryview'able objects return a pointer
45// which points to the start of a GC chunk). Given the above constraints we
46// do the following:
47// - typecode high bit is set if the buffer is read-write (else read-only)
48// - free is the offset in elements to the first item in the memoryview
49// - len is the length in elements
50// - items points to the start of the original buffer
51// Note that we don't handle the case where the original buffer might change
52// size due to a resize of the original parent object.
53
54// make (& TYPECODE_MASK) a null operation if memorview not enabled
55#if MICROPY_PY_BUILTINS_MEMORYVIEW
56#define TYPECODE_MASK (0x7f)
57#else
Paul Sokolovsky16b1f5e2015-03-04 23:01:44 +020058#define TYPECODE_MASK (~(mp_uint_t)0)
Damien Georgede3c8062014-10-26 13:20:50 +000059#endif
60
Paul Sokolovsky427905c2014-01-18 19:24:47 +020061typedef struct _mp_obj_array_t {
62 mp_obj_base_t base;
Damien George40f3c022014-07-03 13:25:24 +010063 mp_uint_t typecode : 8;
Damien George51047752014-02-26 17:40:52 +000064 // free is number of unused elements after len used elements
65 // alloc size = len + free
Damien George40f3c022014-07-03 13:25:24 +010066 mp_uint_t free : (8 * sizeof(mp_uint_t) - 8);
67 mp_uint_t len; // in elements
Paul Sokolovsky427905c2014-01-18 19:24:47 +020068 void *items;
69} mp_obj_array_t;
70
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020071STATIC mp_obj_t array_iterator_new(mp_obj_t array_in);
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020072STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg);
Damien Georgeb2e73112014-11-30 00:00:55 +000073STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in);
Damien George4d917232014-08-30 14:28:06 +010074STATIC 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 +020075
76/******************************************************************************/
Damien Georgedd4f4532014-10-23 13:34:35 +010077// array
Paul Sokolovsky427905c2014-01-18 19:24:47 +020078
Damien Georgedd4f4532014-10-23 13:34:35 +010079#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Damien George7f9d1d62015-04-09 23:56:15 +010080STATIC 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 +000081 (void)kind;
Damien George999cedb2015-11-27 17:01:44 +000082 mp_obj_array_t *o = MP_OBJ_TO_PTR(o_in);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020083 if (o->typecode == BYTEARRAY_TYPECODE) {
Damien George7f9d1d62015-04-09 23:56:15 +010084 mp_print_str(print, "bytearray(b");
85 mp_str_print_quoted(print, o->items, o->len, true);
Paul Sokolovsky427905c2014-01-18 19:24:47 +020086 } else {
Damien George7f9d1d62015-04-09 23:56:15 +010087 mp_printf(print, "array('%c'", o->typecode);
Paul Sokolovsky18014212014-01-28 03:40:48 +020088 if (o->len > 0) {
Damien George7f9d1d62015-04-09 23:56:15 +010089 mp_print_str(print, ", [");
Damien George42f3de92014-10-03 17:44:14 +000090 for (mp_uint_t i = 0; i < o->len; i++) {
Paul Sokolovsky18014212014-01-28 03:40:48 +020091 if (i > 0) {
Damien George7f9d1d62015-04-09 23:56:15 +010092 mp_print_str(print, ", ");
Paul Sokolovsky18014212014-01-28 03:40:48 +020093 }
Damien George7f9d1d62015-04-09 23:56:15 +010094 mp_obj_print_helper(print, mp_binary_get_val_array(o->typecode, o->items, i), PRINT_REPR);
Paul Sokolovsky7e652af2014-01-28 03:14:20 +020095 }
Damien George7f9d1d62015-04-09 23:56:15 +010096 mp_print_str(print, "]");
Paul Sokolovsky427905c2014-01-18 19:24:47 +020097 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +020098 }
Damien George7f9d1d62015-04-09 23:56:15 +010099 mp_print_str(print, ")");
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200100}
Damien Georgedd4f4532014-10-23 13:34:35 +0100101#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200102
Damien Georgedd4f4532014-10-23 13:34:35 +0100103#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
104STATIC mp_obj_array_t *array_new(char typecode, mp_uint_t n) {
105 int typecode_size = mp_binary_get_size('@', typecode, NULL);
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200106 if (typecode_size == 0) {
Damien Georgedd4f4532014-10-23 13:34:35 +0100107 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bad typecode"));
108 }
109 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
110 #if MICROPY_PY_BUILTINS_BYTEARRAY && MICROPY_PY_ARRAY
111 o->base.type = (typecode == BYTEARRAY_TYPECODE) ? &mp_type_bytearray : &mp_type_array;
112 #elif MICROPY_PY_BUILTINS_BYTEARRAY
113 o->base.type = &mp_type_bytearray;
114 #else
115 o->base.type = &mp_type_array;
116 #endif
117 o->typecode = typecode;
118 o->free = 0;
119 o->len = n;
Damien George4d77e1a2015-02-27 09:34:51 +0000120 o->items = m_new(byte, typecode_size * o->len);
Damien Georgedd4f4532014-10-23 13:34:35 +0100121 return o;
122}
123#endif
124
125#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200126STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
Damien George32ef3a32014-12-04 15:46:14 +0000127 // bytearrays can be raw-initialised from anything with the buffer protocol
128 // other arrays can only be raw-initialised from bytes and bytearray objects
129 mp_buffer_info_t bufinfo;
130 if (((MICROPY_PY_BUILTINS_BYTEARRAY
131 && typecode == BYTEARRAY_TYPECODE)
132 || (MICROPY_PY_ARRAY
133 && (MP_OBJ_IS_TYPE(initializer, &mp_type_bytes)
Damien George035deae2015-07-02 16:26:57 +0100134 || (MICROPY_PY_BUILTINS_BYTEARRAY && MP_OBJ_IS_TYPE(initializer, &mp_type_bytearray)))))
Damien George32ef3a32014-12-04 15:46:14 +0000135 && mp_get_buffer(initializer, &bufinfo, MP_BUFFER_READ)) {
136 // construct array from raw bytes
137 // we round-down the len to make it a multiple of sz (CPython raises error)
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200138 size_t sz = mp_binary_get_size('@', typecode, NULL);
Damien George32ef3a32014-12-04 15:46:14 +0000139 mp_uint_t len = bufinfo.len / sz;
140 mp_obj_array_t *o = array_new(typecode, len);
141 memcpy(o->items, bufinfo.buf, len * sz);
Damien George999cedb2015-11-27 17:01:44 +0000142 return MP_OBJ_FROM_PTR(o);
Damien George32ef3a32014-12-04 15:46:14 +0000143 }
144
145 mp_uint_t len;
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200146 // Try to create array of exact len if initializer len is known
147 mp_obj_t len_in = mp_obj_len_maybe(initializer);
148 if (len_in == MP_OBJ_NULL) {
149 len = 0;
150 } else {
151 len = MP_OBJ_SMALL_INT_VALUE(len_in);
152 }
153
154 mp_obj_array_t *array = array_new(typecode, len);
155
Damien Georged17926d2014-03-30 13:35:08 +0100156 mp_obj_t iterable = mp_getiter(initializer);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200157 mp_obj_t item;
Damien George42f3de92014-10-03 17:44:14 +0000158 mp_uint_t i = 0;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100159 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200160 if (len == 0) {
Damien George999cedb2015-11-27 17:01:44 +0000161 array_append(MP_OBJ_FROM_PTR(array), item);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200162 } else {
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300163 mp_binary_set_val_array(typecode, array->items, i++, item);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200164 }
165 }
166
Damien George999cedb2015-11-27 17:01:44 +0000167 return MP_OBJ_FROM_PTR(array);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200168}
Damien Georgedd4f4532014-10-23 13:34:35 +0100169#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200170
Damien Georgedd4f4532014-10-23 13:34:35 +0100171#if MICROPY_PY_ARRAY
Damien George5b3f0b72016-01-03 15:55:55 +0000172STATIC 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 +0000173 (void)type_in;
Damien Georgea3f94e02014-04-20 00:13:22 +0100174 mp_arg_check_num(n_args, n_kw, 1, 2, false);
Damien George32ca1642014-04-18 22:04:06 +0100175
176 // get typecode
Damien Georged182b982014-08-30 14:19:41 +0100177 mp_uint_t l;
Damien George698ec212014-02-08 18:17:23 +0000178 const char *typecode = mp_obj_str_get_data(args[0], &l);
Paul Sokolovsky11973b42014-01-28 02:31:52 +0200179
Damien George32ca1642014-04-18 22:04:06 +0100180 if (n_args == 1) {
181 // 1 arg: make an empty array
Damien George999cedb2015-11-27 17:01:44 +0000182 return MP_OBJ_FROM_PTR(array_new(*typecode, 0));
Damien George32ca1642014-04-18 22:04:06 +0100183 } else {
Damien George32ef3a32014-12-04 15:46:14 +0000184 // 2 args: construct the array from the given object
Damien George32ca1642014-04-18 22:04:06 +0100185 return array_construct(*typecode, args[1]);
186 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200187}
Damien Georgedd4f4532014-10-23 13:34:35 +0100188#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200189
Damien Georgedd4f4532014-10-23 13:34:35 +0100190#if MICROPY_PY_BUILTINS_BYTEARRAY
Damien George5b3f0b72016-01-03 15:55:55 +0000191STATIC 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 +0000192 (void)type_in;
Damien Georgea3f94e02014-04-20 00:13:22 +0100193 mp_arg_check_num(n_args, n_kw, 0, 1, false);
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300194
Damien George32ca1642014-04-18 22:04:06 +0100195 if (n_args == 0) {
196 // no args: construct an empty bytearray
Damien George999cedb2015-11-27 17:01:44 +0000197 return MP_OBJ_FROM_PTR(array_new(BYTEARRAY_TYPECODE, 0));
Paul Sokolovsky343ca1e2015-01-04 17:19:16 +0200198 } else if (MP_OBJ_IS_INT(args[0])) {
Damien George32ca1642014-04-18 22:04:06 +0100199 // 1 arg, an integer: construct a blank bytearray of that length
Paul Sokolovsky343ca1e2015-01-04 17:19:16 +0200200 mp_uint_t len = mp_obj_get_int(args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300201 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len);
202 memset(o->items, 0, len);
Damien George999cedb2015-11-27 17:01:44 +0000203 return MP_OBJ_FROM_PTR(o);
Damien George32ca1642014-04-18 22:04:06 +0100204 } else {
Damien George32ef3a32014-12-04 15:46:14 +0000205 // 1 arg: construct the bytearray from that
Damien George32ca1642014-04-18 22:04:06 +0100206 return array_construct(BYTEARRAY_TYPECODE, args[0]);
Paul Sokolovskyb9cf3d32014-04-08 04:42:44 +0300207 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200208}
Damien Georgedd4f4532014-10-23 13:34:35 +0100209#endif
210
211#if MICROPY_PY_BUILTINS_MEMORYVIEW
Paul Sokolovskycb0fc062015-03-06 21:35:26 +0200212
213mp_obj_t mp_obj_new_memoryview(byte typecode, mp_uint_t nitems, void *items) {
214 mp_obj_array_t *self = m_new_obj(mp_obj_array_t);
215 self->base.type = &mp_type_memoryview;
216 self->typecode = typecode;
217 self->free = 0;
218 self->len = nitems;
219 self->items = items;
Damien George999cedb2015-11-27 17:01:44 +0000220 return MP_OBJ_FROM_PTR(self);
Paul Sokolovskycb0fc062015-03-06 21:35:26 +0200221}
222
Damien George5b3f0b72016-01-03 15:55:55 +0000223STATIC 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 +0000224 (void)type_in;
225
Damien Georgedd4f4532014-10-23 13:34:35 +0100226 // TODO possibly allow memoryview constructor to take start/stop so that one
227 // can do memoryview(b, 4, 8) instead of memoryview(b)[4:8] (uses less RAM)
228
229 mp_arg_check_num(n_args, n_kw, 1, 1, false);
230
231 mp_buffer_info_t bufinfo;
232 mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
233
Damien George999cedb2015-11-27 17:01:44 +0000234 mp_obj_array_t *self = MP_OBJ_TO_PTR(mp_obj_new_memoryview(bufinfo.typecode,
Paul Sokolovskycb0fc062015-03-06 21:35:26 +0200235 bufinfo.len / mp_binary_get_size('@', bufinfo.typecode, NULL),
Damien George999cedb2015-11-27 17:01:44 +0000236 bufinfo.buf));
Damien Georgedd4f4532014-10-23 13:34:35 +0100237
238 // test if the object can be written to
239 if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_RW)) {
Damien Georgede3c8062014-10-26 13:20:50 +0000240 self->typecode |= 0x80; // used to indicate writable buffer
Damien Georgedd4f4532014-10-23 13:34:35 +0100241 }
242
Damien George999cedb2015-11-27 17:01:44 +0000243 return MP_OBJ_FROM_PTR(self);
Damien Georgedd4f4532014-10-23 13:34:35 +0100244}
245#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200246
Damien Georgeecc88e92014-08-30 00:35:11 +0100247STATIC mp_obj_t array_unary_op(mp_uint_t op, mp_obj_t o_in) {
Damien George999cedb2015-11-27 17:01:44 +0000248 mp_obj_array_t *o = MP_OBJ_TO_PTR(o_in);
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200249 switch (op) {
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300250 case MP_UNARY_OP_BOOL: return mp_obj_new_bool(o->len != 0);
Damien Georged17926d2014-03-30 13:35:08 +0100251 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
Damien George6ac5dce2014-05-21 19:42:43 +0100252 default: return MP_OBJ_NULL; // op not supported
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200253 }
254}
255
Damien Georgeecc88e92014-08-30 00:35:11 +0100256STATIC 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 +0000257 mp_obj_array_t *lhs = MP_OBJ_TO_PTR(lhs_in);
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300258 switch (op) {
Damien Georgeb2e73112014-11-30 00:00:55 +0000259 case MP_BINARY_OP_ADD: {
Damien Georgec7ca01a2014-11-30 14:01:33 +0000260 // allow to add anything that has the buffer protocol (extension to CPython)
261 mp_buffer_info_t lhs_bufinfo;
262 mp_buffer_info_t rhs_bufinfo;
263 array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ);
264 mp_get_buffer_raise(rhs_in, &rhs_bufinfo, MP_BUFFER_READ);
265
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200266 size_t sz = mp_binary_get_size('@', lhs_bufinfo.typecode, NULL);
Damien Georgec7ca01a2014-11-30 14:01:33 +0000267
268 // convert byte count to element count (in case rhs is not multiple of sz)
269 mp_uint_t rhs_len = rhs_bufinfo.len / sz;
270
271 // note: lhs->len is element count of lhs, lhs_bufinfo.len is byte count
272 mp_obj_array_t *res = array_new(lhs_bufinfo.typecode, lhs->len + rhs_len);
273 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 +0000274 return MP_OBJ_FROM_PTR(res);
Damien Georgeb2e73112014-11-30 00:00:55 +0000275 }
276
277 case MP_BINARY_OP_INPLACE_ADD: {
278 #if MICROPY_PY_BUILTINS_MEMORYVIEW
279 if (lhs->base.type == &mp_type_memoryview) {
280 return MP_OBJ_NULL; // op not supported
281 }
282 #endif
Damien George999cedb2015-11-27 17:01:44 +0000283 array_extend(lhs_in, rhs_in);
284 return lhs_in;
Damien Georgeb2e73112014-11-30 00:00:55 +0000285 }
286
Paul Sokolovskyc38809e2016-02-14 18:55:16 +0200287 case MP_BINARY_OP_IN: {
288 /* NOTE `a in b` is `b.__contains__(a)` */
289 mp_buffer_info_t lhs_bufinfo;
290 mp_buffer_info_t rhs_bufinfo;
291
292 // Can search string only in bytearray
293 if (mp_get_buffer(rhs_in, &rhs_bufinfo, MP_BUFFER_READ)) {
294 if (!MP_OBJ_IS_TYPE(lhs_in, &mp_type_bytearray)) {
295 return mp_const_false;
296 }
297 array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ);
298 return mp_obj_new_bool(
299 find_subbytes(lhs_bufinfo.buf, lhs_bufinfo.len, rhs_bufinfo.buf, rhs_bufinfo.len, 1) != NULL);
300 }
301
302 // Otherwise, can only look for a scalar numeric value in an array
303 if (MP_OBJ_IS_INT(rhs_in) || mp_obj_is_float(rhs_in)) {
304 mp_not_implemented("");
305 }
306
307 return mp_const_false;
308 }
309
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300310 case MP_BINARY_OP_EQUAL: {
311 mp_buffer_info_t lhs_bufinfo;
312 mp_buffer_info_t rhs_bufinfo;
313 array_get_buffer(lhs_in, &lhs_bufinfo, MP_BUFFER_READ);
314 if (!mp_get_buffer(rhs_in, &rhs_bufinfo, MP_BUFFER_READ)) {
Damien Georgede3c8062014-10-26 13:20:50 +0000315 return mp_const_false;
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300316 }
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300317 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 +0300318 }
Damien Georgeb2e73112014-11-30 00:00:55 +0000319
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300320 default:
321 return MP_OBJ_NULL; // op not supported
322 }
323}
324
Damien Georgeb2e73112014-11-30 00:00:55 +0000325#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200326STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
Damien Georgeb2e73112014-11-30 00:00:55 +0000327 // self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
Damien George035deae2015-07-02 16:26:57 +0100328 assert((MICROPY_PY_BUILTINS_BYTEARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray))
329 || (MICROPY_PY_ARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_array)));
Damien George999cedb2015-11-27 17:01:44 +0000330 mp_obj_array_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgeb2e73112014-11-30 00:00:55 +0000331
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200332 if (self->free == 0) {
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200333 size_t item_sz = mp_binary_get_size('@', self->typecode, NULL);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200334 // TODO: alloc policy
335 self->free = 8;
Damien George4d77e1a2015-02-27 09:34:51 +0000336 self->items = m_renew(byte, self->items, item_sz * self->len, item_sz * (self->len + self->free));
Paul Sokolovskya2240672014-04-28 00:16:57 +0300337 mp_seq_clear(self->items, self->len + 1, self->len + self->free, item_sz);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200338 }
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300339 mp_binary_set_val_array(self->typecode, self->items, self->len++, arg);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200340 self->free--;
341 return mp_const_none; // return None, as per CPython
342}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200343STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200344
Damien Georgeb2e73112014-11-30 00:00:55 +0000345STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in) {
346 // self is not a memoryview, so we don't need to use (& TYPECODE_MASK)
Damien George035deae2015-07-02 16:26:57 +0100347 assert((MICROPY_PY_BUILTINS_BYTEARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray))
348 || (MICROPY_PY_ARRAY && MP_OBJ_IS_TYPE(self_in, &mp_type_array)));
Damien George999cedb2015-11-27 17:01:44 +0000349 mp_obj_array_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgeb2e73112014-11-30 00:00:55 +0000350
Damien Georgec7ca01a2014-11-30 14:01:33 +0000351 // allow to extend by anything that has the buffer protocol (extension to CPython)
352 mp_buffer_info_t arg_bufinfo;
353 mp_get_buffer_raise(arg_in, &arg_bufinfo, MP_BUFFER_READ);
Damien Georgeb2e73112014-11-30 00:00:55 +0000354
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200355 size_t sz = mp_binary_get_size('@', self->typecode, NULL);
Damien Georgeb2e73112014-11-30 00:00:55 +0000356
Damien Georgec7ca01a2014-11-30 14:01:33 +0000357 // convert byte count to element count
358 mp_uint_t len = arg_bufinfo.len / sz;
359
Damien Georgeb2e73112014-11-30 00:00:55 +0000360 // make sure we have enough room to extend
Damien Georgec7ca01a2014-11-30 14:01:33 +0000361 // TODO: alloc policy; at the moment we go conservative
362 if (self->free < len) {
Damien George4d77e1a2015-02-27 09:34:51 +0000363 self->items = m_renew(byte, self->items, (self->len + self->free) * sz, (self->len + len) * sz);
Damien Georgec7ca01a2014-11-30 14:01:33 +0000364 self->free = 0;
365 } else {
366 self->free -= len;
Damien Georgeb2e73112014-11-30 00:00:55 +0000367 }
368
369 // extend
Damien Georgec7ca01a2014-11-30 14:01:33 +0000370 mp_seq_copy((byte*)self->items + self->len * sz, arg_bufinfo.buf, len * sz, byte);
371 self->len += len;
Damien Georgeb2e73112014-11-30 00:00:55 +0000372
373 return mp_const_none;
374}
375STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_extend_obj, array_extend);
376#endif
377
Damien George729f7b42014-04-17 22:10:53 +0100378STATIC 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 +0100379 if (value == MP_OBJ_NULL) {
Damien George32ca1642014-04-18 22:04:06 +0100380 // delete item
381 // TODO implement
Paul Sokolovsky26905252014-04-20 20:58:33 +0300382 // TODO: confirmed that both bytearray and array.array support
383 // slice deletion
Damien George6ac5dce2014-05-21 19:42:43 +0100384 return MP_OBJ_NULL; // op not supported
Damien George729f7b42014-04-17 22:10:53 +0100385 } else {
Damien George999cedb2015-11-27 17:01:44 +0000386 mp_obj_array_t *o = MP_OBJ_TO_PTR(self_in);
Damien Georgec49ddb92014-06-01 13:49:35 +0100387 if (0) {
388#if MICROPY_PY_BUILTINS_SLICE
389 } else if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300390 mp_bound_slice_t slice;
391 if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) {
Damien George821b7f22015-09-03 23:14:06 +0100392 mp_not_implemented("only slices with step=1 (aka None) are supported");
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300393 }
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200394 if (value != MP_OBJ_SENTINEL) {
395 #if MICROPY_PY_ARRAY_SLICE_ASSIGN
396 // Assign
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300397 mp_uint_t src_len;
398 void *src_items;
Delio Brignoli32aba402015-06-03 07:07:35 +0200399 size_t item_sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
Damien George999cedb2015-11-27 17:01:44 +0000400 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 +0100401 // value is array, bytearray or memoryview
Damien George999cedb2015-11-27 17:01:44 +0000402 mp_obj_array_t *src_slice = MP_OBJ_TO_PTR(value);
Delio Brignoli32aba402015-06-03 07:07:35 +0200403 if (item_sz != mp_binary_get_size('@', src_slice->typecode & TYPECODE_MASK, NULL)) {
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300404 compat_error:
Delio Brignoli32aba402015-06-03 07:07:35 +0200405 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "lhs and rhs should be compatible"));
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300406 }
407 src_len = src_slice->len;
408 src_items = src_slice->items;
Damien George4915c2b2015-07-20 16:12:26 +0100409 #if MICROPY_PY_BUILTINS_MEMORYVIEW
410 if (MP_OBJ_IS_TYPE(value, &mp_type_memoryview)) {
411 src_items = (uint8_t*)src_items + (src_slice->free * item_sz);
412 }
413 #endif
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300414 } else if (MP_OBJ_IS_TYPE(value, &mp_type_bytes)) {
415 if (item_sz != 1) {
416 goto compat_error;
417 }
418 mp_buffer_info_t bufinfo;
419 mp_get_buffer_raise(value, &bufinfo, MP_BUFFER_READ);
420 src_len = bufinfo.len;
421 src_items = bufinfo.buf;
422 } else {
423 mp_not_implemented("array/bytes required on right side");
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200424 }
425
426 // TODO: check src/dst compat
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300427 mp_int_t len_adj = src_len - (slice.stop - slice.start);
Delio Brignoli6a388aa2015-06-06 20:14:08 +0200428 uint8_t* dest_items = o->items;
429 #if MICROPY_PY_BUILTINS_MEMORYVIEW
430 if (o->base.type == &mp_type_memoryview) {
431 if (len_adj != 0) {
Delio Brignoli32aba402015-06-03 07:07:35 +0200432 goto compat_error;
433 }
Delio Brignoli6a388aa2015-06-06 20:14:08 +0200434 dest_items += o->free * item_sz;
435 }
436 #endif
437 if (len_adj > 0) {
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200438 if (len_adj > o->free) {
439 // TODO: alloc policy; at the moment we go conservative
Damien Georged8914522015-02-27 09:54:12 +0000440 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 +0200441 o->free = 0;
442 }
Delio Brignoli6a388aa2015-06-06 20:14:08 +0200443 mp_seq_replace_slice_grow_inplace(dest_items, o->len,
Paul Sokolovsky56beb012015-04-16 00:48:36 +0300444 slice.start, slice.stop, src_items, src_len, len_adj, item_sz);
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200445 } else {
Delio Brignoli6a388aa2015-06-06 20:14:08 +0200446 mp_seq_replace_slice_no_grow(dest_items, o->len,
447 slice.start, slice.stop, src_items, src_len, item_sz);
448 // Clear "freed" elements at the end of list
449 // TODO: This is actually only needed for typecode=='O'
450 mp_seq_clear(dest_items, o->len + len_adj, o->len, item_sz);
451 // TODO: alloc policy after shrinking
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200452 }
453 o->len += len_adj;
454 return mp_const_none;
455 #else
456 return MP_OBJ_NULL; // op not supported
457 #endif
458 }
459
Damien Georgedd4f4532014-10-23 13:34:35 +0100460 mp_obj_array_t *res;
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200461 size_t sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300462 assert(sz > 0);
Damien Georgedd4f4532014-10-23 13:34:35 +0100463 if (0) {
464 // dummy
465 #if MICROPY_PY_BUILTINS_MEMORYVIEW
466 } else if (o->base.type == &mp_type_memoryview) {
467 res = m_new_obj(mp_obj_array_t);
468 *res = *o;
Damien Georgede3c8062014-10-26 13:20:50 +0000469 res->free += slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100470 res->len = slice.stop - slice.start;
Damien Georgedd4f4532014-10-23 13:34:35 +0100471 #endif
472 } else {
473 res = array_new(o->typecode, slice.stop - slice.start);
stijn49c47da2014-10-28 17:20:52 +0100474 memcpy(res->items, (uint8_t*)o->items + slice.start * sz, (slice.stop - slice.start) * sz);
Damien Georgedd4f4532014-10-23 13:34:35 +0100475 }
Damien George999cedb2015-11-27 17:01:44 +0000476 return MP_OBJ_FROM_PTR(res);
Damien Georgec49ddb92014-06-01 13:49:35 +0100477#endif
Damien George729f7b42014-04-17 22:10:53 +0100478 } else {
Damien George42f3de92014-10-03 17:44:14 +0000479 mp_uint_t index = mp_get_index(o->base.type, o->len, index_in, false);
Damien Georgede3c8062014-10-26 13:20:50 +0000480 #if MICROPY_PY_BUILTINS_MEMORYVIEW
481 if (o->base.type == &mp_type_memoryview) {
482 index += o->free;
483 if (value != MP_OBJ_SENTINEL && (o->typecode & 0x80) == 0) {
484 // store to read-only memoryview
Damien Georgedd4f4532014-10-23 13:34:35 +0100485 return MP_OBJ_NULL;
486 }
Damien Georgede3c8062014-10-26 13:20:50 +0000487 }
488 #endif
489 if (value == MP_OBJ_SENTINEL) {
490 // load
491 return mp_binary_get_val_array(o->typecode & TYPECODE_MASK, o->items, index);
492 } else {
493 // store
494 mp_binary_set_val_array(o->typecode & TYPECODE_MASK, o->items, index, value);
Paul Sokolovskyd6e12722014-04-19 20:06:57 +0300495 return mp_const_none;
496 }
Damien George729f7b42014-04-17 22:10:53 +0100497 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100498 }
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200499}
500
Damien George4d917232014-08-30 14:28:06 +0100501STATIC 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 +0000502 mp_obj_array_t *o = MP_OBJ_TO_PTR(o_in);
Kaspar Schleiserf5dd6f72015-05-10 13:04:38 +0200503 size_t sz = mp_binary_get_size('@', o->typecode & TYPECODE_MASK, NULL);
Damien Georgede3c8062014-10-26 13:20:50 +0000504 bufinfo->buf = o->items;
505 bufinfo->len = o->len * sz;
506 bufinfo->typecode = o->typecode & TYPECODE_MASK;
Damien Georgedd4f4532014-10-23 13:34:35 +0100507 #if MICROPY_PY_BUILTINS_MEMORYVIEW
Damien Georgede3c8062014-10-26 13:20:50 +0000508 if (o->base.type == &mp_type_memoryview) {
509 if ((o->typecode & 0x80) == 0 && (flags & MP_BUFFER_WRITE)) {
510 // read-only memoryview
511 return 1;
512 }
stijn49c47da2014-10-28 17:20:52 +0100513 bufinfo->buf = (uint8_t*)bufinfo->buf + (mp_uint_t)o->free * sz;
Damien Georgedd4f4532014-10-23 13:34:35 +0100514 }
Damien George3a2171e2015-09-04 16:53:46 +0100515 #else
516 (void)flags;
Damien Georgedd4f4532014-10-23 13:34:35 +0100517 #endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200518 return 0;
519}
520
Damien Georgedd4f4532014-10-23 13:34:35 +0100521#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
Damien Georgecbf76742015-11-27 13:38:15 +0000522STATIC const mp_rom_map_elem_t array_locals_dict_table[] = {
523 { MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&array_append_obj) },
524 { MP_ROM_QSTR(MP_QSTR_extend), MP_ROM_PTR(&array_extend_obj) },
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200525};
526
Damien George9b196cd2014-03-26 21:47:19 +0000527STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
Damien Georgedd4f4532014-10-23 13:34:35 +0100528#endif
Damien George9b196cd2014-03-26 21:47:19 +0000529
Damien Georgedd4f4532014-10-23 13:34:35 +0100530#if MICROPY_PY_ARRAY
Damien Georgecaac5422014-03-25 14:18:18 +0000531const mp_obj_type_t mp_type_array = {
Damien Georgec5966122014-02-15 16:10:44 +0000532 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000533 .name = MP_QSTR_array,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200534 .print = array_print,
535 .make_new = array_make_new,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200536 .getiter = array_iterator_new,
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200537 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300538 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100539 .subscr = array_subscr,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200540 .buffer_p = { .get_buffer = array_get_buffer },
Damien George999cedb2015-11-27 17:01:44 +0000541 .locals_dict = (mp_obj_dict_t*)&array_locals_dict,
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200542};
Damien Georgedd4f4532014-10-23 13:34:35 +0100543#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200544
Damien Georgedd4f4532014-10-23 13:34:35 +0100545#if MICROPY_PY_BUILTINS_BYTEARRAY
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300546const mp_obj_type_t mp_type_bytearray = {
547 { &mp_type_type },
548 .name = MP_QSTR_bytearray,
549 .print = array_print,
550 .make_new = bytearray_make_new,
551 .getiter = array_iterator_new,
552 .unary_op = array_unary_op,
Paul Sokolovsky5f930332014-08-10 11:49:23 +0300553 .binary_op = array_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100554 .subscr = array_subscr,
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300555 .buffer_p = { .get_buffer = array_get_buffer },
Damien George999cedb2015-11-27 17:01:44 +0000556 .locals_dict = (mp_obj_dict_t*)&array_locals_dict,
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300557};
Damien Georgedd4f4532014-10-23 13:34:35 +0100558#endif
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300559
Damien Georgedd4f4532014-10-23 13:34:35 +0100560#if MICROPY_PY_BUILTINS_MEMORYVIEW
561const mp_obj_type_t mp_type_memoryview = {
562 { &mp_type_type },
563 .name = MP_QSTR_memoryview,
564 .make_new = memoryview_make_new,
565 .getiter = array_iterator_new,
566 .unary_op = array_unary_op,
567 .binary_op = array_binary_op,
568 .subscr = array_subscr,
569 .buffer_p = { .get_buffer = array_get_buffer },
570};
571#endif
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200572
Damien Georgedd4f4532014-10-23 13:34:35 +0100573/* unused
Damien Georged182b982014-08-30 14:19:41 +0100574mp_uint_t mp_obj_array_len(mp_obj_t self_in) {
Paul Sokolovsky33996682014-01-21 23:30:10 +0200575 return ((mp_obj_array_t *)self_in)->len;
576}
Damien Georgedd4f4532014-10-23 13:34:35 +0100577*/
Paul Sokolovsky33996682014-01-21 23:30:10 +0200578
Damien Georgedd4f4532014-10-23 13:34:35 +0100579#if MICROPY_PY_BUILTINS_BYTEARRAY
Damien George4abff752014-08-30 14:59:21 +0100580mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items) {
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200581 mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
582 memcpy(o->items, items, n);
Damien George999cedb2015-11-27 17:01:44 +0000583 return MP_OBJ_FROM_PTR(o);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200584}
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200585
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200586// Create bytearray which references specified memory area
Damien Georged182b982014-08-30 14:19:41 +0100587mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items) {
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200588 mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
Damien Georgedd4f4532014-10-23 13:34:35 +0100589 o->base.type = &mp_type_bytearray;
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200590 o->typecode = BYTEARRAY_TYPECODE;
591 o->free = 0;
592 o->len = n;
593 o->items = items;
Damien George999cedb2015-11-27 17:01:44 +0000594 return MP_OBJ_FROM_PTR(o);
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200595}
Damien Georgedd4f4532014-10-23 13:34:35 +0100596#endif
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200597
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200598/******************************************************************************/
Damien Georgedd4f4532014-10-23 13:34:35 +0100599// array iterator
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200600
601typedef struct _mp_obj_array_it_t {
602 mp_obj_base_t base;
603 mp_obj_array_t *array;
Damien Georgede3c8062014-10-26 13:20:50 +0000604 mp_uint_t offset;
Damien George40f3c022014-07-03 13:25:24 +0100605 mp_uint_t cur;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200606} mp_obj_array_it_t;
607
Damien Georgecaac5422014-03-25 14:18:18 +0000608STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000609 mp_obj_array_it_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200610 if (self->cur < self->array->len) {
Damien Georgede3c8062014-10-26 13:20:50 +0000611 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 +0200612 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100613 return MP_OBJ_STOP_ITERATION;
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200614 }
615}
616
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200617STATIC const mp_obj_type_t array_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000618 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000619 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300620 .getiter = mp_identity,
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200621 .iternext = array_it_iternext,
622};
623
Damien Georgecaac5422014-03-25 14:18:18 +0000624STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
Damien George999cedb2015-11-27 17:01:44 +0000625 mp_obj_array_t *array = MP_OBJ_TO_PTR(array_in);
Damien Georgede3c8062014-10-26 13:20:50 +0000626 mp_obj_array_it_t *o = m_new0(mp_obj_array_it_t, 1);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200627 o->base.type = &array_it_type;
628 o->array = array;
Damien Georgede3c8062014-10-26 13:20:50 +0000629 #if MICROPY_PY_BUILTINS_MEMORYVIEW
630 if (array->base.type == &mp_type_memoryview) {
631 o->offset = array->free;
632 }
633 #endif
Damien George999cedb2015-11-27 17:01:44 +0000634 return MP_OBJ_FROM_PTR(o);
Paul Sokolovsky09ce0592014-01-21 23:45:19 +0200635}
Paul Sokolovskycb78f862014-06-27 20:39:09 +0300636
Damien Georgedd4f4532014-10-23 13:34:35 +0100637#endif // MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW