[Python-checkins] CVS: python/dist/src/Lib/test test_descr.py,1.73,1.74

Tim Peters tim_one@users.sourceforge.net
Mon, 24 Sep 2001 14:17:52 -0700


Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv32355/python/Lib/test

Modified Files:
	test_descr.py 
Log Message:
Make properties discoverable from Python:

- property() now takes 4 keyword arguments:  fget, fset, fdel, doc.
  Note that the real purpose of the 'f' prefix is to make fdel fit in
  ('del' is a keyword, so can't used as a keyword argument name).

- These map to visible readonly attributes 'fget', 'fset', 'fdel',
  and '__doc__' in the property object.

- fget/fset/fdel weren't discoverable from Python before.

- __doc__ is new, and allows to associate a docstring with a property.


Index: test_descr.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_descr.py,v
retrieving revision 1.73
retrieving revision 1.74
diff -C2 -d -r1.73 -r1.74
*** test_descr.py	2001/09/24 18:47:40	1.73
--- test_descr.py	2001/09/24 21:17:50	1.74
***************
*** 1365,1369 ****
          def delx(self):
              del self.__x
!         x = property(getx, setx, delx)
      a = C()
      verify(not hasattr(a, "x"))
--- 1365,1369 ----
          def delx(self):
              del self.__x
!         x = property(getx, setx, delx, doc="I'm the x property.")
      a = C()
      verify(not hasattr(a, "x"))
***************
*** 1379,1382 ****
--- 1379,1408 ----
  ##    verify(not hasattr(a, "x"))
  
+     raw = C.__dict__['x']
+     verify(isinstance(raw, property))
+ 
+     attrs = dir(raw)
+     verify("__doc__" in attrs)
+     verify("fget" in attrs)
+     verify("fset" in attrs)
+     verify("fdel" in attrs)
+ 
+     verify(raw.__doc__ == "I'm the x property.")
+     verify(raw.fget is C.__dict__['getx'])
+     verify(raw.fset is C.__dict__['setx'])
+     verify(raw.fdel is C.__dict__['delx'])
+ 
+     for attr in "__doc__", "fget", "fset", "fdel":
+         try:
+             setattr(raw, attr, 42)
+         except TypeError, msg:
+             if str(msg).find('readonly') < 0:
+                 raise TestFailed("when setting readonly attr %r on a "
+                                  "property, got unexpected TypeError "
+                                  "msg %r" % (attr, str(msg)))
+         else:
+             raise TestFailed("expected TypeError from trying to set "
+                              "readonly %r attr on a property" % attr)
+ 
  def supers():
      if verbose: print "Testing super..."
***************
*** 1885,1889 ****
      verify(zz == 1+0j)
      verify(1+0j == zz)
!             
      class classic:
          pass
--- 1911,1915 ----
      verify(zz == 1+0j)
      verify(1+0j == zz)
! 
      class classic:
          pass