Added list.clear. Fixes issue #53.

This commit is contained in:
John R. Lenton 2014-01-03 23:22:53 +00:00
parent 64427d6ee6
commit 069ded9514
2 changed files with 15 additions and 0 deletions

View File

@ -125,7 +125,17 @@ static mp_obj_t list_sort(mp_obj_t self_in, mp_obj_t key_fn) {
return mp_const_none; // return None, as per CPython return mp_const_none; // return None, as per CPython
} }
static mp_obj_t list_clear(mp_obj_t self_in) {
assert(MP_OBJ_IS_TYPE(self_in, &list_type));
mp_obj_list_t *self = self_in;
self->len = 0;
self->items = m_renew(mp_obj_t, self->items, self->alloc, 4);
self->alloc = 4;
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append); static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop); static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort); static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
@ -140,6 +150,7 @@ const mp_obj_type_t list_type = {
NULL, // iternext NULL, // iternext
{ // method list { // method list
{ "append", &list_append_obj }, { "append", &list_append_obj },
{ "clear", &list_clear_obj },
{ "pop", &list_pop_obj }, { "pop", &list_pop_obj },
{ "sort", &list_sort_obj }, { "sort", &list_sort_obj },
{ NULL, NULL }, // end-of-list sentinel { NULL, NULL }, // end-of-list sentinel

View File

@ -0,0 +1,4 @@
# tests list.clear
x = [1, 2, 3, 4]
x.clear()
print(x)