diff options
-rw-r--r-- | py/builtin.c | 16 | ||||
-rw-r--r-- | py/runtime.c | 14 | ||||
-rw-r--r-- | py/runtime.h | 1 |
3 files changed, 20 insertions, 11 deletions
diff --git a/py/builtin.c b/py/builtin.c index 7a01731d06..de1a8fc6a5 100644 --- a/py/builtin.c +++ b/py/builtin.c @@ -400,6 +400,22 @@ STATIC mp_obj_t mp_builtin_id(mp_obj_t o_in) { MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_builtin_id); +// See mp_load_attr() if making any changes +STATIC inline mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t defval) { + mp_obj_t dest[2]; + // use load_method, raising or not raising exception + ((defval == MP_OBJ_NULL) ? mp_load_method : mp_load_method_maybe)(base, attr, dest); + if (dest[0] == MP_OBJ_NULL) { + return defval; + } else if (dest[1] == MP_OBJ_NULL) { + // load_method returned just a normal attribute + return dest[0]; + } else { + // load_method returned a method, so build a bound method object + return mp_obj_new_bound_meth(dest[0], dest[1]); + } +} + STATIC mp_obj_t mp_builtin_getattr(uint n_args, const mp_obj_t *args) { assert(MP_OBJ_IS_QSTR(args[1])); mp_obj_t defval = MP_OBJ_NULL; diff --git a/py/runtime.c b/py/runtime.c index f9361a8a5e..1ab971de4c 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -672,14 +672,12 @@ too_long: nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "too many values to unpack (expected %d)", num)); } -mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t defval) { +mp_obj_t mp_load_attr(mp_obj_t base, qstr attr) { DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr)); + // use load_method mp_obj_t dest[2]; - // use load_method, raising or not raising exception - ((defval == MP_OBJ_NULL) ? mp_load_method : mp_load_method_maybe)(base, attr, dest); - if (dest[0] == MP_OBJ_NULL) { - return defval; - } else if (dest[1] == MP_OBJ_NULL) { + mp_load_method(base, attr, dest); + if (dest[1] == MP_OBJ_NULL) { // load_method returned just a normal attribute return dest[0]; } else { @@ -688,10 +686,6 @@ mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t defval) { } } -mp_obj_t mp_load_attr(mp_obj_t base, qstr attr) { - return mp_load_attr_default(base, attr, MP_OBJ_NULL); -} - // no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL // normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL // method attribute found, returns: dest[0] == <method>, dest[1] == <self> diff --git a/py/runtime.h b/py/runtime.h index a627fa509b..8e59550af5 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -45,7 +45,6 @@ mp_obj_t mp_call_method_n_kw_var(bool have_self, uint n_args_n_kw, const mp_obj_ void mp_unpack_sequence(mp_obj_t seq, uint num, mp_obj_t *items); mp_obj_t mp_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value); mp_obj_t mp_load_attr(mp_obj_t base, qstr attr); -mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t defval); void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest); void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest); void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t val); |