diff options
-rw-r--r-- | kafka/util.py | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/kafka/util.py b/kafka/util.py index de8f228..385fd56 100644 --- a/kafka/util.py +++ b/kafka/util.py @@ -12,14 +12,21 @@ from kafka.vendor import six from kafka.errors import BufferUnderflowError -def crc32(data): - crc = binascii.crc32(data) - # py2 and py3 behave a little differently - # CRC is encoded as a signed int in kafka protocol - # so we'll convert the py3 unsigned result to signed - if six.PY3 and crc >= 2**31: - crc -= 2**32 - return crc +if six.PY3: + MAX_INT = 2 ** 31 + TO_SIGNED = 2 ** 32 + + def crc32(data): + crc = binascii.crc32(data) + # py2 and py3 behave a little differently + # CRC is encoded as a signed int in kafka protocol + # so we'll convert the py3 unsigned result to signed + if crc >= MAX_INT: + crc -= TO_SIGNED + return crc +else: + def crc32(data): + return binascii.crc32(data) def write_int_string(s): |