aboutsummaryrefslogtreecommitdiff
path: root/py/objtype.c
diff options
context:
space:
mode:
authorDamien George2014-03-31 22:57:56 +0100
committerDamien George2014-03-31 22:57:56 +0100
commite44d26ae0c1b5d248fa4db112cdeabe404944f3c (patch)
tree03bad17caf73555d5880263e894b5dc2fdb5dbe2 /py/objtype.c
parent4db727afea0082780fca558ff251afb4a8b32ad7 (diff)
py: Implement __getattr__.
It's not completely satisfactory, because a failed call to __getattr__ should not raise an exception. __setattr__ could be implemented, but it would slow down all stores to a user created object. Need to implement some caching system.
Diffstat (limited to 'py/objtype.c')
-rw-r--r--py/objtype.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/py/objtype.c b/py/objtype.c
index 06bc803eb..11200c913 100644
--- a/py/objtype.c
+++ b/py/objtype.c
@@ -16,6 +16,7 @@
typedef struct _mp_obj_class_t {
mp_obj_base_t base;
mp_map_t members;
+ // TODO maybe cache __getattr__ and __setattr__ for efficient lookup of them
} mp_obj_class_t;
STATIC mp_obj_t mp_obj_new_class(mp_obj_t class) {
@@ -225,6 +226,19 @@ STATIC void class_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
} else {
// class member is a value, so just return that value
dest[0] = member;
+ }
+ return;
+ }
+
+ // try __getattr__
+ if (attr != MP_QSTR___getattr__) {
+ mp_obj_t dest2[3];
+ mp_load_method_maybe(self_in, MP_QSTR___getattr__, dest2);
+ if (dest2[0] != MP_OBJ_NULL) {
+ // __getattr__ exists, call it and return its result
+ // XXX if this fails to load the requested attr, should we catch the attribute error and return silently?
+ dest2[2] = MP_OBJ_NEW_QSTR(attr);
+ dest[0] = mp_call_method_n_kw(1, 0, dest2);
return;
}
}