Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 000ae6b

Browse filesBrowse files
committed
support subclasses
1 parent 84b8ae1 commit 000ae6b
Copy full SHA for 000ae6b

1 file changed

+19-21Lines changed: 19 additions & 21 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎shared-bindings/_pixelbuf/PixelBuf.c‎

Copy file name to clipboardExpand all lines: shared-bindings/_pixelbuf/PixelBuf.c
+19-21Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include "py/obj.h"
2828
#include "py/objarray.h"
29+
#include "py/objtype.h"
2930
#include "py/mphal.h"
3031
#include "py/runtime.h"
3132
#include "py/binary.h"
@@ -223,13 +224,20 @@ STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_a
223224
return MP_OBJ_FROM_PTR(self);
224225
}
225226

227+
228+
// Helper to ensure we have the native super class instead of a subclass.
229+
static pixelbuf_pixelbuf_obj_t* native_pixelbuf(mp_obj_t pixelbuf_obj) {
230+
mp_obj_t native_pixelbuf = mp_instance_cast_to_native_base(pixelbuf_obj, &pixelbuf_pixelbuf_type);
231+
mp_obj_assert_native_inited(native_pixelbuf);
232+
return MP_OBJ_TO_PTR(native_pixelbuf);
233+
}
234+
226235
//| .. attribute:: bpp
227236
//|
228237
//| The number of bytes per pixel in the buffer (read-only)
229238
//|
230239
STATIC mp_obj_t pixelbuf_pixelbuf_obj_get_bpp(mp_obj_t self_in) {
231-
mp_check_self(MP_OBJ_IS_TYPE(self_in, &pixelbuf_pixelbuf_type));
232-
pixelbuf_pixelbuf_obj_t *self = MP_OBJ_TO_PTR(self_in);
240+
pixelbuf_pixelbuf_obj_t *self = native_pixelbuf(self_in);
233241
return mp_obj_new_int_from_uint(self->byteorder.bpp);
234242
}
235243
MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_pixelbuf_get_bpp_obj, pixelbuf_pixelbuf_obj_get_bpp);
@@ -252,16 +260,14 @@ const mp_obj_property_t pixelbuf_pixelbuf_bpp_obj = {
252260
//| In DotStar mode
253261
//|
254262
STATIC mp_obj_t pixelbuf_pixelbuf_obj_get_brightness(mp_obj_t self_in) {
255-
mp_check_self(MP_OBJ_IS_TYPE(self_in, &pixelbuf_pixelbuf_type));
256-
pixelbuf_pixelbuf_obj_t *self = MP_OBJ_TO_PTR(self_in);
263+
pixelbuf_pixelbuf_obj_t *self = native_pixelbuf(self_in);
257264
return mp_obj_new_float(self->brightness);
258265
}
259266
MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_pixelbuf_get_brightness_obj, pixelbuf_pixelbuf_obj_get_brightness);
260267

261268

262269
STATIC mp_obj_t pixelbuf_pixelbuf_obj_set_brightness(mp_obj_t self_in, mp_obj_t value) {
263-
mp_check_self(MP_OBJ_IS_TYPE(self_in, &pixelbuf_pixelbuf_type));
264-
pixelbuf_pixelbuf_obj_t *self = MP_OBJ_TO_PTR(self_in);
270+
pixelbuf_pixelbuf_obj_t *self = native_pixelbuf(self_in);
265271
self->brightness = mp_obj_float_get(value);
266272
if (self->brightness > 1)
267273
self->brightness = 1;
@@ -298,16 +304,14 @@ void pixelbuf_recalculate_brightness(pixelbuf_pixelbuf_obj_t *self) {
298304
//| Whether to automatically write the pixels after each update.
299305
//|
300306
STATIC mp_obj_t pixelbuf_pixelbuf_obj_get_auto_write(mp_obj_t self_in) {
301-
mp_check_self(MP_OBJ_IS_TYPE(self_in, &pixelbuf_pixelbuf_type));
302-
pixelbuf_pixelbuf_obj_t *self = MP_OBJ_TO_PTR(self_in);
307+
pixelbuf_pixelbuf_obj_t *self = native_pixelbuf(self_in);
303308
return mp_obj_new_bool(self->auto_write);
304309
}
305310
MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_pixelbuf_get_auto_write_obj, pixelbuf_pixelbuf_obj_get_auto_write);
306311

307312

308313
STATIC mp_obj_t pixelbuf_pixelbuf_obj_set_auto_write(mp_obj_t self_in, mp_obj_t value) {
309-
mp_check_self(MP_OBJ_IS_TYPE(self_in, &pixelbuf_pixelbuf_type));
310-
pixelbuf_pixelbuf_obj_t *self = MP_OBJ_TO_PTR(self_in);
314+
pixelbuf_pixelbuf_obj_t *self = native_pixelbuf(self_in);
311315
self->auto_write = mp_obj_is_true(value);
312316
return mp_const_none;
313317
}
@@ -328,8 +332,7 @@ const mp_obj_property_t pixelbuf_pixelbuf_auto_write_obj = {
328332
//| actual pixels.
329333
//|
330334
STATIC mp_obj_t pixelbuf_pixelbuf_obj_get_buf(mp_obj_t self_in) {
331-
mp_check_self(MP_OBJ_IS_TYPE(self_in, &pixelbuf_pixelbuf_type));
332-
pixelbuf_pixelbuf_obj_t *self = MP_OBJ_TO_PTR(self_in);
335+
pixelbuf_pixelbuf_obj_t *self = native_pixelbuf(self_in);
333336
return mp_obj_new_bytearray_by_ref(self->bytes, self->buf);
334337
}
335338
MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_pixelbuf_get_buf_obj, pixelbuf_pixelbuf_obj_get_buf);
@@ -346,8 +349,7 @@ const mp_obj_property_t pixelbuf_pixelbuf_buf_obj = {
346349
//| byteorder string for the buffer (read-only)
347350
//|
348351
STATIC mp_obj_t pixelbuf_pixelbuf_obj_get_byteorder(mp_obj_t self_in) {
349-
mp_check_self(MP_OBJ_IS_TYPE(self_in, &pixelbuf_pixelbuf_type));
350-
pixelbuf_pixelbuf_obj_t *self = MP_OBJ_TO_PTR(self_in);
352+
pixelbuf_pixelbuf_obj_t *self = native_pixelbuf(self_in);
351353
return mp_obj_new_str(self->byteorder.order, strlen(self->byteorder.order));
352354
}
353355
MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_pixelbuf_get_byteorder_str, pixelbuf_pixelbuf_obj_get_byteorder);
@@ -360,8 +362,7 @@ const mp_obj_property_t pixelbuf_pixelbuf_byteorder_str = {
360362
};
361363

362364
STATIC mp_obj_t pixelbuf_pixelbuf_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
363-
mp_check_self(MP_OBJ_IS_TYPE(self_in, &pixelbuf_pixelbuf_type));
364-
pixelbuf_pixelbuf_obj_t *self = MP_OBJ_TO_PTR(self_in);
365+
pixelbuf_pixelbuf_obj_t *self = native_pixelbuf(self_in);
365366
switch (op) {
366367
case MP_UNARY_OP_BOOL: return mp_const_true;
367368
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->pixels);
@@ -375,8 +376,7 @@ STATIC mp_obj_t pixelbuf_pixelbuf_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
375376
//|
376377

377378
STATIC mp_obj_t pixelbuf_pixelbuf_show(mp_obj_t self_in) {
378-
mp_check_self(MP_OBJ_IS_TYPE(self_in, &pixelbuf_pixelbuf_type));
379-
pixelbuf_pixelbuf_obj_t *self = MP_OBJ_TO_PTR(self_in);
379+
pixelbuf_pixelbuf_obj_t *self = native_pixelbuf(self_in);
380380
call_write_function(self);
381381
return mp_const_none;
382382
}
@@ -398,15 +398,13 @@ void call_write_function(pixelbuf_pixelbuf_obj_t *self) {
398398
//| Sets the pixel value at the given index.
399399
//|
400400
STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
401-
mp_check_self(MP_OBJ_IS_TYPE(self_in, &pixelbuf_pixelbuf_type));
402-
403401
if (value == MP_OBJ_NULL) {
404402
// delete item
405403
// slice deletion
406404
return MP_OBJ_NULL; // op not supported
407405
}
408406

409-
pixelbuf_pixelbuf_obj_t *self = MP_OBJ_TO_PTR(self_in);
407+
pixelbuf_pixelbuf_obj_t *self = native_pixelbuf(self_in);
410408
if (0) {
411409
#if MICROPY_PY_BUILTINS_SLICE
412410
} else if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.