diff --git a/CHANGES.txt b/CHANGES.txt index c755310..12186ef 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -27,6 +27,10 @@ Bug Fixes when the default_timezone is set. These previously had the default timezone. +- ``colander.String`` schema type now raises ``colander.Invalid`` when trying + to deserialize a non-string item. + See https://github.com/Pylons/colander/issues/100 + Features ~~~~~~~~ diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 120ccfb..afe9392 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -112,3 +112,4 @@ Contributors - Doug Latornell, 2013/03/17 - Clayton Parker, 2013/08/15 - Brian Sutherland, 2013/08/16 +- Peter Lamut, 2013/08/16 diff --git a/colander/__init__.py b/colander/__init__.py index c5ef5cb..7051113 100644 --- a/colander/__init__.py +++ b/colander/__init__.py @@ -1131,7 +1131,7 @@ class String(SchemaType): else: result = text_type(cstruct) else: - result = text_type(cstruct) + raise Invalid(node) except Exception as e: raise Invalid(node, _('${val} is not a string: ${err}', diff --git a/colander/tests/test_colander.py b/colander/tests/test_colander.py index 9915c25..0a3ca4d 100644 --- a/colander/tests/test_colander.py +++ b/colander/tests/test_colander.py @@ -1350,11 +1350,11 @@ class TestString(unittest.TestCase): self.assertEqual(result, uni) def test_deserialize_nonunicode_from_None(self): + import colander value = object() node = DummySchemaNode(None) typ = self._makeOne() - result = typ.deserialize(node, value) - self.assertEqual(result, text_type(value)) + self.assertRaises(colander.Invalid, typ.deserialize, node, value) def test_deserialize_from_utf8(self): uni = text_(b'\xe3\x81\x82', encoding='utf-8') @@ -1372,6 +1372,13 @@ class TestString(unittest.TestCase): result = typ.deserialize(node, utf16) self.assertEqual(result, uni) + def test_deserialize_from_nonstring_obj(self): + import colander + value = object() + node = DummySchemaNode(None) + typ = self._makeOne() + self.assertRaises(colander.Invalid, typ.deserialize, node, value) + def test_serialize_null(self): from colander import null node = DummySchemaNode(None)