summaryrefslogtreecommitdiff
path: root/kafka/util.py
diff options
context:
space:
mode:
authorBruno ReniƩ <brutasse@gmail.com>2014-08-28 20:50:20 +0200
committerMark Roberts <wizzat@fb.com>2014-09-03 09:55:44 -0700
commitcf0b7f0530e765f2cf710bd35daf53bb4ea205d2 (patch)
treef38669f145112a7f301691f311b3c8aca51d1f59 /kafka/util.py
parent83af5102e995e854a1980b90f1400afdd098da37 (diff)
downloadkafka-python-cf0b7f0530e765f2cf710bd35daf53bb4ea205d2.tar.gz
Make all unit tests pass on py3.3/3.4
Diffstat (limited to 'kafka/util.py')
-rw-r--r--kafka/util.py26
1 files changed, 21 insertions, 5 deletions
diff --git a/kafka/util.py b/kafka/util.py
index 9121374..a4a0174 100644
--- a/kafka/util.py
+++ b/kafka/util.py
@@ -1,14 +1,30 @@
import collections
import struct
import sys
+import zlib
from threading import Thread, Event
+import six
+
from kafka.common import BufferUnderflowError
+def crc32(data):
+ """
+ Python 2 returns a value in the range [-2**31, 2**31-1].
+ Python 3 returns a value in the range [0, 2**32-1].
+
+ We want a consistent behavior so let's use python2's.
+ """
+ crc = zlib.crc32(data)
+ if six.PY3 and crc > 2**31:
+ crc -= 2 ** 32
+ return crc
+
+
def write_int_string(s):
- if s is not None and not isinstance(s, str):
- raise TypeError('Expected "%s" to be str\n'
+ if s is not None and not isinstance(s, six.binary_type):
+ raise TypeError('Expected "%s" to be bytes\n'
'data=%s' % (type(s), repr(s)))
if s is None:
return struct.pack('>i', -1)
@@ -17,12 +33,12 @@ def write_int_string(s):
def write_short_string(s):
- if s is not None and not isinstance(s, str):
- raise TypeError('Expected "%s" to be str\n'
+ if s is not None and not isinstance(s, six.binary_type):
+ raise TypeError('Expected "%s" to be bytes\n'
'data=%s' % (type(s), repr(s)))
if s is None:
return struct.pack('>h', -1)
- elif len(s) > 32767 and sys.version < (2, 7):
+ elif len(s) > 32767 and sys.version_info < (2, 7):
# Python 2.6 issues a deprecation warning instead of a struct error
raise struct.error(len(s))
else: