Re: [FE-discuss] String validator not caching encoding excep…

Top Page
Author: Leandro Lucarella
Date:  
To: formencode-discuss
Subject: Re: [FE-discuss] String validator not caching encoding exceptions
Ian Bicking, el 15 de octubre a las 10:38 me escribiste:
> > Should I send a patch?
>
> Sure, Invalid seems much more reasonable. Probably it should also be
> possible to do unicode_error='replace' to avoid any errors.


So, what do you think about this patches? I'm not sure if
_{to,from}_python is the right place for raising an Invalid, so comments
are welcome.

-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
A veces quisiera ser un auto,
para chocar como choco siendo humano,
para romperme en mil pedazos.
>From 09e43df08e4a73eee734f505eacf66d7aaa10090 Mon Sep 17 00:00:00 2001
From: Leandro Lucarella <llucarella@???>
Date: Wed, 15 Oct 2008 13:11:11 -0300
Subject: [PATCH 1/2] Raise Invalid if an encoding problem is detected in String validator

---
 formencode/validators.py |   34 +++++++++++++++++++++++++++-------
 1 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/formencode/validators.py b/formencode/validators.py
index 134b2f9..59baf92 100644
--- a/formencode/validators.py
+++ b/formencode/validators.py
@@ -1056,6 +1056,10 @@ class String(FancyValidator):
         Traceback (most recent call last):
             ...
         Invalid: Please enter a value
+ >>> String(encoding='ascii').to_python(u'j\xe1!')
+ Traceback (most recent call last):
+ ...
+ Invalid: Enter a value in ascii encoding

     """

@@ -1068,6 +1072,8 @@ class String(FancyValidator):
     messages = {
         'tooLong': _("Enter a value less than %(max)i characters long"),
         'tooShort': _("Enter a value %(min)i characters long or more"),
+ 'badEncoding' : _("Invalid data or incorrect encoding "
+ "(%(encoding)s expected)"),
         }

     def __initargs__(self, new_attrs):
@@ -1083,7 +1089,12 @@ class String(FancyValidator):
             except UnicodeEncodeError:
                 value = unicode(value)
         if self.encoding is not None and isinstance(value, unicode):
- value = value.encode(self.encoding)
+ try:
+ value = value.encode(self.encoding)
+ except UnicodeEncodeError:
+ raise Invalid(self.message('badEncoding', state,
+ encoding=self.encoding),
+ value, state)
         return value

     def _from_python(self, value, state):
@@ -1098,7 +1109,12 @@ class String(FancyValidator):
             except UnicodeEncodeError:
                 value = unicode(value)
         if self.encoding is not None and isinstance(value, unicode):
- value = value.encode(self.encoding)
+ try:
+ value = value.encode(self.encoding)
+ except UnicodeEncodeError:
+ raise Invalid(self.message('badEncoding', state,
+ encoding=self.encoding),
+ value, state)
         if self.strip:
             value = value.strip()
         return value
@@ -1139,9 +1155,6 @@ 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)
@@ -1162,7 +1175,9 @@ class UnicodeString(String):
         try:
             return unicode(value, self.inputEncoding)
         except UnicodeDecodeError:
- raise Invalid(self.message('badEncoding', state), value, state)
+ raise Invalid(self.message('badEncoding', state,
+ encoding=self.inputEncoding),
+ value, state)
         except TypeError:
             raise Invalid(self.message('badType', state, type=type(value), value=value), value, state)

@@ -1173,7 +1188,12 @@ class UnicodeString(String):
             else:
                 value = str(value)
         if isinstance(value, unicode):
- value = value.encode(self.outputEncoding)
+ try:
+ value = value.encode(self.outputEncoding)
+ except UnicodeEncodeError:
+ raise Invalid(self.message('badEncoding', state,
+ encoding=self.outputEncoding),
+ value, state)
         return value

     def empty_value(self, value):
-- 
1.5.6.5

>From 79c913fec7a510b7e8da23e8faf034d30d546173 Mon Sep 17 00:00:00 2001
From: Leandro Lucarella <llucarella@???>
Date: Wed, 15 Oct 2008 13:20:20 -0300
Subject: [PATCH 2/2] Add encoding_error option to String validator

Using this new option, you can tell String validator how to handle
encoding errors. Any Python error handling scheme can be used (for example
'strict', the default, 'replace', 'ignore').
---
 formencode/validators.py |   11 +++++++----
 1 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/formencode/validators.py b/formencode/validators.py
index 59baf92..04587b9 100644
--- a/formencode/validators.py
+++ b/formencode/validators.py
@@ -1048,6 +1048,8 @@ class String(FancyValidator):
         ''
         >>> String().to_python(None)
         ''
+ >>> String(encoding='ascii', encoding_error='ignore').to_python(u'\xe1')
+ ''
         >>> String(min=3).to_python(None)
         Traceback (most recent call last):
             ...
@@ -1067,6 +1069,7 @@ class String(FancyValidator):
     max = None
     not_empty = None
     encoding = None
+ encoding_error = 'strict'
     list_joiner = ', '

     messages = {
@@ -1090,7 +1093,7 @@ class String(FancyValidator):
                 value = unicode(value)
         if self.encoding is not None and isinstance(value, unicode):
             try:
- value = value.encode(self.encoding)
+ value = value.encode(self.encoding, self.encoding_error)
             except UnicodeEncodeError:
                 raise Invalid(self.message('badEncoding', state,
                                            encoding=self.encoding),
@@ -1110,7 +1113,7 @@ class String(FancyValidator):
                 value = unicode(value)
         if self.encoding is not None and isinstance(value, unicode):
             try:
- value = value.encode(self.encoding)
+ value = value.encode(self.encoding, self.encoding_error)
             except UnicodeEncodeError:
                 raise Invalid(self.message('badEncoding', state,
                                            encoding=self.encoding),
@@ -1173,7 +1176,7 @@ class UnicodeString(String):
             else:
                 value = str(value)
         try:
- return unicode(value, self.inputEncoding)
+ return unicode(value, self.inputEncoding, self.encoding_error)
         except UnicodeDecodeError:
             raise Invalid(self.message('badEncoding', state,
                                        encoding=self.inputEncoding),
@@ -1189,7 +1192,7 @@ class UnicodeString(String):
                 value = str(value)
         if isinstance(value, unicode):
             try:
- value = value.encode(self.outputEncoding)
+ value = value.encode(self.outputEncoding, self.encoding_error)
             except UnicodeEncodeError:
                 raise Invalid(self.message('badEncoding', state,
                                            encoding=self.outputEncoding),
-- 
1.5.6.5

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/_______________________________________________
FormEncode-discuss mailing list
FormEncode-discuss@???
https://lists.sourceforge.net/lists/listinfo/formencode-discuss