Skip to content

Commit 9ba9589

Browse files
committed
Merge pull request adafruit#58 from chipaca/list_clear
Added list.clear. Fixes issue adafruit#53.
2 parents 98adccf + 069ded9 commit 9ba9589

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

py/objlist.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,17 @@ static mp_obj_t list_sort(mp_obj_t self_in, mp_obj_t key_fn) {
125125
return mp_const_none; // return None, as per CPython
126126
}
127127

128+
static mp_obj_t list_clear(mp_obj_t self_in) {
129+
assert(MP_OBJ_IS_TYPE(self_in, &list_type));
130+
mp_obj_list_t *self = self_in;
131+
self->len = 0;
132+
self->items = m_renew(mp_obj_t, self->items, self->alloc, 4);
133+
self->alloc = 4;
134+
return mp_const_none;
135+
}
136+
128137
static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
138+
static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
129139
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
130140
static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
131141

@@ -140,6 +150,7 @@ const mp_obj_type_t list_type = {
140150
NULL, // iternext
141151
{ // method list
142152
{ "append", &list_append_obj },
153+
{ "clear", &list_clear_obj },
143154
{ "pop", &list_pop_obj },
144155
{ "sort", &list_sort_obj },
145156
{ NULL, NULL }, // end-of-list sentinel

tests/basics/tests/list_clear.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# tests list.clear
2+
x = [1, 2, 3, 4]
3+
x.clear()
4+
print(x)

0 commit comments

Comments
 (0)