Update MicroPython from 1.12 to 1.17

This commit is contained in:
Yaya.Cout
2021-12-14 18:16:49 +01:00
parent 5cbce3c116
commit 38faecda29
162 changed files with 8326 additions and 3888 deletions

View File

@@ -44,13 +44,18 @@ STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args);
STATIC void list_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
mp_obj_list_t *o = MP_OBJ_TO_PTR(o_in);
const char *item_separator = ", ";
if (!(MICROPY_PY_UJSON && kind == PRINT_JSON)) {
kind = PRINT_REPR;
} else {
#if MICROPY_PY_UJSON_SEPARATORS
item_separator = MP_PRINT_GET_EXT(print)->item_separator;
#endif
}
mp_print_str(print, "[");
for (size_t i = 0; i < o->len; i++) {
if (i > 0) {
mp_print_str(print, ", ");
mp_print_str(print, item_separator);
}
mp_obj_print_helper(print, o->items[i], kind);
}
@@ -88,15 +93,18 @@ STATIC mp_obj_t list_make_new(const mp_obj_type_t *type_in, size_t n_args, size_
STATIC mp_obj_t list_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
switch (op) {
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->len != 0);
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
case MP_UNARY_OP_BOOL:
return mp_obj_new_bool(self->len != 0);
case MP_UNARY_OP_LEN:
return MP_OBJ_NEW_SMALL_INT(self->len);
#if MICROPY_PY_SYS_GETSIZEOF
case MP_UNARY_OP_SIZEOF: {
size_t sz = sizeof(*self) + sizeof(mp_obj_t) * self->alloc;
return MP_OBJ_NEW_SMALL_INT(sz);
}
#endif
default: return MP_OBJ_NULL; // op not supported
default:
return MP_OBJ_NULL; // op not supported
}
}
@@ -153,7 +161,7 @@ STATIC mp_obj_t list_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) {
STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
if (value == MP_OBJ_NULL) {
// delete
#if MICROPY_PY_BUILTINS_SLICE
#if MICROPY_PY_BUILTINS_SLICE
if (mp_obj_is_type(index, &mp_type_slice)) {
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
mp_bound_slice_t slice;
@@ -162,22 +170,21 @@ STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
}
mp_int_t len_adj = slice.start - slice.stop;
//printf("Len adj: %d\n", len_adj);
assert(len_adj <= 0);
mp_seq_replace_slice_no_grow(self->items, self->len, slice.start, slice.stop, self->items/*NULL*/, 0, sizeof(*self->items));
mp_seq_replace_slice_no_grow(self->items, self->len, slice.start, slice.stop, self->items /*NULL*/, 0, sizeof(*self->items));
// Clear "freed" elements at the end of list
mp_seq_clear(self->items, self->len + len_adj, self->len, sizeof(*self->items));
self->len += len_adj;
return mp_const_none;
}
#endif
#endif
mp_obj_t args[2] = {self_in, index};
list_pop(2, args);
return mp_const_none;
} else if (value == MP_OBJ_SENTINEL) {
// load
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
#if MICROPY_PY_BUILTINS_SLICE
#if MICROPY_PY_BUILTINS_SLICE
if (mp_obj_is_type(index, &mp_type_slice)) {
mp_bound_slice_t slice;
if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) {
@@ -187,21 +194,21 @@ STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
mp_seq_copy(res->items, self->items + slice.start, res->len, mp_obj_t);
return MP_OBJ_FROM_PTR(res);
}
#endif
#endif
size_t index_val = mp_get_index(self->base.type, self->len, index, false);
return self->items[index_val];
} else {
#if MICROPY_PY_BUILTINS_SLICE
#if MICROPY_PY_BUILTINS_SLICE
if (mp_obj_is_type(index, &mp_type_slice)) {
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
size_t value_len; mp_obj_t *value_items;
size_t value_len;
mp_obj_t *value_items;
mp_obj_get_array(value, &value_len, &value_items);
mp_bound_slice_t slice_out;
if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice_out)) {
mp_raise_NotImplementedError(NULL);
}
mp_int_t len_adj = value_len - (slice_out.stop - slice_out.start);
//printf("Len adj: %d\n", len_adj);
if (len_adj > 0) {
if (self->len + len_adj > self->alloc) {
// TODO: Might optimize memory copies here by checking if block can
@@ -221,7 +228,7 @@ STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
self->len += len_adj;
return mp_const_none;
}
#endif
#endif
mp_obj_list_store(self_in, index, value);
return mp_const_none;
}
@@ -268,7 +275,7 @@ STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) {
mp_check_self(mp_obj_is_type(args[0], &mp_type_list));
mp_obj_list_t *self = MP_OBJ_TO_PTR(args[0]);
if (self->len == 0) {
mp_raise_msg(&mp_type_IndexError, "pop from empty list");
mp_raise_msg(&mp_type_IndexError, MP_ERROR_TEXT("pop from empty list"));
}
size_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];
@@ -277,7 +284,7 @@ STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) {
// Clear stale pointer from slot which just got freed to prevent GC issues
self->items[self->len] = MP_OBJ_NULL;
if (self->alloc > LIST_MIN_ALLOC && self->alloc > 2 * self->len) {
self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc/2);
self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc / 2);
self->alloc /= 2;
}
return ret;
@@ -290,9 +297,13 @@ STATIC void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, mp_obj
mp_obj_t *t = tail;
mp_obj_t v = key_fn == MP_OBJ_NULL ? tail[0] : mp_call_function_1(key_fn, tail[0]); // get pivot using key_fn
for (;;) {
do ++h; while (h < t && mp_binary_op(MP_BINARY_OP_LESS, key_fn == MP_OBJ_NULL ? h[0] : mp_call_function_1(key_fn, h[0]), v) == binop_less_result);
do --t; while (h < t && mp_binary_op(MP_BINARY_OP_LESS, v, key_fn == MP_OBJ_NULL ? t[0] : mp_call_function_1(key_fn, t[0])) == binop_less_result);
if (h >= t) break;
do {++h;
} while (h < t && mp_binary_op(MP_BINARY_OP_LESS, key_fn == MP_OBJ_NULL ? h[0] : mp_call_function_1(key_fn, h[0]), v) == binop_less_result);
do {--t;
} while (h < t && mp_binary_op(MP_BINARY_OP_LESS, v, key_fn == MP_OBJ_NULL ? t[0] : mp_call_function_1(key_fn, t[0])) == binop_less_result);
if (h >= t) {
break;
}
mp_obj_t x = h[0];
h[0] = t[0];
t[0] = x;
@@ -314,7 +325,7 @@ STATIC void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, mp_obj
// TODO Python defines sort to be stable but ours is not
mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_key, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} },
{ MP_QSTR_key, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
{ MP_QSTR_reverse, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
};
@@ -323,15 +334,15 @@ mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_
mp_arg_val_t key, reverse;
} args;
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args,
MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t*)&args);
MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t *)&args);
mp_check_self(mp_obj_is_type(pos_args[0], &mp_type_list));
mp_obj_list_t *self = MP_OBJ_TO_PTR(pos_args[0]);
if (self->len > 1) {
mp_quicksort(self->items, self->items + self->len - 1,
args.key.u_obj == mp_const_none ? MP_OBJ_NULL : args.key.u_obj,
args.reverse.u_bool ? mp_const_false : mp_const_true);
args.key.u_obj == mp_const_none ? MP_OBJ_NULL : args.key.u_obj,
args.reverse.u_bool ? mp_const_false : mp_const_true);
}
return mp_const_none;
@@ -371,19 +382,19 @@ STATIC mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
// insert has its own strange index logic
mp_int_t index = MP_OBJ_SMALL_INT_VALUE(idx);
if (index < 0) {
index += self->len;
index += self->len;
}
if (index < 0) {
index = 0;
index = 0;
}
if ((size_t)index > self->len) {
index = self->len;
index = self->len;
}
mp_obj_list_append(self_in, mp_const_none);
for (mp_int_t i = self->len-1; i > index; i--) {
self->items[i] = self->items[i-1];
for (mp_int_t i = self->len - 1; i > index; i--) {
self->items[i] = self->items[i - 1];
}
self->items[index] = obj;
@@ -404,10 +415,10 @@ STATIC mp_obj_t list_reverse(mp_obj_t self_in) {
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
mp_int_t len = self->len;
for (mp_int_t i = 0; i < len/2; i++) {
mp_obj_t a = self->items[i];
self->items[i] = self->items[len-i-1];
self->items[len-i-1] = a;
for (mp_int_t i = 0; i < len / 2; i++) {
mp_obj_t a = self->items[i];
self->items[i] = self->items[len - i - 1];
self->items[len - i - 1] = a;
}
return mp_const_none;
@@ -450,7 +461,7 @@ const mp_obj_type_t mp_type_list = {
.binary_op = list_binary_op,
.subscr = list_subscr,
.getiter = list_getiter,
.locals_dict = (mp_obj_dict_t*)&list_locals_dict,
.locals_dict = (mp_obj_dict_t *)&list_locals_dict,
};
void mp_obj_list_init(mp_obj_list_t *o, size_t n) {
@@ -520,7 +531,7 @@ STATIC mp_obj_t list_it_iternext(mp_obj_t self_in) {
mp_obj_t mp_obj_new_list_iterator(mp_obj_t list, size_t cur, mp_obj_iter_buf_t *iter_buf) {
assert(sizeof(mp_obj_list_it_t) <= sizeof(mp_obj_iter_buf_t));
mp_obj_list_it_t *o = (mp_obj_list_it_t*)iter_buf;
mp_obj_list_it_t *o = (mp_obj_list_it_t *)iter_buf;
o->base.type = &mp_type_polymorph_iter;
o->iternext = list_it_iternext;
o->list = list;