[Checkins] SVN: z3c.form/trunk/ - `TextLinesConverter`: do not ignore newlines at the end of the inputted

Adam Groszer cvs-admin at zope.org
Sat Nov 24 11:06:44 UTC 2012


Log message for revision 128434:
  - `TextLinesConverter`: do not ignore newlines at the end of the inputted
    string, thus do not eat blank items
  - `TextLinesConverter`: toFieldValue, convert conversion exceptions to
    `FormatterValidationError`, for cases like got a string instead of int

Changed:
  U   z3c.form/trunk/CHANGES.txt
  U   z3c.form/trunk/src/z3c/form/converter.py
  U   z3c.form/trunk/src/z3c/form/converter.txt

-=-
Modified: z3c.form/trunk/CHANGES.txt
===================================================================
--- z3c.form/trunk/CHANGES.txt	2012-11-24 09:36:32 UTC (rev 128433)
+++ z3c.form/trunk/CHANGES.txt	2012-11-24 11:06:43 UTC (rev 128434)
@@ -20,6 +20,12 @@
   has been applied also to the existing translations (where
   applicable).
 
+- `TextLinesConverter`: do not ignore newlines at the end of the inputted
+  string, thus do not eat blank items
+
+- `TextLinesConverter`: toFieldValue, convert conversion exceptions to
+  `FormatterValidationError`, for cases like got a string instead of int
+
 2.9.0 (2012-09-17)
 ------------------
 

Modified: z3c.form/trunk/src/z3c/form/converter.py
===================================================================
--- z3c.form/trunk/src/z3c/form/converter.py	2012-11-24 09:36:32 UTC (rev 128433)
+++ z3c.form/trunk/src/z3c/form/converter.py	2012-11-24 11:06:43 UTC (rev 128434)
@@ -300,7 +300,7 @@
         for entry in value:
             try:
                 values.append(widget.terms.getTerm(entry).token)
-            except LookupError, err:
+            except LookupError:
                 # Swallow lookup errors, in case the options changed.
                 pass
         return values
@@ -339,7 +339,17 @@
         valueType = self.field.value_type._type
         if isinstance(valueType, tuple):
             valueType = valueType[0]
-        return collectionType(valueType(v) for v in value.splitlines())
+        # having a blank line at the end matters, one might want to have a blank
+        # entry at the end, resp. do not eat it once we have one
+        # splitlines ate that, so need to use split now
+        value = value.replace('\r\n', '\n')
+        items = []
+        for v in value.split('\n'):
+            try:
+                items.append(valueType(v))
+            except ValueError, err:
+                raise FormatterValidationError(str(err), v)
+        return collectionType(items)
 
 
 class MultiConverter(BaseDataConverter):

Modified: z3c.form/trunk/src/z3c/form/converter.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/converter.txt	2012-11-24 09:36:32 UTC (rev 128433)
+++ z3c.form/trunk/src/z3c/form/converter.txt	2012-11-24 11:06:43 UTC (rev 128434)
@@ -897,7 +897,12 @@
   >>> tlc.toWidgetValue([u'de', u'fr', u'en'])
   u'de\nfr\nen'
 
+Empty entries are significant:
 
+  >>> tlc.toWidgetValue([u'de', u'fr', u'en', u''])
+  u'de\nfr\nen\n'
+
+
 The result is always a string, since text lines widgets only deal with textarea
 as input field. Of course, we can convert the widget value back to an internal
 value:
@@ -910,6 +915,17 @@
   >>> tlc.toFieldValue('this morning\ntomorrow evening\nyesterday')
   [u'this morning', u'tomorrow evening', u'yesterday']
 
+Empty lines are significant:
+
+  >>> tlc.toFieldValue('de\n\nfr\nen')
+  [u'de', u'', u'fr', u'en']
+
+Empty lines are also significant at the end:
+
+  >>> tlc.toFieldValue('de\nfr\nen\n')
+  [u'de', u'fr', u'en', u'']
+
+
 An empty string will also cause the missing value to be returned:
 
   >>> tlc.toFieldValue('') is None
@@ -939,7 +955,7 @@
 
 And back:
 
-  >>> tlc.toFieldValue(u'1\n2\n3\n')
+  >>> tlc.toFieldValue(u'1\n2\n3')
   [1, 2, 3]
 
 An empty string will also cause the missing value to be returned:
@@ -973,10 +989,19 @@
 
 And back:
 
-  >>> tlc.toFieldValue(u'1\n2\n3\n')
+  >>> tlc.toFieldValue(u'1\n2\n3')
   (1, 2, 3)
 
+What if we have a wrong number:
 
+  >>> tlc.toFieldValue(u'1\n2\n3\nfoo')
+  Traceback (most recent call last):
+  ...
+  FormatterValidationError: ("invalid literal for int() with base 10: 'foo'", u'foo')
+
+
+
+
 Multi Data Converter
 --------------------
 



More information about the checkins mailing list