mirror of
https://github.com/micropython/micropython.git
synced 2026-01-05 03:30:14 +01:00
Merge remote-tracking branch 'upstream/master' into list_remove
This commit is contained in:
30
py/objlist.c
30
py/objlist.c
@@ -6,6 +6,7 @@
|
||||
#include "nlr.h"
|
||||
#include "misc.h"
|
||||
#include "mpconfig.h"
|
||||
#include "mpqstr.h"
|
||||
#include "obj.h"
|
||||
#include "runtime0.h"
|
||||
#include "runtime.h"
|
||||
@@ -35,6 +36,29 @@ static void list_print(void (*print)(void *env, const char *fmt, ...), void *env
|
||||
print(env, "]");
|
||||
}
|
||||
|
||||
static mp_obj_t list_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
|
||||
switch (n_args) {
|
||||
case 0:
|
||||
// return a new, empty list
|
||||
return rt_build_list(0, NULL);
|
||||
|
||||
case 1:
|
||||
{
|
||||
// make list from iterable
|
||||
mp_obj_t iterable = rt_getiter(args[0]);
|
||||
mp_obj_t list = rt_build_list(0, NULL);
|
||||
mp_obj_t item;
|
||||
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
|
||||
rt_list_append(list, item);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
default:
|
||||
nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "list takes at most 1 argument, %d given", (void*)(machine_int_t)n_args));
|
||||
}
|
||||
}
|
||||
|
||||
static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
|
||||
mp_obj_list_t *o = lhs;
|
||||
switch (op) {
|
||||
@@ -81,7 +105,7 @@ static mp_obj_t list_pop(int n_args, const mp_obj_t *args) {
|
||||
assert(MP_OBJ_IS_TYPE(args[0], &list_type));
|
||||
mp_obj_list_t *self = args[0];
|
||||
if (self->len == 0) {
|
||||
nlr_jump(mp_obj_new_exception_msg(rt_q_IndexError, "pop from empty list"));
|
||||
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "pop from empty list"));
|
||||
}
|
||||
uint index = mp_get_index(self->base.type, self->len, n_args == 1 ? mp_obj_new_int(-1) : args[1]);
|
||||
mp_obj_t ret = self->items[index];
|
||||
@@ -170,7 +194,7 @@ static mp_obj_t list_index(int n_args, const mp_obj_t *args) {
|
||||
}
|
||||
}
|
||||
|
||||
nlr_jump(mp_obj_new_exception_msg(rt_q_ValueError, "Object not in list."));
|
||||
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "object not in list"));
|
||||
}
|
||||
|
||||
static mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
|
||||
@@ -221,6 +245,7 @@ const mp_obj_type_t list_type = {
|
||||
{ &mp_const_type },
|
||||
"list",
|
||||
list_print, // print
|
||||
list_make_new, // make_new
|
||||
NULL, // call_n
|
||||
NULL, // unary_op
|
||||
list_binary_op, // binary_op
|
||||
@@ -301,6 +326,7 @@ static const mp_obj_type_t list_it_type = {
|
||||
{ &mp_const_type },
|
||||
"list_iterator",
|
||||
NULL, // print
|
||||
NULL, // make_new
|
||||
NULL, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
|
||||
Reference in New Issue
Block a user