py: Make tuple and list use mp_int_t/mp_uint_t.

Part of code cleanup, to resolve issue #50.
This commit is contained in:
Damien George 2014-08-30 14:04:14 +01:00
parent 93965e726f
commit 9c4cbe2ac0
10 changed files with 52 additions and 52 deletions

View File

@ -70,7 +70,7 @@ STATIC mp_import_stat_t stat_dir_or_file(vstr_t *path) {
STATIC mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *dest) {
// extract the list of paths
uint path_num = 0;
mp_uint_t path_num = 0;
mp_obj_t *path_items;
#if MICROPY_PY_SYS
mp_obj_list_get(mp_sys_path, &path_num, &path_items);

View File

@ -308,7 +308,7 @@ void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
#endif
#endif
void mp_obj_get_array(mp_obj_t o, uint *len, mp_obj_t **items) {
void mp_obj_get_array(mp_obj_t o, mp_uint_t *len, mp_obj_t **items) {
if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) {
mp_obj_tuple_get(o, len, items);
} else if (MP_OBJ_IS_TYPE(o, &mp_type_list)) {
@ -318,9 +318,9 @@ void mp_obj_get_array(mp_obj_t o, uint *len, mp_obj_t **items) {
}
}
void mp_obj_get_array_fixed_n(mp_obj_t o, uint 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)) {
uint seq_len;
mp_uint_t seq_len;
if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) {
mp_obj_tuple_get(o, &seq_len, items);
} else {
@ -335,7 +335,7 @@ void mp_obj_get_array_fixed_n(mp_obj_t o, uint len, mp_obj_t **items) {
}
// is_slice determines whether the index is a slice index
uint mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, bool is_slice) {
mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, bool is_slice) {
mp_int_t i;
if (MP_OBJ_IS_SMALL_INT(index)) {
i = MP_OBJ_SMALL_INT_VALUE(index);

View File

@ -384,8 +384,8 @@ mp_obj_t mp_obj_new_fun_viper(mp_uint_t n_args, void *fun_data, mp_uint_t type_s
mp_obj_t mp_obj_new_fun_asm(mp_uint_t n_args, void *fun_data);
mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun);
mp_obj_t mp_obj_new_closure(mp_obj_t fun, uint n_closed, const mp_obj_t *closed);
mp_obj_t mp_obj_new_tuple(uint n, const mp_obj_t *items);
mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items);
mp_obj_t mp_obj_new_tuple(mp_uint_t n, const mp_obj_t *items);
mp_obj_t mp_obj_new_list(mp_uint_t n, mp_obj_t *items);
mp_obj_t mp_obj_new_dict(mp_uint_t n_args);
mp_obj_t mp_obj_new_set(mp_uint_t n_args, mp_obj_t *items);
mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step);
@ -425,9 +425,9 @@ mp_float_t mp_obj_get_float(mp_obj_t self_in);
void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
#endif
//qstr mp_obj_get_qstr(mp_obj_t arg);
void mp_obj_get_array(mp_obj_t o, uint *len, mp_obj_t **items);
void mp_obj_get_array_fixed_n(mp_obj_t o, uint len, mp_obj_t **items);
uint mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, bool is_slice);
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);
mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, bool is_slice);
mp_obj_t mp_obj_len(mp_obj_t o_in);
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); /* may return MP_OBJ_NULL */
mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
@ -490,16 +490,16 @@ mp_obj_t mp_obj_complex_binary_op(mp_uint_t op, mp_float_t lhs_real, mp_float_t
#endif
// tuple
void mp_obj_tuple_get(mp_obj_t self_in, uint *len, mp_obj_t **items);
void mp_obj_tuple_get(mp_obj_t self_in, mp_uint_t *len, mp_obj_t **items);
void mp_obj_tuple_del(mp_obj_t self_in);
mp_int_t mp_obj_tuple_hash(mp_obj_t self_in);
// list
struct _mp_obj_list_t;
void mp_obj_list_init(struct _mp_obj_list_t *o, uint n);
void mp_obj_list_init(struct _mp_obj_list_t *o, mp_uint_t n);
mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg);
void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items);
void mp_obj_list_set_len(mp_obj_t self_in, uint len);
void mp_obj_list_get(mp_obj_t self_in, mp_uint_t *len, mp_obj_t **items);
void mp_obj_list_set_len(mp_obj_t self_in, mp_uint_t len);
void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
mp_obj_t mp_obj_list_sort(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs);

View File

@ -464,7 +464,7 @@ void mp_obj_exception_get_traceback(mp_obj_t self_in, mp_uint_t *n, mp_uint_t **
*n = 0;
*values = NULL;
} else {
uint n2;
mp_uint_t n2;
mp_obj_list_get(self->traceback, &n2, (mp_obj_t**)values);
*n = n2;
}

View File

@ -36,8 +36,8 @@
#include "runtime.h"
#include "objlist.h"
STATIC mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur);
STATIC mp_obj_list_t *list_new(uint n);
STATIC mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, mp_uint_t cur);
STATIC mp_obj_list_t *list_new(mp_uint_t n);
STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in);
STATIC mp_obj_t list_pop(mp_uint_t n_args, const mp_obj_t *args);
@ -50,7 +50,7 @@ STATIC mp_obj_t list_pop(mp_uint_t n_args, const mp_obj_t *args);
STATIC void list_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
mp_obj_list_t *o = o_in;
print(env, "[");
for (int i = 0; i < o->len; i++) {
for (mp_uint_t i = 0; i < o->len; i++) {
if (i > 0) {
print(env, ", ");
}
@ -188,7 +188,7 @@ STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
return res;
}
#endif
uint index_val = mp_get_index(self->base.type, self->len, index, false);
mp_uint_t index_val = mp_get_index(self->base.type, self->len, index, false);
return self->items[index_val];
} else {
#if MICROPY_PY_BUILTINS_SLICE
@ -271,7 +271,7 @@ STATIC mp_obj_t list_pop(mp_uint_t n_args, const mp_obj_t *args) {
if (self->len == 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "pop from empty list"));
}
uint index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false);
mp_uint_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false);
mp_obj_t ret = self->items[index];
self->len -= 1;
memmove(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
@ -286,7 +286,7 @@ STATIC mp_obj_t list_pop(mp_uint_t n_args, const mp_obj_t *args) {
// TODO make this conform to CPython's definition of sort
STATIC void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, bool reversed) {
int op = reversed ? MP_BINARY_OP_MORE : MP_BINARY_OP_LESS;
mp_uint_t op = reversed ? MP_BINARY_OP_MORE : MP_BINARY_OP_LESS;
while (head < tail) {
mp_obj_t *h = head - 1;
mp_obj_t *t = tail;
@ -442,7 +442,7 @@ const mp_obj_type_t mp_type_list = {
.locals_dict = (mp_obj_t)&list_locals_dict,
};
void mp_obj_list_init(mp_obj_list_t *o, uint n) {
void mp_obj_list_init(mp_obj_list_t *o, mp_uint_t n) {
o->base.type = &mp_type_list;
o->alloc = n < LIST_MIN_ALLOC ? LIST_MIN_ALLOC : n;
o->len = n;
@ -450,29 +450,29 @@ void mp_obj_list_init(mp_obj_list_t *o, uint n) {
mp_seq_clear(o->items, n, o->alloc, sizeof(*o->items));
}
STATIC mp_obj_list_t *list_new(uint n) {
STATIC mp_obj_list_t *list_new(mp_uint_t n) {
mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
mp_obj_list_init(o, n);
return o;
}
mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items) {
mp_obj_t mp_obj_new_list(mp_uint_t n, mp_obj_t *items) {
mp_obj_list_t *o = list_new(n);
if (items != NULL) {
for (int i = 0; i < n; i++) {
for (mp_uint_t i = 0; i < n; i++) {
o->items[i] = items[i];
}
}
return o;
}
void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items) {
void mp_obj_list_get(mp_obj_t self_in, mp_uint_t *len, mp_obj_t **items) {
mp_obj_list_t *self = self_in;
*len = self->len;
*items = self->items;
}
void mp_obj_list_set_len(mp_obj_t self_in, uint len) {
void mp_obj_list_set_len(mp_obj_t self_in, mp_uint_t len) {
// trust that the caller knows what it's doing
// TODO realloc if len got much smaller than alloc
mp_obj_list_t *self = self_in;
@ -481,7 +481,7 @@ void mp_obj_list_set_len(mp_obj_t self_in, uint len) {
void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
mp_obj_list_t *self = self_in;
uint i = mp_get_index(self->base.type, self->len, index, false);
mp_uint_t i = mp_get_index(self->base.type, self->len, index, false);
self->items[i] = value;
}
@ -512,7 +512,7 @@ STATIC const mp_obj_type_t mp_type_list_it = {
.iternext = list_it_iternext,
};
mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {
mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, mp_uint_t cur) {
mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t);
o->base.type = &mp_type_list_it;
o->list = list;

View File

@ -307,7 +307,7 @@ mp_obj_t mp_obj_str_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
case MP_BINARY_OP_MODULO: {
mp_obj_t *args;
uint n_args;
mp_uint_t n_args;
mp_obj_t dict = MP_OBJ_NULL;
if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_tuple)) {
// TODO: Support tuple subclasses?
@ -394,7 +394,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
GET_STR_DATA_LEN(self_in, sep_str, sep_len);
// process args
uint seq_len;
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);

View File

@ -36,7 +36,7 @@
#include "runtime.h"
#include "objtuple.h"
STATIC mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, int cur);
STATIC mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, mp_uint_t cur);
/******************************************************************************/
/* tuple */
@ -44,7 +44,7 @@ STATIC mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, int cur);
void mp_obj_tuple_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
mp_obj_tuple_t *o = o_in;
print(env, "(");
for (int i = 0; i < o->len; i++) {
for (mp_uint_t i = 0; i < o->len; i++) {
if (i > 0) {
print(env, ", ");
}
@ -73,8 +73,8 @@ STATIC mp_obj_t mp_obj_tuple_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uin
// TODO optimise for cases where we know the length of the iterator
uint alloc = 4;
uint len = 0;
mp_uint_t alloc = 4;
mp_uint_t len = 0;
mp_obj_t *items = m_new(mp_obj_t, alloc);
mp_obj_t iterable = mp_getiter(args[0]);
@ -176,7 +176,7 @@ mp_obj_t mp_obj_tuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
return res;
}
#endif
uint index_value = mp_get_index(self->base.type, self->len, index, false);
mp_uint_t index_value = mp_get_index(self->base.type, self->len, index, false);
return self->items[index_value];
} else {
return MP_OBJ_NULL; // op not supported
@ -223,7 +223,7 @@ const mp_obj_type_t mp_type_tuple = {
// the zero-length tuple
const mp_obj_tuple_t mp_const_empty_tuple_obj = {{&mp_type_tuple}, 0};
mp_obj_t mp_obj_new_tuple(uint n, const mp_obj_t *items) {
mp_obj_t mp_obj_new_tuple(mp_uint_t n, const mp_obj_t *items) {
if (n == 0) {
return mp_const_empty_tuple;
}
@ -231,14 +231,14 @@ mp_obj_t mp_obj_new_tuple(uint n, const mp_obj_t *items) {
o->base.type = &mp_type_tuple;
o->len = n;
if (items) {
for (int i = 0; i < n; i++) {
for (mp_uint_t i = 0; i < n; i++) {
o->items[i] = items[i];
}
}
return o;
}
void mp_obj_tuple_get(mp_obj_t self_in, uint *len, 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) {
@ -260,7 +260,7 @@ mp_int_t mp_obj_tuple_hash(mp_obj_t self_in) {
mp_obj_tuple_t *self = self_in;
// start hash with pointer to empty tuple, to make it fairly unique
mp_int_t hash = (mp_int_t)mp_const_empty_tuple;
for (uint i = 0; i < self->len; i++) {
for (mp_uint_t i = 0; i < self->len; i++) {
hash += mp_obj_hash(self->items[i]);
}
return hash;
@ -293,7 +293,7 @@ STATIC const mp_obj_type_t mp_type_tuple_it = {
.iternext = tuple_it_iternext,
};
STATIC mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, int cur) {
STATIC mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, mp_uint_t cur) {
mp_obj_tuple_it_t *o = m_new_obj(mp_obj_tuple_it_t);
o->base.type = &mp_type_tuple_it;
o->tuple = tuple;

View File

@ -65,7 +65,7 @@ STATIC mp_obj_t mp_obj_new_instance(mp_obj_t class, uint subobjs) {
}
STATIC int instance_count_native_bases(const mp_obj_type_t *type, const mp_obj_type_t **last_native_base) {
uint len;
mp_uint_t len;
mp_obj_t *items;
mp_obj_tuple_get(type->bases_tuple, &len, &items);
@ -166,7 +166,7 @@ STATIC void mp_obj_class_lookup(struct class_lookup_data *lookup, const mp_obj_
return;
}
uint len;
mp_uint_t len;
mp_obj_t *items;
mp_obj_tuple_get(type->bases_tuple, &len, &items);
if (len == 0) {
@ -755,7 +755,7 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
// TODO might need to make a copy of locals_dict; at least that's how CPython does it
// Basic validation of base classes
uint len;
mp_uint_t len;
mp_obj_t *items;
mp_obj_tuple_get(bases_tuple, &len, &items);
for (uint i = 0; i < len; i++) {
@ -842,7 +842,7 @@ STATIC void super_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
return;
}
uint len;
mp_uint_t len;
mp_obj_t *items;
mp_obj_tuple_get(type->bases_tuple, &len, &items);
struct class_lookup_data lookup = {
@ -901,7 +901,7 @@ bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo) {
}
// get the base objects (they should be type objects)
uint len;
mp_uint_t len;
mp_obj_t *items;
mp_obj_tuple_get(self->bases_tuple, &len, &items);
if (len == 0) {
@ -921,7 +921,7 @@ bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo) {
}
STATIC mp_obj_t mp_obj_is_subclass(mp_obj_t object, mp_obj_t classinfo) {
uint len;
mp_uint_t len;
mp_obj_t *items;
if (MP_OBJ_IS_TYPE(classinfo, &mp_type_type)) {
len = 1;

View File

@ -593,7 +593,7 @@ mp_obj_t mp_call_method_n_kw_var(bool have_self, uint n_args_n_kw, const mp_obj_
// optimise the case of a tuple and list
// get the items
uint len;
mp_uint_t len;
mp_obj_t *items;
mp_obj_get_array(pos_seq, &len, &items);
@ -689,7 +689,7 @@ mp_obj_t mp_call_method_n_kw_var(bool have_self, uint n_args_n_kw, const mp_obj_
// unpacked items are stored in reverse order into the array pointed to by items
void mp_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
uint seq_len;
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)) {
@ -732,7 +732,7 @@ void mp_unpack_ex(mp_obj_t seq_in, uint num_in, mp_obj_t *items) {
uint num_left = num_in & 0xff;
uint num_right = (num_in >> 8) & 0xff;
DEBUG_OP_printf("unpack ex %d %d\n", num_left, num_right);
uint seq_len;
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)) {
@ -773,7 +773,7 @@ void mp_unpack_ex(mp_obj_t seq_in, uint num_in, mp_obj_t *items) {
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
mp_obj_list_append(rest, item);
}
uint rest_len;
mp_uint_t rest_len;
mp_obj_t *rest_items;
mp_obj_list_get(rest, &rest_len, &rest_items);
if (rest_len < num_right) {

View File

@ -293,7 +293,7 @@ int main(int argc, char **argv) {
if (path == NULL) {
path = "~/.micropython/lib:/usr/lib/micropython";
}
uint path_num = 1; // [0] is for current dir (or base dir of the script)
mp_uint_t path_num = 1; // [0] is for current dir (or base dir of the script)
for (char *p = path; p != NULL; p = strchr(p, PATHLIST_SEP_CHAR)) {
path_num++;
if (p != NULL) {