Skip to content

Commit be23042

Browse files
author
Mark Roberts
committed
Update kafka.util.crc32 to unsigned everywhere
1 parent 84a7add commit be23042

File tree

2 files changed

+4
-13
lines changed

2 files changed

+4
-13
lines changed

kafka/protocol.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def _encode_message(cls, message):
9898
msg += write_int_string(message.key)
9999
msg += write_int_string(message.value)
100100
crc = crc32(msg)
101-
msg = struct.pack('>i%ds' % len(msg), crc, msg)
101+
msg = struct.pack('>I%ds' % len(msg), crc, msg)
102102
else:
103103
raise ProtocolError("Unexpected magic number: %d" % message.magic)
104104
return msg
@@ -148,7 +148,7 @@ def _decode_message(cls, data, offset):
148148
The offset is actually read from decode_message_set_iter (it is part
149149
of the MessageSet payload).
150150
"""
151-
((crc, magic, att), cur) = relative_unpack('>iBB', data, 0)
151+
((crc, magic, att), cur) = relative_unpack('>IBB', data, 0)
152152
if crc != crc32(data[4:]):
153153
raise ChecksumError("Message checksum failed")
154154

kafka/util.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import binascii
12
import collections
23
import struct
34
import sys
4-
import zlib
55
from threading import Thread, Event
66

77
import six
@@ -10,16 +10,7 @@
1010

1111

1212
def crc32(data):
13-
"""
14-
Python 2 returns a value in the range [-2**31, 2**31-1].
15-
Python 3 returns a value in the range [0, 2**32-1].
16-
17-
We want a consistent behavior so let's use python2's.
18-
"""
19-
crc = zlib.crc32(data)
20-
if six.PY3 and crc > 2**31:
21-
crc -= 2 ** 32
22-
return crc
13+
return binascii.crc32(data) & 0xffffffff
2314

2415

2516
def write_int_string(s):

0 commit comments

Comments
 (0)