diff options
author | John R. Lenton <jlenton@gmail.com> | 2014-01-03 23:22:53 +0000 |
---|---|---|
committer | John R. Lenton <jlenton@gmail.com> | 2014-01-03 23:22:53 +0000 |
commit | 069ded95147c26a1c5b5a4c2c21c1daf94191bb5 (patch) | |
tree | baec8c4b96a62e3a87da626ad4bc08d2d1869a60 /py/objlist.c | |
parent | 64427d6ee62c02d5ac2d9c8aa555c1fa4310fc40 (diff) | |
download | micropython-069ded95147c26a1c5b5a4c2c21c1daf94191bb5.tar.gz micropython-069ded95147c26a1c5b5a4c2c21c1daf94191bb5.zip |
Added list.clear. Fixes issue #53.
Diffstat (limited to 'py/objlist.c')
-rw-r--r-- | py/objlist.c | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/py/objlist.c b/py/objlist.c index 47bd6d985a..28b373b782 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -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 } +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_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_2(list_sort_obj, list_sort); @@ -140,6 +150,7 @@ const mp_obj_type_t list_type = { NULL, // iternext { // method list { "append", &list_append_obj }, + { "clear", &list_clear_obj }, { "pop", &list_pop_obj }, { "sort", &list_sort_obj }, { NULL, NULL }, // end-of-list sentinel |