Skip to content

Commit f6a635b

Browse files
committed
Fix subclassing of objects that are tested. Others may still be broken.
1 parent 3997179 commit f6a635b

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

extmod/modframebuf.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <string.h>
2929

3030
#include "py/runtime.h"
31+
#include "py/objtype.h"
3132
#include "py/proto.h"
3233

3334
#if MICROPY_PY_FRAMEBUF
@@ -304,17 +305,26 @@ STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, cons
304305
return MP_OBJ_FROM_PTR(o);
305306
}
306307

308+
STATIC const mp_obj_type_t mp_type_framebuf;
309+
310+
// Helper to ensure we have the native super class instead of a subclass.
311+
static mp_obj_framebuf_t* native_framebuf(mp_obj_t framebuf_obj) {
312+
mp_obj_t native_framebuf = mp_instance_cast_to_native_base(framebuf_obj, &mp_type_framebuf);
313+
mp_obj_assert_native_inited(native_framebuf);
314+
return MP_OBJ_TO_PTR(native_framebuf);
315+
}
316+
307317
STATIC mp_int_t framebuf_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
308318
(void)flags;
309-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in);
319+
mp_obj_framebuf_t *self = native_framebuf(self_in);
310320
bufinfo->buf = self->buf;
311321
bufinfo->len = self->stride * self->height * (self->format == FRAMEBUF_RGB565 ? 2 : 1);
312322
bufinfo->typecode = 'B'; // view framebuf as bytes
313323
return 0;
314324
}
315325

316326
STATIC mp_obj_t framebuf_fill(mp_obj_t self_in, mp_obj_t col_in) {
317-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in);
327+
mp_obj_framebuf_t *self = native_framebuf(self_in);
318328
mp_int_t col = mp_obj_get_int(col_in);
319329
formats[self->format].fill_rect(self, 0, 0, self->width, self->height, col);
320330
return mp_const_none;
@@ -324,7 +334,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(framebuf_fill_obj, framebuf_fill);
324334
STATIC mp_obj_t framebuf_fill_rect(size_t n_args, const mp_obj_t *args) {
325335
(void)n_args;
326336

327-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
337+
mp_obj_framebuf_t *self = native_framebuf(args[0]);
328338
mp_int_t x = mp_obj_get_int(args[1]);
329339
mp_int_t y = mp_obj_get_int(args[2]);
330340
mp_int_t width = mp_obj_get_int(args[3]);
@@ -338,7 +348,7 @@ STATIC mp_obj_t framebuf_fill_rect(size_t n_args, const mp_obj_t *args) {
338348
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_fill_rect_obj, 6, 6, framebuf_fill_rect);
339349

340350
STATIC mp_obj_t framebuf_pixel(size_t n_args, const mp_obj_t *args) {
341-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
351+
mp_obj_framebuf_t *self = native_framebuf(args[0]);
342352
mp_int_t x = mp_obj_get_int(args[1]);
343353
mp_int_t y = mp_obj_get_int(args[2]);
344354
if (0 <= x && x < self->width && 0 <= y && y < self->height) {
@@ -357,7 +367,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_pixel_obj, 3, 4, framebuf_pi
357367
STATIC mp_obj_t framebuf_hline(size_t n_args, const mp_obj_t *args) {
358368
(void)n_args;
359369

360-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
370+
mp_obj_framebuf_t *self = native_framebuf(args[0]);
361371
mp_int_t x = mp_obj_get_int(args[1]);
362372
mp_int_t y = mp_obj_get_int(args[2]);
363373
mp_int_t w = mp_obj_get_int(args[3]);
@@ -372,7 +382,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_hline_obj, 5, 5, framebuf_hl
372382
STATIC mp_obj_t framebuf_vline(size_t n_args, const mp_obj_t *args) {
373383
(void)n_args;
374384

375-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
385+
mp_obj_framebuf_t *self = native_framebuf(args[0]);
376386
mp_int_t x = mp_obj_get_int(args[1]);
377387
mp_int_t y = mp_obj_get_int(args[2]);
378388
mp_int_t h = mp_obj_get_int(args[3]);
@@ -387,7 +397,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_vline_obj, 5, 5, framebuf_vl
387397
STATIC mp_obj_t framebuf_rect(size_t n_args, const mp_obj_t *args) {
388398
(void)n_args;
389399

390-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
400+
mp_obj_framebuf_t *self = native_framebuf(args[0]);
391401
mp_int_t x = mp_obj_get_int(args[1]);
392402
mp_int_t y = mp_obj_get_int(args[2]);
393403
mp_int_t w = mp_obj_get_int(args[3]);
@@ -406,7 +416,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_rect_obj, 6, 6, framebuf_rec
406416
STATIC mp_obj_t framebuf_line(size_t n_args, const mp_obj_t *args) {
407417
(void)n_args;
408418

409-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
419+
mp_obj_framebuf_t *self = native_framebuf(args[0]);
410420
mp_int_t x1 = mp_obj_get_int(args[1]);
411421
mp_int_t y1 = mp_obj_get_int(args[2]);
412422
mp_int_t x2 = mp_obj_get_int(args[3]);
@@ -470,8 +480,8 @@ STATIC mp_obj_t framebuf_line(size_t n_args, const mp_obj_t *args) {
470480
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_line_obj, 6, 6, framebuf_line);
471481

472482
STATIC mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *args) {
473-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
474-
mp_obj_framebuf_t *source = MP_OBJ_TO_PTR(args[1]);
483+
mp_obj_framebuf_t *self = native_framebuf(args[0]);
484+
mp_obj_framebuf_t *source = native_framebuf(args[1]);
475485
mp_int_t x = mp_obj_get_int(args[2]);
476486
mp_int_t y = mp_obj_get_int(args[3]);
477487
mp_int_t key = -1;
@@ -513,7 +523,7 @@ STATIC mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *args) {
513523
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_blit_obj, 4, 5, framebuf_blit);
514524

515525
STATIC mp_obj_t framebuf_scroll(mp_obj_t self_in, mp_obj_t xstep_in, mp_obj_t ystep_in) {
516-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in);
526+
mp_obj_framebuf_t *self = native_framebuf(self_in);
517527
mp_int_t xstep = mp_obj_get_int(xstep_in);
518528
mp_int_t ystep = mp_obj_get_int(ystep_in);
519529
int sx, y, xend, yend, dx, dy;
@@ -546,7 +556,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(framebuf_scroll_obj, framebuf_scroll);
546556

547557
STATIC mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args) {
548558
// extract arguments
549-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
559+
mp_obj_framebuf_t *self = native_framebuf(args[0]);
550560
const char *str = mp_obj_str_get_str(args[1]);
551561
mp_int_t x0 = mp_obj_get_int(args[2]);
552562
mp_int_t y0 = mp_obj_get_int(args[3]);

py/objlist.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args);
4646

4747
STATIC void list_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
4848
mp_obj_list_t *o = MP_OBJ_TO_PTR(o_in);
49+
//mp_obj_list_t *o = mp_instance_cast_to_native_base(o_in, &mp_type_list);
4950
if (!(MICROPY_PY_UJSON && kind == PRINT_JSON)) {
5051
kind = PRINT_REPR;
5152
}
@@ -93,7 +94,7 @@ STATIC mp_obj_t list_make_new(const mp_obj_type_t *type_in, size_t n_args, const
9394
}
9495

9596
STATIC mp_obj_t list_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
96-
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
97+
mp_obj_list_t *self = mp_instance_cast_to_native_base(self_in, &mp_type_list);
9798
switch (op) {
9899
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->len != 0);
99100
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
@@ -108,7 +109,7 @@ STATIC mp_obj_t list_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
108109
}
109110

110111
STATIC mp_obj_t list_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) {
111-
mp_obj_list_t *o = MP_OBJ_TO_PTR(lhs);
112+
mp_obj_list_t *o = mp_instance_cast_to_native_base(lhs, &mp_type_list);
112113
switch (op) {
113114
case MP_BINARY_OP_ADD: {
114115
if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) {
@@ -239,7 +240,7 @@ STATIC mp_obj_t list_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
239240

240241
mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
241242
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
242-
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
243+
mp_obj_list_t *self = mp_instance_cast_to_native_base(self_in, &mp_type_list);
243244
if (self->len >= self->alloc) {
244245
self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
245246
self->alloc *= 2;
@@ -252,8 +253,8 @@ mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
252253
STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) {
253254
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
254255
if (MP_OBJ_IS_TYPE(arg_in, &mp_type_list)) {
255-
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
256-
mp_obj_list_t *arg = MP_OBJ_TO_PTR(arg_in);
256+
mp_obj_list_t *self = mp_instance_cast_to_native_base(self_in, &mp_type_list);
257+
mp_obj_list_t *arg = mp_instance_cast_to_native_base(arg_in, &mp_type_list);
257258

258259
if (self->len + arg->len > self->alloc) {
259260
// TODO: use alloc policy for "4"
@@ -272,7 +273,7 @@ STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) {
272273

273274
STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) {
274275
mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
275-
mp_obj_list_t *self = MP_OBJ_TO_PTR(args[0]);
276+
mp_obj_list_t *self = mp_instance_cast_to_native_base(args[0], &mp_type_list);
276277
if (self->len == 0) {
277278
mp_raise_IndexError(translate("pop from empty list"));
278279
}
@@ -332,7 +333,7 @@ mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_
332333
MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t*)&args);
333334

334335
mp_check_self(MP_OBJ_IS_TYPE(pos_args[0], &mp_type_list));
335-
mp_obj_list_t *self = MP_OBJ_TO_PTR(pos_args[0]);
336+
mp_obj_list_t *self = mp_instance_cast_to_native_base(pos_args[0], &mp_type_list);
336337

337338
if (self->len > 1) {
338339
mp_quicksort(self->items, self->items + self->len - 1,
@@ -345,7 +346,7 @@ mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_
345346

346347
mp_obj_t mp_obj_list_clear(mp_obj_t self_in) {
347348
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
348-
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
349+
mp_obj_list_t *self = mp_instance_cast_to_native_base(self_in, &mp_type_list);
349350
self->len = 0;
350351
self->items = m_renew(mp_obj_t, self->items, self->alloc, LIST_MIN_ALLOC);
351352
self->alloc = LIST_MIN_ALLOC;
@@ -355,25 +356,25 @@ mp_obj_t mp_obj_list_clear(mp_obj_t self_in) {
355356

356357
STATIC mp_obj_t list_copy(mp_obj_t self_in) {
357358
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
358-
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
359+
mp_obj_list_t *self = mp_instance_cast_to_native_base(self_in, &mp_type_list);
359360
return mp_obj_new_list(self->len, self->items);
360361
}
361362

362363
STATIC mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
363364
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
364-
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
365+
mp_obj_list_t *self = mp_instance_cast_to_native_base(self_in, &mp_type_list);
365366
return mp_seq_count_obj(self->items, self->len, value);
366367
}
367368

368369
STATIC mp_obj_t list_index(size_t n_args, const mp_obj_t *args) {
369370
mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
370-
mp_obj_list_t *self = MP_OBJ_TO_PTR(args[0]);
371+
mp_obj_list_t *self = mp_instance_cast_to_native_base(args[0], &mp_type_list);
371372
return mp_seq_index_obj(self->items, self->len, n_args, args);
372373
}
373374

374375
STATIC mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
375376
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
376-
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
377+
mp_obj_list_t *self = mp_instance_cast_to_native_base(self_in, &mp_type_list);
377378
// insert has its own strange index logic
378379
mp_int_t index = MP_OBJ_SMALL_INT_VALUE(idx);
379380
if (index < 0) {
@@ -407,7 +408,7 @@ mp_obj_t mp_obj_list_remove(mp_obj_t self_in, mp_obj_t value) {
407408

408409
STATIC mp_obj_t list_reverse(mp_obj_t self_in) {
409410
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
410-
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
411+
mp_obj_list_t *self = mp_instance_cast_to_native_base(self_in, &mp_type_list);
411412

412413
mp_int_t len = self->len;
413414
for (mp_int_t i = 0; i < len/2; i++) {
@@ -484,7 +485,7 @@ mp_obj_t mp_obj_new_list(size_t n, mp_obj_t *items) {
484485
}
485486

486487
void mp_obj_list_get(mp_obj_t self_in, size_t *len, mp_obj_t **items) {
487-
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
488+
mp_obj_list_t *self = mp_instance_cast_to_native_base(self_in, &mp_type_list);
488489
*len = self->len;
489490
*items = self->items;
490491
}
@@ -497,7 +498,7 @@ void mp_obj_list_set_len(mp_obj_t self_in, size_t len) {
497498
}
498499

499500
void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
500-
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
501+
mp_obj_list_t *self = mp_instance_cast_to_native_base(self_in, &mp_type_list);
501502
size_t i = mp_get_index(self->base.type, self->len, index, false);
502503
self->items[i] = value;
503504
}

0 commit comments

Comments
 (0)