summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_descr.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-11-10 22:12:24 (GMT)
committerGuido van Rossum <guido@python.org>2007-11-10 22:12:24 (GMT)
commitd1ef78942adedbb0d3483b43911038a44a77ad4d (patch)
tree87a53f3589ff9430c8c09b538ce70dd1745f3e85 /Lib/test/test_descr.py
parentd3ffd341b8370f0bd134ecc6c1b634a12f35446d (diff)
downloadcpython-d1ef78942adedbb0d3483b43911038a44a77ad4d.zip
cpython-d1ef78942adedbb0d3483b43911038a44a77ad4d.tar.gz
cpython-d1ef78942adedbb0d3483b43911038a44a77ad4d.tar.bz2
Issue 1416. Add getter, setter, deleter methods to properties that can be
used as decorators to create fully-populated properties.
Diffstat (limited to 'Lib/test/test_descr.py')
-rw-r--r--Lib/test/test_descr.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index 9a45cf1..0c06d9a 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -2131,6 +2131,71 @@ def properties():
p = property(_testcapi.test_with_docstring)
+def properties_plus():
+ class C:
+ foo = property(doc="hello")
+ @foo.getter
+ def foo(self):
+ return self._foo
+ @foo.setter
+ def foo(self, value):
+ self._foo = abs(value)
+ @foo.deleter
+ def foo(self):
+ del self._foo
+ c = C()
+ assert C.foo.__doc__ == "hello"
+ assert not hasattr(c, "foo")
+ c.foo = -42
+ assert c.foo == 42
+ del c.foo
+ assert not hasattr(c, "foo")
+
+ class D(C):
+ @C.foo.deleter
+ def foo(self):
+ try:
+ del self._foo
+ except AttributeError:
+ pass
+ d = D()
+ d.foo = 24
+ assert d.foo == 24
+ del d.foo
+ del d.foo
+
+ class E:
+ @property
+ def foo(self):
+ return self._foo
+ @foo.setter
+ def foo (self, value):
+ raise RuntimeError
+ @foo.setter
+ @foo.deleter
+ def foo(self, value=None):
+ if value is None:
+ del self._foo
+ else:
+ self._foo = abs(value)
+ e = E()
+ e.foo = -42
+ assert e.foo == 42
+ del e.foo
+
+ class F(E):
+ @E.foo.deleter
+ def foo(self):
+ del self._foo
+ @foo.setter
+ def foo(self, value):
+ self._foo = max(0, value)
+ f = F()
+ f.foo = -10
+ assert f.foo == 0
+ del f.foo
+
+
def supers():
if verbose: print "Testing super..."