summaryrefslogtreecommitdiff
path: root/kafka/partitioner/hashed.py
diff options
context:
space:
mode:
authorDana Powers <dana.powers@gmail.com>2016-10-22 09:48:08 -0700
committerGitHub <noreply@github.com>2016-10-22 09:48:08 -0700
commit8de40a20d909c90745b39df09b3aa9d2cc194b68 (patch)
tree85914fcaaccc700b3ddce7eb1f9ff143dc40285a /kafka/partitioner/hashed.py
parent9450a6bfff8517371162a968f4345ffc09380bb8 (diff)
downloadkafka-python-8de40a20d909c90745b39df09b3aa9d2cc194b68.tar.gz
Fix murmur2 bug handling python2 bytes that do not ascii encode (#815)
* Add test for murmur2 py2 bytes bug * Fix murmur2 handling of python2 bytes * Drop bytearray / str / unicode MurmurPartitioner tests -- no longer supported * Make DefaultPartitioner importable from kafka.partitioner
Diffstat (limited to 'kafka/partitioner/hashed.py')
-rw-r--r--kafka/partitioner/hashed.py16
1 files changed, 7 insertions, 9 deletions
diff --git a/kafka/partitioner/hashed.py b/kafka/partitioner/hashed.py
index b6b8f7f..06307f0 100644
--- a/kafka/partitioner/hashed.py
+++ b/kafka/partitioner/hashed.py
@@ -49,22 +49,20 @@ HashedPartitioner = LegacyPartitioner
# https://github.com/apache/kafka/blob/0.8.2/clients/src/main/java/org/apache/kafka/common/utils/Utils.java#L244
-def murmur2(key):
+def murmur2(data):
"""Pure-python Murmur2 implementation.
Based on java client, see org.apache.kafka.common.utils.Utils.murmur2
Args:
- key: if not a bytes type, encoded using default encoding
+ data (bytes): opaque bytes
- Returns: MurmurHash2 of key bytearray
+ Returns: MurmurHash2 of data
"""
-
- # Convert key to bytes or bytearray
- if isinstance(key, bytearray) or (six.PY3 and isinstance(key, bytes)):
- data = key
- else:
- data = bytearray(str(key).encode())
+ # Python2 bytes is really a str, causing the bitwise operations below to fail
+ # so convert to bytearray.
+ if six.PY2:
+ data = bytearray(bytes(data))
length = len(data)
seed = 0x9747b28c