Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stricter checking for what is allowed as a Mapping object. #96

Merged
merged 1 commit into from Mar 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion colander/__init__.py
Expand Up @@ -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}',
Expand Down
17 changes: 17 additions & 0 deletions colander/tests/test_colander.py
Expand Up @@ -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)
Expand Down