summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
authordmazzella <damianomazzella@gmail.com>2017-01-03 11:00:12 +0100
committerDamien George <damien.p.george@gmail.com>2017-02-09 12:40:15 +1100
commit18e65691661ef8e83060d0e72d66b16cc918c8b4 (patch)
treed1d7f8b5d86c0119a061bf08d07ccec5d827f7c5 /py
parentec7dc7f8d796b5b67772d1f40863d13fb5e19be2 (diff)
downloadmicropython-18e65691661ef8e83060d0e72d66b16cc918c8b4.tar.gz
micropython-18e65691661ef8e83060d0e72d66b16cc918c8b4.zip
py/objtype: Implement __delattr__ and __setattr__.
This patch implements support for class methods __delattr__ and __setattr__ for customising attribute access. It is controlled by the config option MICROPY_PY_DELATTR_SETATTR and is disabled by default.
Diffstat (limited to 'py')
-rw-r--r--py/mpconfig.h6
-rw-r--r--py/objtype.c34
2 files changed, 40 insertions, 0 deletions
diff --git a/py/mpconfig.h b/py/mpconfig.h
index afd9a0be5e..093625a461 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -635,6 +635,12 @@ typedef double mp_float_t;
#define MICROPY_PY_DESCRIPTORS (0)
#endif
+// Whether to support class __delattr__ and __setattr__ methods
+// This costs some code size and makes all del attrs and store attrs slow
+#ifndef MICROPY_PY_DELATTR_SETATTR
+#define MICROPY_PY_DELATTR_SETATTR (0)
+#endif
+
// Support for async/await/async for/async with
#ifndef MICROPY_PY_ASYNC_AWAIT
#define MICROPY_PY_ASYNC_AWAIT (1)
diff --git a/py/objtype.c b/py/objtype.c
index c20b0693e5..85e10e7624 100644
--- a/py/objtype.c
+++ b/py/objtype.c
@@ -533,6 +533,15 @@ STATIC void mp_obj_instance_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *des
// try __getattr__
if (attr != MP_QSTR___getattr__) {
+ #if MICROPY_PY_DELATTR_SETATTR
+ // If the requested attr is __setattr__/__delattr__ then don't delegate the lookup
+ // to __getattr__. If we followed CPython's behaviour then __setattr__/__delattr__
+ // would have already been found in the "object" base class.
+ if (attr == MP_QSTR___setattr__ || attr == MP_QSTR___delattr__) {
+ return;
+ }
+ #endif
+
mp_obj_t dest2[3];
mp_load_method_maybe(self_in, MP_QSTR___getattr__, dest2);
if (dest2[0] != MP_OBJ_NULL) {
@@ -626,10 +635,35 @@ STATIC bool mp_obj_instance_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t val
if (value == MP_OBJ_NULL) {
// delete attribute
+ #if MICROPY_PY_DELATTR_SETATTR
+ // try __delattr__ first
+ mp_obj_t attr_delattr_method[3];
+ mp_load_method_maybe(self_in, MP_QSTR___delattr__, attr_delattr_method);
+ if (attr_delattr_method[0] != MP_OBJ_NULL) {
+ // __delattr__ exists, so call it
+ attr_delattr_method[2] = MP_OBJ_NEW_QSTR(attr);
+ mp_call_method_n_kw(1, 0, attr_delattr_method);
+ return true;
+ }
+ #endif
+
mp_map_elem_t *elem = mp_map_lookup(&self->members, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_REMOVE_IF_FOUND);
return elem != NULL;
} else {
// store attribute
+ #if MICROPY_PY_DELATTR_SETATTR
+ // try __setattr__ first
+ mp_obj_t attr_setattr_method[4];
+ mp_load_method_maybe(self_in, MP_QSTR___setattr__, attr_setattr_method);
+ if (attr_setattr_method[0] != MP_OBJ_NULL) {
+ // __setattr__ exists, so call it
+ attr_setattr_method[2] = MP_OBJ_NEW_QSTR(attr);
+ attr_setattr_method[3] = value;
+ mp_call_method_n_kw(2, 0, attr_setattr_method);
+ return true;
+ }
+ #endif
+
mp_map_lookup(&self->members, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
return true;
}