Diff of /branches/1.0/turbogears/validators.py [r1516] .. [r1517] Maximize Restore

  Switch to side-by-side view

--- a/branches/1.0/turbogears/validators.py
+++ b/branches/1.0/turbogears/validators.py
@@ -104,24 +104,34 @@
         else:
             return value.strftime(self.format)
 
-# formencode trunk already contains UnicodeString but we
-# provide our own version because formencode's version may be broken
-# remove this when formencode.validators.UnicodeString will be identical to ours.
+# formencode trunk contains UnicodeString implementation 
+# but it is different from ours and was broken at the time.
+# remove this impl. when formencode.validators.UnicodeString will be identical to ours.
 class UnicodeString(String):
     encoding = 'utf-8'
+    messages = {
+        'badEncoding' : "Invalid data or incorrect encoding",
+    }
+    def __init__(self, inputEncoding=None, outputEncoding=None, **kw):
+        String.__init__(self, **kw)
+        self.inputEncoding = inputEncoding or self.encoding
+        self.outputEncoding = outputEncoding or self.encoding
     def _to_python(self, value, state):
         if value:
             if isinstance(value, unicode):
                 return value
             if hasattr(value, '__unicode__'):
                 return unicode(value)
-            return unicode(value, self.encoding)
+            try:
+                return unicode(value, self.inputEncoding)
+            except UnicodeDecodeError:
+                raise Invalid(self.message('badEncoding', state), value, state)
         return u''
     def _from_python(self, value, state):
         if hasattr(value, '__unicode__'):
             value = unicode(value)
         if isinstance(value, unicode):
-            return value.encode(self.encoding)
+            return value.encode(self.outputEncoding)
         return str(value)
 
 # another formencode workaround,