summaryrefslogtreecommitdiff
path: root/tests/basics/class_delattr_setattr.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 /tests/basics/class_delattr_setattr.py
parentec7dc7f8d796b5b67772d1f40863d13fb5e19be2 (diff)
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 'tests/basics/class_delattr_setattr.py')
-rw-r--r--tests/basics/class_delattr_setattr.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/tests/basics/class_delattr_setattr.py b/tests/basics/class_delattr_setattr.py
new file mode 100644
index 000000000..0d061aee6
--- /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")