From e1e876212caa5b56d8ce6fef378a9b31bffd6e3d Mon Sep 17 00:00:00 2001 From: Tim Tisdall Date: Tue, 16 Jun 2015 20:12:41 +0000 Subject: [PATCH 1/4] fix String encoding fixes #233 --- colander/__init__.py | 5 ++++- colander/tests/test_colander.py | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/colander/__init__.py b/colander/__init__.py index d2a518b..2aa8995 100644 --- a/colander/__init__.py +++ b/colander/__init__.py @@ -1184,7 +1184,10 @@ class String(SchemaType): else: result = text_type(appstruct) else: - result = text_type(appstruct) + if self.encoding: + result = text_type(appstruct).encode(self.encoding) + else: + result = text_type(appstruct) return result except Exception as e: raise Invalid(node, diff --git a/colander/tests/test_colander.py b/colander/tests/test_colander.py index 6d05d71..644a04b 100644 --- a/colander/tests/test_colander.py +++ b/colander/tests/test_colander.py @@ -1557,6 +1557,13 @@ class TestString(unittest.TestCase): e = invalid_exc(typ.serialize, node, not_utf8) self.assertTrue('cannot be serialized' in e.msg) + def test_serialize_encoding_with_non_string_type(self): + utf8 = '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): def _makeOne(self): from colander import Integer From be63b5533cdde8fd3e825779bac688663857b0a0 Mon Sep 17 00:00:00 2001 From: Tim Tisdall Date: Mon, 22 Jun 2015 14:28:12 +0000 Subject: [PATCH 2/4] pulled `text_type()` out of `if` statement --- colander/__init__.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/colander/__init__.py b/colander/__init__.py index 2aa8995..d514ca2 100644 --- a/colander/__init__.py +++ b/colander/__init__.py @@ -1184,10 +1184,9 @@ class String(SchemaType): else: result = text_type(appstruct) else: + result = text_type(appstruct) if self.encoding: - result = text_type(appstruct).encode(self.encoding) - else: - result = text_type(appstruct) + result = result.encode(self.encoding) return result except Exception as e: raise Invalid(node, From a941784b5ad168597fb352ee2b241281f1f83d26 Mon Sep 17 00:00:00 2001 From: Tim Tisdall Date: Tue, 23 Jun 2015 13:29:24 +0000 Subject: [PATCH 3/4] be more explicit about string type --- colander/tests/test_colander.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/colander/tests/test_colander.py b/colander/tests/test_colander.py index 644a04b..0dd99aa 100644 --- a/colander/tests/test_colander.py +++ b/colander/tests/test_colander.py @@ -1558,7 +1558,7 @@ class TestString(unittest.TestCase): self.assertTrue('cannot be serialized' in e.msg) def test_serialize_encoding_with_non_string_type(self): - utf8 = '123'.encode('utf-8') + utf8 = text_type('123').encode('utf-8') node = DummySchemaNode(None) typ = self._makeOne('utf-8') result = typ.serialize(node, 123) From 7b8e44b78c7b2fca3635f6044d04bc20e2f80f0d Mon Sep 17 00:00:00 2001 From: Tim Tisdall Date: Tue, 23 Jun 2015 17:10:13 +0000 Subject: [PATCH 4/4] noted string encoding fix in CHANGES --- CHANGES.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index c7aaf66..57d74ec 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -27,6 +27,9 @@ Bug Fixes - Fix an issue where the ``flatten()`` method produces an invalid name (ex: "answer.0.") for the type "Sequence". See 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 `_ 1.0 (2014-11-26)