Skip to content

Commit 3399668

Browse files
committed
Add len() support for arrays.
1 parent 12eacca commit 3399668

3 files changed

Lines changed: 7 additions & 0 deletions

File tree

py/obj.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
285285
len = seq_len;
286286
} else if (MP_OBJ_IS_TYPE(o_in, &dict_type)) {
287287
len = mp_obj_dict_len(o_in);
288+
} else if (MP_OBJ_IS_TYPE(o_in, &array_type)) {
289+
len = mp_obj_array_len(o_in);
288290
} else {
289291
return MP_OBJ_NULL;
290292
}

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ extern const mp_obj_type_t zip_type;
334334

335335
// array
336336
extern const mp_obj_type_t array_type;
337+
uint mp_obj_array_len(mp_obj_t self_in);
337338

338339
// functions
339340
typedef struct _mp_obj_fun_native_t { // need this so we can define const objects (to go in ROM)

py/objarray.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ static mp_obj_array_t *array_new(char typecode, uint n) {
255255
return o;
256256
}
257257

258+
uint mp_obj_array_len(mp_obj_t self_in) {
259+
return ((mp_obj_array_t *)self_in)->len;
260+
}
261+
258262
mp_obj_t mp_obj_new_bytearray(uint n, void *items) {
259263
mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
260264
memcpy(o->items, items, n);

0 commit comments

Comments
 (0)