summaryrefslogtreecommitdiffstatshomepage
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
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.
-rw-r--r--py/mpconfig.h6
-rw-r--r--py/objtype.c34
-rw-r--r--tests/basics/class_delattr_setattr.py63
-rw-r--r--unix/mpconfigport_coverage.h1
4 files changed, 104 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;
}
diff --git a/tests/basics/class_delattr_setattr.py b/tests/basics/class_delattr_setattr.py
new file mode 100644
index 0000000000..0d061aee68
--- /dev/null
+++ b/tests/basics/class_delattr_setattr.py
@@ -0,0 +1,63 @@
+# test __delattr__ and __setattr__
+
+# feature test for __setattr__/__delattr__
+try:
+ class Test():
+ def __delattr__(self, attr): pass
+ del Test().noexist
+except AttributeError:
+ import sys
+ print('SKIP')
+ sys.exit()
+
+# this class just prints the calls to see if they were executed
+class A():
+ def __getattr__(self, attr):
+ print('get', attr)
+ return 1
+ def __setattr__(self, attr, val):
+ print('set', attr, val)
+ def __delattr__(self, attr):
+ print('del', attr)
+a = A()
+
+# check basic behaviour
+print(getattr(a, 'foo'))
+setattr(a, 'bar', 2)
+delattr(a, 'baz')
+
+# check meta behaviour
+getattr(a, '__getattr__') # should not call A.__getattr__
+getattr(a, '__setattr__') # should not call A.__getattr__
+getattr(a, '__delattr__') # should not call A.__getattr__
+setattr(a, '__setattr__', 1) # should call A.__setattr__
+delattr(a, '__delattr__') # should call A.__delattr__
+
+# this class acts like a dictionary
+class B:
+ def __init__(self, d):
+ # store the dict in the class, not instance, so
+ # we don't get infinite recursion in __getattr_
+ B.d = d
+
+ def __getattr__(self, attr):
+ if attr in B.d:
+ return B.d[attr]
+ else:
+ raise AttributeError(attr)
+
+ def __setattr__(self, attr, value):
+ B.d[attr] = value
+
+ def __delattr__(self, attr):
+ del B.d[attr]
+
+a = B({"a":1, "b":2})
+print(a.a, a.b)
+a.a = 3
+print(a.a, a.b)
+del a.a
+try:
+ print(a.a)
+except AttributeError:
+ print("AttributeError")
diff --git a/unix/mpconfigport_coverage.h b/unix/mpconfigport_coverage.h
index 87a743cf87..9df8d0fca8 100644
--- a/unix/mpconfigport_coverage.h
+++ b/unix/mpconfigport_coverage.h
@@ -32,6 +32,7 @@
#include <mpconfigport.h>
+#define MICROPY_PY_DELATTR_SETATTR (1)
#define MICROPY_PY_BUILTINS_HELP (1)
#define MICROPY_PY_BUILTINS_HELP_MODULES (1)
#define MICROPY_PY_URANDOM_EXTRA_FUNCS (1)