element access on bytes yields int in py3k
[~helmut/debian-dedup.git] / dedup / compression.py
index 4385f1d..4fd7320 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."""
@@ -32,7 +35,7 @@ class GzipDecompressor(object):
                 return b""
             if not self.inbuffer.startswith(b"\037\213\010"):
                 raise ValueError("gzip magic not found")
-            flag = ord(self.inbuffer[3])
+            flag = ord(self.inbuffer[3:4])
             if flag & 4:
                 if len(self.inbuffer) < skip + 2:
                     return b""
@@ -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""