diff options
author | Damien George <damien.p.george@gmail.com> | 2014-04-05 18:00:04 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-04-05 18:00:04 +0100 |
commit | 23419a2f8a2097c6ba00455eee4f7ae736fdf878 (patch) | |
tree | 347bab7726fb05141dd39a1d103f8a8c3f4b01eb /py/runtime.c | |
parent | 6d508666ea9f524f5fc16fa95052e06144ac728e (diff) | |
parent | bfb7d6a26d6fe5b9a75447085514c609204eb469 (diff) | |
download | micropython-23419a2f8a2097c6ba00455eee4f7ae736fdf878.tar.gz micropython-23419a2f8a2097c6ba00455eee4f7ae736fdf878.zip |
Merge pull request #433 from pfalcon/getattr-3arg
py: Support 3-arg getattr() builtin (with default value).
Diffstat (limited to 'py/runtime.c')
-rw-r--r-- | py/runtime.c | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/py/runtime.c b/py/runtime.c index 1a1db46ec5..5c9df06a28 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -690,12 +690,14 @@ too_long: nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "too many values to unpack (expected %d)", num)); } -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) { DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr)); - // use load_method mp_obj_t dest[2]; - mp_load_method(base, attr, dest); - if (dest[1] == MP_OBJ_NULL) { + // 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 { @@ -704,6 +706,10 @@ mp_obj_t mp_load_attr(mp_obj_t base, qstr attr) { } } +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> |