summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorScott Shawcroft <scott@tannewt.org>2019-05-13 17:31:30 -0700
committerScott Shawcroft <scott@tannewt.org>2019-05-13 17:31:30 -0700
commitf29de5132562cd6e88661d150fa1ee3ab8b771e1 (patch)
tree1f3ac8c4779e4badc825d22fac3c38aceb5bb99e /py
parenta6785d7ff74e156c5bcfdd8f46b1cccae41cc3bf (diff)
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
Diffstat (limited to 'py')
-rw-r--r--py/objtype.c10
-rw-r--r--py/objtype.h2
2 files changed, 12 insertions, 0 deletions
diff --git a/py/objtype.c b/py/objtype.c
index 88e918827..5133d849f 100644
--- a/py/objtype.c
+++ b/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
diff --git a/py/objtype.h b/py/objtype.h
index 13613f01f..a32c87496 100644
--- a/py/objtype.h
+++ b/py/objtype.h
@@ -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);