Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cassandra/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,13 @@ def _insert_unchecked(self, key, flat_key, value):
self._index[flat_key] = len(self._items) - 1

def _serialize_key(self, key):
return self.cass_key_type.serialize(key, self.protocol_version)
try:
return self.cass_key_type.serialize(key, self.protocol_version)
except Exception:
# A key that cannot be serialized with the map's key type cannot
# be present, so treat it as missing to keep Mapping semantics
# (get() returns the default, `in` returns False).
raise KeyError(str(key)) from None


@total_ordering
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_orderedmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,16 @@ def test_normalized_lookup(self):
assert om[{'one': 1}] is om[{u'one': 1}]
assert om[{'two': 2}] is om[{u'two': 2}]
assert om[{'one': 1}] is not om[{'two': 2}]

def test_unserializable_key_treated_as_missing(self):
# a key that cannot be serialized with the map's key type cannot be
# present, so lookups behave like a plain dict instead of leaking the
# serializer's exception
om = OrderedMapSerializedKey(UTF8Type, 3)
om._insert_unchecked('one', UTF8Type.serialize('one', 3), 1)

assert om.get(None) is None
assert om.get(None, 2) == 2
assert None not in om
with pytest.raises(KeyError):
om[None]