From 30f002e946cd8290ec9de983ce9113252a495a7f Mon Sep 17 00:00:00 2001 From: Peter Lamut Date: Fri, 16 Aug 2013 15:07:43 +0200 Subject: [PATCH 1/2] String: raise Invalid on deserialization from a non-string (fixes #100) --- colander/__init__.py | 2 +- colander/tests/test_colander.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/colander/__init__.py b/colander/__init__.py index ddb3849..a3b4e4e 100644 --- a/colander/__init__.py +++ b/colander/__init__.py @@ -1121,7 +1121,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 63958de..d4d967b 100644 --- a/colander/tests/test_colander.py +++ b/colander/tests/test_colander.py @@ -1329,11 +1329,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') @@ -1351,6 +1351,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) From b6a882561d3dcdd8c2bbfbf20df389279c57f696 Mon Sep 17 00:00:00 2001 From: Peter Lamut Date: Fri, 16 Aug 2013 15:43:02 +0200 Subject: [PATCH 2/2] Update change log, add myself (plamut) to a list of contributors --- CHANGES.txt | 4 ++++ CONTRIBUTORS.txt | 1 + 2 files changed, 5 insertions(+) diff --git a/CHANGES.txt b/CHANGES.txt index b65f23d..0a41a12 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -20,6 +20,10 @@ Bug Fixes ``ParseErrorr`` in ``parse_date``, so that the validation machinery upstream handles it properly. +- ``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 83015d6..109752b 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -111,3 +111,4 @@ Contributors - Lorenzo M. Catucci, 2013/01/29 - Doug Latornell, 2013/03/17 - Clayton Parker, 2013/08/15 +- Peter Lamut, 2013/08/16