From 7e480e8a30e5e293586bd4806f02a481a08648ca Mon Sep 17 00:00:00 2001
From: Krzysztof Blazewicz <blazewicz.krzysztof@gmail.com>
Date: Sat, 4 Mar 2017 12:29:20 +0100
Subject: [PATCH] py: Use mp_obj_get_array where sequence may be a tuple or a
 list.

---
 py/objfun.c  | 10 ++--------
 py/objstr.c  | 15 ++++++---------
 py/runtime.c | 12 ++----------
 3 files changed, 10 insertions(+), 27 deletions(-)

diff --git a/py/objfun.c b/py/objfun.c
index e5f6009dc..a823f49e5 100644
--- a/py/objfun.c
+++ b/py/objfun.c
@@ -511,17 +511,11 @@ STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
             // convert float to int (could also pass in float registers)
             return (mp_int_t)mp_obj_float_get(obj);
 #endif
-        } else if (type == &mp_type_tuple) {
+        } else if (type == &mp_type_tuple || type == &mp_type_list) {
             // pointer to start of tuple (could pass length, but then could use len(x) for that)
             mp_uint_t len;
             mp_obj_t *items;
-            mp_obj_tuple_get(obj, &len, &items);
-            return (mp_uint_t)items;
-        } else if (type == &mp_type_list) {
-            // pointer to start of list (could pass length, but then could use len(x) for that)
-            mp_uint_t len;
-            mp_obj_t *items;
-            mp_obj_list_get(obj, &len, &items);
+            mp_obj_get_array(obj, &len, &items);
             return (mp_uint_t)items;
         } else {
             mp_buffer_info_t bufinfo;
diff --git a/py/objstr.c b/py/objstr.c
index c137afe67..17d06f88e 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -430,16 +430,13 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
     // process args
     mp_uint_t seq_len;
     mp_obj_t *seq_items;
-    if (MP_OBJ_IS_TYPE(arg, &mp_type_tuple)) {
-        mp_obj_tuple_get(arg, &seq_len, &seq_items);
-    } else {
-        if (!MP_OBJ_IS_TYPE(arg, &mp_type_list)) {
-            // arg is not a list, try to convert it to one
-            // TODO: Try to optimize?
-            arg = mp_type_list.make_new(&mp_type_list, 1, 0, &arg);
-        }
-        mp_obj_list_get(arg, &seq_len, &seq_items);
+
+    if (!MP_OBJ_IS_TYPE(arg, &mp_type_list) && !MP_OBJ_IS_TYPE(arg, &mp_type_tuple)) {
+        // arg is not a list nor a tuple, try to convert it to a list
+        // TODO: Try to optimize?
+        arg = mp_type_list.make_new(&mp_type_list, 1, 0, &arg);
     }
+    mp_obj_get_array(arg, &seq_len, &seq_items);
 
     // count required length
     size_t required_len = 0;
diff --git a/py/runtime.c b/py/runtime.c
index 0f8044c8d..1f69290ba 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -797,11 +797,7 @@ void mp_unpack_sequence(mp_obj_t seq_in, size_t num, mp_obj_t *items) {
     mp_uint_t seq_len;
     if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(seq_in, &mp_type_list)) {
         mp_obj_t *seq_items;
-        if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
-            mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
-        } else {
-            mp_obj_list_get(seq_in, &seq_len, &seq_items);
-        }
+        mp_obj_get_array(seq_in, &seq_len, &seq_items);
         if (seq_len < num) {
             goto too_short;
         } else if (seq_len > num) {
@@ -851,11 +847,7 @@ void mp_unpack_ex(mp_obj_t seq_in, size_t num_in, mp_obj_t *items) {
     mp_uint_t seq_len;
     if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(seq_in, &mp_type_list)) {
         mp_obj_t *seq_items;
-        if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
-            mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
-        } else {
-            mp_obj_list_get(seq_in, &seq_len, &seq_items);
-        }
+        mp_obj_get_array(seq_in, &seq_len, &seq_items);
         if (seq_len < num_left + num_right) {
             goto too_short;
         }
-- 
GitLab