Merge pull request #235 from tisdall/string_encoding

fix String encoding
This commit is contained in:
Tres Seaver
2015-06-23 16:29:07 -04:00
3 changed files with 12 additions and 0 deletions

View File

@@ -27,6 +27,9 @@ Bug Fixes
- Fix an issue where the ``flatten()`` method produces an invalid name - Fix an issue where the ``flatten()`` method produces an invalid name
(ex: "answer.0.") for the type "Sequence". See (ex: "answer.0.") for the type "Sequence". See
https://github.com/Pylons/colander/issues/179 https://github.com/Pylons/colander/issues/179
- Fixed issue with ``String`` not being properly encoded when non-string
values were passed into ``serialize()``
See `#235 <https://github.com/Pylons/colander/pull/235>`_
1.0 (2014-11-26) 1.0 (2014-11-26)

View File

@@ -1185,6 +1185,8 @@ class String(SchemaType):
result = text_type(appstruct) result = text_type(appstruct)
else: else:
result = text_type(appstruct) result = text_type(appstruct)
if self.encoding:
result = result.encode(self.encoding)
return result return result
except Exception as e: except Exception as e:
raise Invalid(node, raise Invalid(node,

View File

@@ -1557,6 +1557,13 @@ class TestString(unittest.TestCase):
e = invalid_exc(typ.serialize, node, not_utf8) e = invalid_exc(typ.serialize, node, not_utf8)
self.assertTrue('cannot be serialized' in e.msg) self.assertTrue('cannot be serialized' in e.msg)
def test_serialize_encoding_with_non_string_type(self):
utf8 = text_type('123').encode('utf-8')
node = DummySchemaNode(None)
typ = self._makeOne('utf-8')
result = typ.serialize(node, 123)
self.assertEqual(result, utf8)
class TestInteger(unittest.TestCase): class TestInteger(unittest.TestCase):
def _makeOne(self): def _makeOne(self):
from colander import Integer from colander import Integer