Permalink
Browse files

More strict checking for what is allowed as a Mapping object.

  • Loading branch information...
1 parent ff6d8c2 commit 7ccdf501940d76519355ddfdb49599c024e79b28 @jayd3e jayd3e committed Mar 19, 2013
Showing with 21 additions and 1 deletion.
  1. +4 −1 colander/__init__.py
  2. +17 −0 colander/tests/test_colander.py
@@ -500,7 +500,10 @@ def _get_unknown(self):
def _validate(self, node, value):
try:
- return dict(value)
+ if hasattr(value, 'items'):
+ return dict(value)
+ else:
+ raise TypeError('Does not implement dict-like functionality.')
except Exception as e:
raise Invalid(node,
_('"${val}" is not a mapping type: ${err}',
@@ -527,10 +527,27 @@ def test_ctor_good_unknown(self):
def test_deserialize_not_a_mapping(self):
node = DummySchemaNode(None)
typ = self._makeOne()
+
+ # None
e = invalid_exc(typ.deserialize, node, None)
self.assertTrue(
e.msg.interpolate().startswith('"None" is not a mapping type'))
+ # list
+ e = invalid_exc(typ.deserialize, node, [])
+ self.assertTrue(
+ e.msg.interpolate().startswith('"[]" is not a mapping type'))
+
+ # str
+ e = invalid_exc(typ.deserialize, node, "")
+ self.assertTrue(
+ e.msg.interpolate().startswith('"" is not a mapping type'))
+
+ # tuple
+ e = invalid_exc(typ.deserialize, node, ())
+ self.assertTrue(
+ e.msg.interpolate().startswith('"()" is not a mapping type'))
+
def test_deserialize_null(self):
import colander
node = DummySchemaNode(None)

0 comments on commit 7ccdf50

Please sign in to comment.