- Fix bug in serialization of non-Unicode values in the `String` class.
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
Changes
|
||||
=======
|
||||
|
||||
Next release
|
||||
------------
|
||||
|
||||
- Fix bug in serialization of non-Unicode values in the ``String`` class.
|
||||
|
||||
0.5.1 (2010-04-02)
|
||||
------------------
|
||||
|
||||
|
||||
@@ -525,6 +525,11 @@ class String(Type):
|
||||
which should be applied to object serialization. It defaults to
|
||||
``utf-8`` if not provided.
|
||||
|
||||
If a string (as opposed to a unicode object) is provided as a
|
||||
value to either the serialize or deserialize method of this type,
|
||||
it must be encoded with the type's encoding; an
|
||||
:exc:`colander.Invalid` error will result if not.
|
||||
|
||||
The subnodes of the :class:`colander.SchemaNode` that wraps
|
||||
this type are ignored.
|
||||
"""
|
||||
@@ -545,10 +550,16 @@ class String(Type):
|
||||
|
||||
def serialize(self, node, value):
|
||||
try:
|
||||
return unicode(value).encode(self.encoding or default_encoding)
|
||||
encoding = self.encoding or default_encoding
|
||||
if isinstance(value, unicode):
|
||||
result = value.encode(encoding)
|
||||
else:
|
||||
# do validation here
|
||||
result = unicode(value, encoding).encode(encoding)
|
||||
return result
|
||||
except Exception, e:
|
||||
raise Invalid(node,
|
||||
'%r is cannot be serialized to str: %s' % (value, e))
|
||||
'%r cannot be serialized to str: %s' % (value, e))
|
||||
|
||||
Str = String
|
||||
|
||||
|
||||
@@ -703,6 +703,14 @@ class TestString(unittest.TestCase):
|
||||
result = typ.serialize(node, uni)
|
||||
self.assertEqual(result, utf16)
|
||||
|
||||
def test_serialize_string_with_high_unresolveable_high_order_chars(self):
|
||||
not_utf8 = '\xff\xfe\xf8\x00'
|
||||
node = DummySchemaNode(None)
|
||||
typ = self._makeOne('utf-8')
|
||||
e = invalid_exc(typ.serialize, node, not_utf8)
|
||||
self.failUnless('cannot be serialized to str' in e.msg)
|
||||
|
||||
|
||||
class TestInteger(unittest.TestCase):
|
||||
def _makeOne(self):
|
||||
from colander import Integer
|
||||
|
||||
Reference in New Issue
Block a user