summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--py/objtuple.c11
-rw-r--r--tests/basics/tuple1.py16
2 files changed, 26 insertions, 1 deletions
diff --git a/py/objtuple.c b/py/objtuple.c
index 5f1744ea30..da714e08a3 100644
--- a/py/objtuple.c
+++ b/py/objtuple.c
@@ -1,3 +1,4 @@
+#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
@@ -87,7 +88,15 @@ static mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
switch (op) {
case RT_BINARY_OP_SUBSCR:
{
- // tuple load
+#if MICROPY_ENABLE_SLICE
+ if (MP_OBJ_IS_TYPE(rhs, &slice_type)) {
+ machine_uint_t start, stop;
+ assert(m_seq_get_fast_slice_indexes(o->len, rhs, &start, &stop));
+ mp_obj_tuple_t *res = mp_obj_new_tuple(stop - start, NULL);
+ m_seq_copy(res->items, o->items + start, res->len, mp_obj_t);
+ return res;
+ }
+#endif
uint index = mp_get_index(o->base.type, o->len, rhs);
return o->items[index];
}
diff --git a/tests/basics/tuple1.py b/tests/basics/tuple1.py
new file mode 100644
index 0000000000..b64720b3eb
--- /dev/null
+++ b/tests/basics/tuple1.py
@@ -0,0 +1,16 @@
+# basic tuple functionality
+x = (1, 2, 3 * 4)
+print(x)
+try:
+ x[0] = 4
+except TypeError:
+ print("TypeError")
+print(x)
+try:
+ x.append(5)
+except AttributeError:
+ print("AttributeError")
+
+print(x[1:])
+print(x[:-1])
+print(x[2:3])