10000 objarray: Implement slice subscription. · errordeveloper/micropython@d6e1272 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit d6e1272

Browse files
committed
objarray: Implement slice subscription.
1 parent 9ae0912 commit d6e1272

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

py/objarray.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,27 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
139139
return MP_OBJ_NOT_SUPPORTED;
140140
} else {
141141
mp_obj_array_t *o = self_in;
142-
uint index = mp_get_index(o->base.type, o->len, index_in, false);
143-
if (value == MP_OBJ_SENTINEL) {
144-
// load
145-
return mp_binary_get_val_array(o->typecode, o->items, index);
142+
if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
143+
machine_uint_t start, stop;
144+
if (!m_seq_get_fast_slice_indexes(o->len, index_in, &start, &stop)) {
145+
assert(0);
146+
}
147+
mp_obj_array_t *res = array_new(o->typecode, stop - start);
148+
int sz = mp_binary_get_size('@', o->typecode, NULL);
149+
assert(sz > 0);
150+
byte *p = o->items;
151+
memcpy(res->items, p + start * sz, (stop - start) * sz);
152+
return res;
146153
} else {
147-
// store
148-
mp_binary_set_val_array(o->typecode, o->items, index, value);
149-
return mp_const_none;
154+
uint index = mp_get_index(o->base.type, o->len, index_in, false);
155+
if (value == MP_OBJ_SENTINEL) {
156+
// load
157+
return mp_binary_get_val_array(o->typecode, o->items, index);
158+
} else {
159+
// store
160+
mp_binary_set_val_array(o->typecode, o->items, index, value);
161+
return mp_const_none;
162+
}
150163
}
151164
}
152165
}

tests/basics/bytearray1.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@
1313
for i in a:
1414
s += i
1515
print(s)
16+
17+
print(a[1:])
18+
print(a[:-1])
19+
print(a[2:3])

0 commit comments

Comments
 (0)
0