Leandro Lucarella
2008-10-15 16:20:20 UTC
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(encoding='ascii', encoding_error='ignore').to_python(u'\xe1')
+ ''
...
@@ -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),
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
--C7zPtVaVf+AK4Oqc--
1.5.6.5
--C7zPtVaVf+AK4Oqc--