diff options
| author | Dan Halbert <halbert@adafruit.com> | 2019-05-14 15:30:44 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-05-14 15:30:44 -0400 |
| commit | d3ce9280e2c00d62c327b25edc271d4ca97c4fae (patch) | |
| tree | 2c076359868e0d3a301eb2834b5835f6c2589c50 /py | |
| parent | c6c6171472bcf67d3d9b505e56acb72e86748389 (diff) | |
| parent | e74f5d5d7695ec49bd489dedd35daf2a90c2da21 (diff) | |
Merge pull request #1883 from tannewt/fix_1881
Check native object in case of early access
Diffstat (limited to 'py')
| -rw-r--r-- | py/objtype.c | 10 | ||||
| -rw-r--r-- | py/objtype.h | 2 |
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); |
