From 2839168340b5ac9f947c01b42f7ddf960da81aa6 Mon Sep 17 00:00:00 2001 From: Damien Date: Fri, 25 Oct 2013 00:40:38 +0100 Subject: [PATCH] Add py_get_array_fixed_n function. --- py/runtime.c | 12 ++++++++++++ py/runtime.h | 1 + 2 files changed, 13 insertions(+) diff --git a/py/runtime.c b/py/runtime.c index a1bd676367..2d8fa0206d 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -871,6 +871,18 @@ qstr py_get_qstr(py_obj_t arg) { } } +py_obj_t *py_get_array_fixed_n(py_obj_t o_in, int n) { + if (IS_O(o_in, O_TUPLE) || IS_O(o_in, O_LIST)) { + py_obj_base_t *o = o_in; + if (o->u_tuple_list.len != n) { + nlr_jump(py_obj_new_exception_2(q_IndexError, "requested length %d but object has length %d", (void*)n, (void*)o->u_tuple_list.len)); + } + return o->u_tuple_list.items; + } else { + nlr_jump(py_obj_new_exception_2(q_TypeError, "object '%s' is not a tuple or list", py_obj_get_type_str(o_in), NULL)); + } +} + py_obj_t rt_load_const_str(qstr qstr) { DEBUG_OP_printf("load '%s'\n", qstr_str(qstr)); return py_obj_new_str(qstr); diff --git a/py/runtime.h b/py/runtime.h index 98d77c8e72..33d7bed266 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -99,6 +99,7 @@ void py_obj_print(py_obj_t o); int rt_is_true(py_obj_t arg); int py_get_int(py_obj_t arg); qstr py_get_qstr(py_obj_t arg); +py_obj_t *py_get_array_fixed_n(py_obj_t o, int n); py_obj_t py_obj_new_int(int value); py_obj_t rt_load_const_str(qstr qstr); py_obj_t rt_load_name(qstr qstr);