[Checkins] SVN: zope.schema/trunk/ Don't fail trying to validate default value for Choice fields with IContextSourceBinder object given as a source.

Dan Korostelev nadako at gmail.com
Tue Mar 10 07:44:32 EDT 2009


Log message for revision 97798:
  Don't fail trying to validate default value for Choice fields with IContextSourceBinder object given as a source.
  
  Also, change encoding of _field.py to utf-8.
  

Changed:
  U   zope.schema/trunk/CHANGES.txt
  U   zope.schema/trunk/src/zope/schema/_field.py
  U   zope.schema/trunk/src/zope/schema/sources.txt

-=-
Modified: zope.schema/trunk/CHANGES.txt
===================================================================
--- zope.schema/trunk/CHANGES.txt	2009-03-10 11:28:13 UTC (rev 97797)
+++ zope.schema/trunk/CHANGES.txt	2009-03-10 11:44:32 UTC (rev 97798)
@@ -5,6 +5,10 @@
 3.5.4 (unreleased)
 ------------------
 
+- Don't fail trying to validate default value for Choice fields with
+  IContextSourceBinder object given as a source. See
+  https://bugs.launchpad.net/zope3/+bug/340416. 
+
 - Add an interface for ``DottedName`` field.
 
 - Add ``vocabularyName`` attribute to the ``IChoice`` interface, change

Modified: zope.schema/trunk/src/zope/schema/_field.py
===================================================================
--- zope.schema/trunk/src/zope/schema/_field.py	2009-03-10 11:28:13 UTC (rev 97797)
+++ zope.schema/trunk/src/zope/schema/_field.py	2009-03-10 11:44:32 UTC (rev 97798)
@@ -1,4 +1,4 @@
-# -*- coding: ISO-8859-1 -*-
+# -*- coding: utf-8 -*-
 ##############################################################################
 # Copyright (c) 2002 Zope Corporation and Contributors.
 # All Rights Reserved.
@@ -119,7 +119,7 @@
         >>> alphanumeric = "Bob\'s my 23rd uncle"
         >>> ascii._validate(alphanumeric)
 
-        >>> umlauts = "Köhlerstraße"
+        >>> umlauts = "Köhlerstraße"
         >>> ascii._validate(umlauts)
         Traceback (most recent call last):
         ...
@@ -263,7 +263,8 @@
         # initialized. Therefore signal the validation method to ignore
         # default value checks during initialization of a Choice tied to a
         # registered vocabulary.
-        self._init_field = bool(self.vocabularyName)
+        self._init_field = bool(self.vocabularyName) or \
+                           IContextSourceBinder.providedBy(self.vocabulary)
         super(Choice, self).__init__(**kw)
         self._init_field = False
 

Modified: zope.schema/trunk/src/zope/schema/sources.txt
===================================================================
--- zope.schema/trunk/src/zope/schema/sources.txt	2009-03-10 11:28:13 UTC (rev 97797)
+++ zope.schema/trunk/src/zope/schema/sources.txt	2009-03-10 11:44:32 UTC (rev 97798)
@@ -77,3 +77,21 @@
     <...MySource...>
     >>> bound.vocabulary.divisor
     3
+
+When using IContextSourceBinder together with default value, it's
+impossible to validate it on field initialization. Let's check if
+initalization doesn't fail in that case.
+
+    >>> choice = Choice(__name__='number', source=my_binder, default=2)
+
+    >>> bound = choice.bind(Context())
+    Binder was called.
+
+    >>> bound.validate(bound.default)
+    >>> bound.validate(3)
+    Traceback (most recent call last):
+    ...
+    ConstraintNotSatisfied: 3
+
+It's developer's responsibility to provide a default value that fits the
+constraints when using context-based sources.



More information about the Checkins mailing list