From ca6d75f16d8fc465ddaa4dc90a6f87ab9667b12e Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Sat, 30 Aug 2014 15:17:47 +0100
Subject: [PATCH] py: Small simplifications in tuple and list accessors.

---
 py/obj.c      | 16 ++++------------
 py/objtuple.c |  8 ++------
 py/objzip.c   | 17 ++++++++---------
 py/runtime.c  | 12 +++++-------
 4 files changed, 19 insertions(+), 34 deletions(-)

diff --git a/py/obj.c b/py/obj.c
index 4a9cf7ffb..430712058 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -319,18 +319,10 @@ void mp_obj_get_array(mp_obj_t o, mp_uint_t *len, mp_obj_t **items) {
 }
 
 void mp_obj_get_array_fixed_n(mp_obj_t o, mp_uint_t len, mp_obj_t **items) {
-    if (MP_OBJ_IS_TYPE(o, &mp_type_tuple) || MP_OBJ_IS_TYPE(o, &mp_type_list)) {
-        mp_uint_t seq_len;
-        if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) {
-            mp_obj_tuple_get(o, &seq_len, items);
-        } else {
-            mp_obj_list_get(o, &seq_len, items);
-        }
-        if (seq_len != len) {
-            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "requested length %d but object has length %d", len, seq_len));
-        }
-    } else {
-        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "object '%s' is not a tuple or list", mp_obj_get_type_str(o)));
+    mp_uint_t seq_len;
+    mp_obj_get_array(o, &seq_len, items);
+    if (seq_len != len) {
+        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "requested length %d but object has length %d", len, seq_len));
     }
 }
 
diff --git a/py/objtuple.c b/py/objtuple.c
index 2cc4ad3ae..6abe7d2c6 100644
--- a/py/objtuple.c
+++ b/py/objtuple.c
@@ -241,12 +241,8 @@ mp_obj_t mp_obj_new_tuple(mp_uint_t n, const mp_obj_t *items) {
 void mp_obj_tuple_get(mp_obj_t self_in, mp_uint_t *len, mp_obj_t **items) {
     assert(MP_OBJ_IS_TYPE(self_in, &mp_type_tuple));
     mp_obj_tuple_t *self = self_in;
-    if (len) {
-        *len = self->len;
-    }
-    if (items) {
-        *items = &self->items[0];
-    }
+    *len = self->len;
+    *items = &self->items[0];
 }
 
 void mp_obj_tuple_del(mp_obj_t self_in) {
diff --git a/py/objzip.c b/py/objzip.c
index 828d6bd03..738131f92 100644
--- a/py/objzip.c
+++ b/py/objzip.c
@@ -31,11 +31,12 @@
 #include "misc.h"
 #include "qstr.h"
 #include "obj.h"
+#include "objtuple.h"
 #include "runtime.h"
 
 typedef struct _mp_obj_zip_t {
     mp_obj_base_t base;
-    int n_iters;
+    mp_uint_t n_iters;
     mp_obj_t iters[];
 } mp_obj_zip_t;
 
@@ -45,7 +46,7 @@ STATIC mp_obj_t zip_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw,
     mp_obj_zip_t *o = m_new_obj_var(mp_obj_zip_t, mp_obj_t, n_args);
     o->base.type = &mp_type_zip;
     o->n_iters = n_args;
-    for (int i = 0; i < n_args; i++) {
+    for (mp_uint_t i = 0; i < n_args; i++) {
         o->iters[i] = mp_getiter(args[i]);
     }
     return o;
@@ -54,22 +55,20 @@ STATIC mp_obj_t zip_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw,
 STATIC mp_obj_t zip_iternext(mp_obj_t self_in) {
     assert(MP_OBJ_IS_TYPE(self_in, &mp_type_zip));
     mp_obj_zip_t *self = self_in;
-    mp_obj_t *items;
     if (self->n_iters == 0) {
         return MP_OBJ_STOP_ITERATION;
     }
-    mp_obj_t o = mp_obj_new_tuple(self->n_iters, NULL);
-    mp_obj_tuple_get(o, NULL, &items);
+    mp_obj_tuple_t *tuple = mp_obj_new_tuple(self->n_iters, NULL);
 
-    for (int i = 0; i < self->n_iters; i++) {
+    for (mp_uint_t i = 0; i < self->n_iters; i++) {
         mp_obj_t next = mp_iternext(self->iters[i]);
         if (next == MP_OBJ_STOP_ITERATION) {
-            mp_obj_tuple_del(o);
+            mp_obj_tuple_del(tuple);
             return MP_OBJ_STOP_ITERATION;
         }
-        items[i] = next;
+        tuple->items[i] = next;
     }
-    return o;
+    return tuple;
 }
 
 const mp_obj_type_t mp_type_zip = {
diff --git a/py/runtime.c b/py/runtime.c
index 98263fab7..e84db4e12 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -34,6 +34,7 @@
 #include "qstr.h"
 #include "obj.h"
 #include "objtuple.h"
+#include "objlist.h"
 #include "objmodule.h"
 #include "parsenum.h"
 #include "runtime0.h"
@@ -769,21 +770,18 @@ void mp_unpack_ex(mp_obj_t seq_in, mp_uint_t num_in, mp_obj_t *items) {
             }
             items[num_left + num_right + 1 - 1 - seq_len] = item;
         }
-        mp_obj_t rest = mp_obj_new_list(0, NULL);
+        mp_obj_list_t *rest = mp_obj_new_list(0, NULL);
         while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
             mp_obj_list_append(rest, item);
         }
-        mp_uint_t rest_len;
-        mp_obj_t *rest_items;
-        mp_obj_list_get(rest, &rest_len, &rest_items);
-        if (rest_len < num_right) {
+        if (rest->len < num_right) {
             goto too_short;
         }
         items[num_right] = rest;
         for (mp_uint_t i = 0; i < num_right; i++) {
-            items[num_right - 1 - i] = rest_items[rest_len - num_right + i];
+            items[num_right - 1 - i] = rest->items[rest->len - num_right + i];
         }
-        mp_obj_list_set_len(rest, rest_len - num_right);
+        mp_obj_list_set_len(rest, rest->len - num_right);
     }
     return;
 
-- 
GitLab