Check native object in case of early access
If a native displayio object is accessed before it's super().__init__() has been called, then a placeholder is given that will cause a crash if accessed. This is tricky to get right so we detect this case and raise a NotInplementedError instead of crashing. Fixes #1881
This commit is contained in:
parent
a6785d7ff7
commit
f29de51325
10
py/objtype.c
10
py/objtype.c
@ -117,6 +117,16 @@ mp_obj_instance_t *mp_obj_new_instance(const mp_obj_type_t *class, const mp_obj_
|
||||
return o;
|
||||
}
|
||||
|
||||
// When instances are first created they have the base_init wrapper as their native parent's
|
||||
// instance because make_new combines __new__ and __init__. This object is invalid for the native
|
||||
// code so it must call this method to ensure that the given object has been __init__'d and is
|
||||
// valid.
|
||||
void mp_obj_assert_native_inited(mp_obj_t native_object) {
|
||||
if (native_object == MP_OBJ_FROM_PTR(&native_base_init_wrapper_obj)) {
|
||||
mp_raise_NotImplementedError(translate("Call super().__init__() before accessing native object."));
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
// This implements depth-first left-to-right MRO, which is not compliant with Python3 MRO
|
||||
// http://python-history.blogspot.com/2010/06/method-resolution-order.html
|
||||
|
@ -37,6 +37,8 @@ typedef struct _mp_obj_instance_t {
|
||||
// TODO maybe cache __getattr__ and __setattr__ for efficient lookup of them
|
||||
} mp_obj_instance_t;
|
||||
|
||||
void mp_obj_assert_native_inited(mp_obj_t native_object);
|
||||
|
||||
#if MICROPY_CPYTHON_COMPAT
|
||||
// this is needed for object.__new__
|
||||
mp_obj_instance_t *mp_obj_new_instance(const mp_obj_type_t *cls, const mp_obj_type_t **native_base);
|
||||
|
@ -85,6 +85,7 @@ STATIC mp_obj_t stage_render(size_t n_args, const mp_obj_t *args) {
|
||||
|
||||
mp_obj_t native_display = mp_instance_cast_to_native_base(args[6],
|
||||
&displayio_display_type);
|
||||
mp_obj_assert_native_inited(native_display);
|
||||
if (!MP_OBJ_IS_TYPE(native_display, &displayio_display_type)) {
|
||||
mp_raise_TypeError(translate("argument num/types mismatch"));
|
||||
}
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include "lib/utils/context_manager_helpers.h"
|
||||
#include "py/binary.h"
|
||||
#include "py/objproperty.h"
|
||||
#include "py/objtype.h"
|
||||
#include "py/runtime.h"
|
||||
#include "shared-bindings/displayio/Group.h"
|
||||
#include "shared-bindings/microcontroller/Pin.h"
|
||||
@ -172,6 +173,7 @@ STATIC mp_obj_t displayio_display_make_new(const mp_obj_type_t *type, size_t n_a
|
||||
// Helper to ensure we have the native super class instead of a subclass.
|
||||
static displayio_display_obj_t* native_display(mp_obj_t display_obj) {
|
||||
mp_obj_t native_display = mp_instance_cast_to_native_base(display_obj, &displayio_display_type);
|
||||
mp_obj_assert_native_inited(native_display);
|
||||
return MP_OBJ_TO_PTR(native_display);
|
||||
}
|
||||
|
||||
@ -184,11 +186,7 @@ STATIC mp_obj_t displayio_display_obj_show(mp_obj_t self_in, mp_obj_t group_in)
|
||||
displayio_display_obj_t *self = native_display(self_in);
|
||||
displayio_group_t* group = NULL;
|
||||
if (group_in != mp_const_none) {
|
||||
mp_obj_t native_layer = mp_instance_cast_to_native_base(group_in, &displayio_group_type);
|
||||
if (native_layer == MP_OBJ_NULL) {
|
||||
mp_raise_ValueError(translate("Must be a Group subclass."));
|
||||
}
|
||||
group = MP_OBJ_TO_PTR(native_layer);
|
||||
group = MP_OBJ_TO_PTR(native_group(group_in));
|
||||
}
|
||||
|
||||
common_hal_displayio_display_show(self, group);
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include "lib/utils/context_manager_helpers.h"
|
||||
#include "py/binary.h"
|
||||
#include "py/objproperty.h"
|
||||
#include "py/objtype.h"
|
||||
#include "py/runtime.h"
|
||||
#include "supervisor/shared/translate.h"
|
||||
|
||||
@ -80,8 +81,12 @@ STATIC mp_obj_t displayio_group_make_new(const mp_obj_type_t *type, size_t n_arg
|
||||
}
|
||||
|
||||
// Helper to ensure we have the native super class instead of a subclass.
|
||||
static displayio_group_t* native_group(mp_obj_t group_obj) {
|
||||
displayio_group_t* native_group(mp_obj_t group_obj) {
|
||||
mp_obj_t native_group = mp_instance_cast_to_native_base(group_obj, &displayio_group_type);
|
||||
if (native_group == MP_OBJ_NULL) {
|
||||
mp_raise_ValueError_varg(translate("Must be a %q subclass."), MP_QSTR_Group);
|
||||
}
|
||||
mp_obj_assert_native_inited(native_group);
|
||||
return MP_OBJ_TO_PTR(native_group);
|
||||
}
|
||||
|
||||
|
@ -31,6 +31,7 @@
|
||||
|
||||
extern const mp_obj_type_t displayio_group_type;
|
||||
|
||||
displayio_group_t* native_group(mp_obj_t group_obj);
|
||||
|
||||
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);
|
||||
uint32_t common_hal_displayio_group_get_scale(displayio_group_t* self);
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include "lib/utils/context_manager_helpers.h"
|
||||
#include "py/binary.h"
|
||||
#include "py/objproperty.h"
|
||||
#include "py/objtype.h"
|
||||
#include "py/runtime.h"
|
||||
#include "shared-bindings/displayio/Bitmap.h"
|
||||
#include "shared-bindings/displayio/ColorConverter.h"
|
||||
@ -139,6 +140,7 @@ STATIC mp_obj_t displayio_tilegrid_make_new(const mp_obj_type_t *type, size_t n_
|
||||
// Helper to ensure we have the native super class instead of a subclass.
|
||||
static displayio_tilegrid_t* native_tilegrid(mp_obj_t tilegrid_obj) {
|
||||
mp_obj_t native_tilegrid = mp_instance_cast_to_native_base(tilegrid_obj, &displayio_tilegrid_type);
|
||||
mp_obj_assert_native_inited(native_tilegrid);
|
||||
return MP_OBJ_TO_PTR(native_tilegrid);
|
||||
}
|
||||
|
||||
|
@ -257,7 +257,7 @@ STATIC mp_int_t _socket_recv_into(mod_network_socket_obj_t *sock, byte *buf, mp_
|
||||
//| Reads some bytes from the connected remote address, writing
|
||||
//| into the provided buffer. If bufsize <= len(buffer) is given,
|
||||
//| a maximum of bufsize bytes will be read into the buffer. If no
|
||||
//| valid value is given for bufsize, the default is the length of
|
||||
//| valid value is given for bufsize, the default is the length of
|
||||
//| the given buffer.
|
||||
//|
|
||||
//| Suits sockets of type SOCK_STREAM
|
||||
@ -274,13 +274,14 @@ STATIC mp_obj_t socket_recv_into(size_t n_args, const mp_obj_t *args) {
|
||||
}
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
|
||||
mp_int_t len;
|
||||
mp_int_t len = bufinfo.len;
|
||||
if (n_args == 3) {
|
||||
len = mp_obj_get_int(args[2]);
|
||||
}
|
||||
if (n_args == 2 || (size_t) len > bufinfo.len) {
|
||||
len = bufinfo.len;
|
||||
mp_int_t given_len = mp_obj_get_int(args[2]);
|
||||
if (given_len < len) {
|
||||
len = given_len;
|
||||
}
|
||||
}
|
||||
|
||||
mp_int_t ret = _socket_recv_into(self, (byte*)bufinfo.buf, len);
|
||||
return mp_obj_new_int_from_uint(ret);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user