Skip to content

Commit e505c59

Browse files
committed
Separate mp_obj_list_pop so it can be used outside of objlist.c
1 parent 121c6bc commit e505c59

File tree

3 files changed

+11
-27
lines changed

3 files changed

+11
-27
lines changed

py/objlist.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,10 @@ STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) {
270270
return mp_const_none; // return None, as per CPython
271271
}
272272

273-
STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) {
274-
mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
275-
mp_obj_list_t *self = mp_instance_cast_to_native_base(args[0], &mp_type_list);
273+
mp_obj_t mp_obj_list_pop(mp_obj_list_t *self, size_t index) {
276274
if (self->len == 0) {
277275
mp_raise_IndexError_varg(translate("pop from empty %q"), MP_QSTR_list);
278276
}
279-
size_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false);
280277
mp_obj_t ret = self->items[index];
281278
self->len -= 1;
282279
memmove(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
@@ -289,6 +286,13 @@ STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) {
289286
return ret;
290287
}
291288

289+
STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) {
290+
mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
291+
mp_obj_list_t *self = mp_instance_cast_to_native_base(args[0], &mp_type_list);
292+
size_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false);
293+
return mp_obj_list_pop(self, index);
294+
}
295+
292296
STATIC void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, mp_obj_t binop_less_result) {
293297
MP_STACK_CHECK();
294298
while (head < tail) {

py/objlist.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ typedef struct _mp_obj_list_t {
3636
} mp_obj_list_t;
3737

3838
void mp_obj_list_init(mp_obj_list_t *o, size_t n);
39+
mp_obj_t mp_obj_list_pop(mp_obj_list_t *self, size_t index);
3940

4041
#endif // MICROPY_INCLUDED_PY_OBJLIST_H

shared-module/displayio/Group.c

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,13 @@
2727
#include "shared-bindings/displayio/Group.h"
2828

2929
#include "py/runtime.h"
30+
#include "py/objlist.h"
3031
#include "shared-bindings/displayio/TileGrid.h"
3132

3233
#if CIRCUITPY_VECTORIO
3334
#include "shared-bindings/vectorio/VectorShape.h"
3435
#endif
3536

36-
#include <string.h>
37-
#define LIST_MIN_ALLOC 4
38-
39-
STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) {
40-
mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
41-
mp_obj_list_t *self = mp_instance_cast_to_native_base(args[0], &mp_type_list);
42-
if (self->len == 0) {
43-
mp_raise_IndexError_varg(translate("pop from empty %q"), MP_QSTR_list);
44-
}
45-
size_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false);
46-
mp_obj_t ret = self->items[index];
47-
self->len -= 1;
48-
memmove(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
49-
// Clear stale pointer from slot which just got freed to prevent GC issues
50-
self->items[self->len] = MP_OBJ_NULL;
51-
if (self->alloc > LIST_MIN_ALLOC && self->alloc > 2 * self->len) {
52-
self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc/2);
53-
self->alloc /= 2;
54-
}
55-
return ret;
56-
}
5737

5838
void common_hal_displayio_group_construct(displayio_group_t* self, uint32_t max_size, uint32_t scale, mp_int_t x, mp_int_t y) {
5939
mp_obj_list_t *members = mp_obj_new_list(0, NULL);
@@ -343,8 +323,7 @@ void common_hal_displayio_group_insert(displayio_group_t* self, size_t index, mp
343323

344324
mp_obj_t common_hal_displayio_group_pop(displayio_group_t* self, size_t index) {
345325
_remove_layer(self, index);
346-
mp_obj_t args[] = {self->members, MP_OBJ_NEW_SMALL_INT(index)};
347-
return list_pop(2, args);
326+
return mp_obj_list_pop(self->members, index);
348327
}
349328

350329
mp_int_t common_hal_displayio_group_index(displayio_group_t* self, mp_obj_t layer) {

0 commit comments

Comments
 (0)