zlib.crc32 behaves inconsistently on py2 vs py3
authorHelmut Grohne <helmut@subdivi.de>
Thu, 16 Apr 2015 15:46:07 +0000 (17:46 +0200)
committerHelmut Grohne <helmut@subdivi.de>
Thu, 16 Apr 2015 15:46:07 +0000 (17:46 +0200)
zlib.crc32 returns a int32_t on py2 and a uint32_t on py3.

dedup/compression.py

index 4385f1d..cc65bbf 100644 (file)
@@ -1,6 +1,9 @@
 import struct
+import sys
 import zlib
 
+crc32_type = "L" if sys.version_info.major >= 3 else "l"
+
 class GzipDecompressor(object):
     """An interface to gzip which is similar to bz2.BZ2Decompressor and
     lzma.LZMADecompressor."""
@@ -60,7 +63,7 @@ class GzipDecompressor(object):
         elif not self.sawheader:
             return self.inbuffer
         else:
-            expect = struct.pack("<ll", self.crc, self.size)
+            expect = struct.pack("<" + crc32_type + "L", self.crc, self.size)
             if self.inbuffer.startswith(expect) and \
                     self.inbuffer[len(expect):].replace(b"\0", b"") == b"":
                 return b""