[Zope3-checkins] SVN: Zope3/trunk/src/zope/schema/ Make sure readonly is enforced. Once the attribute is set once, it cannot

Stephan Richter srichter at cosmos.phy.tufts.edu
Wed Dec 7 16:49:44 EST 2005


Log message for revision 40640:
  Make sure readonly is enforced. Once the attribute is set once, it cannot 
  be reassigned.
  

Changed:
  U   Zope3/trunk/src/zope/schema/fieldproperty.py
  U   Zope3/trunk/src/zope/schema/tests/test_fieldproperty.py

-=-
Modified: Zope3/trunk/src/zope/schema/fieldproperty.py
===================================================================
--- Zope3/trunk/src/zope/schema/fieldproperty.py	2005-12-07 21:49:15 UTC (rev 40639)
+++ Zope3/trunk/src/zope/schema/fieldproperty.py	2005-12-07 21:49:43 UTC (rev 40640)
@@ -50,6 +50,8 @@
     def __set__(self, inst, value):
         field = self.__field.bind(inst)
         field.validate(value)
+        if field.readonly and inst.__dict__.has_key(self.__name):
+            raise ValueError(self.__name, 'field is readonly')
         inst.__dict__[self.__name] = value
 
     def __getattr__(self, name):

Modified: Zope3/trunk/src/zope/schema/tests/test_fieldproperty.py
===================================================================
--- Zope3/trunk/src/zope/schema/tests/test_fieldproperty.py	2005-12-07 21:49:15 UTC (rev 40639)
+++ Zope3/trunk/src/zope/schema/tests/test_fieldproperty.py	2005-12-07 21:49:43 UTC (rev 40640)
@@ -28,16 +28,19 @@
     title = Text(description=u"Short summary", default=u'say something')
     weight = Float(min=0.0)
     code = Bytes(min_length=6, max_length=6, default='xxxxxx')
+    date = Float(title=u'Date', readonly=True)
 
+
 class C(object):
 
     title = FieldProperty(I['title'])
     weight = FieldProperty(I['weight'])
     code = FieldProperty(I['code'])
+    date = FieldProperty(I['date'])
 
 class Test(TestCase):
 
-    def test(self):
+    def test_basic(self):
         c = C()
         self.assertEqual(c.title, u'say something')
         self.assertEqual(c.weight, None)
@@ -58,6 +61,12 @@
         self.assertEqual(c.weight, 10)
         self.assertEqual(c.code, 'abcdef')
 
+    def test_readonly(self):
+        c = C()
+        # The date should be only settable once
+        c.date = 0.0
+        # Setting the value a second time should fail.
+        self.assertRaises(ValueError, setattr, c, 'date', 1.0)
 
 
 def test_suite():



More information about the Zope3-Checkins mailing list