]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/vint.py
Add compat.items() and use it
[bup.git] / lib / bup / vint.py
index 8f7f58a6698ddc22a3d9a3959190c323d03e69dd..378234c62d2a2a5ec1f2ed9b62598a132e8dd07d 100644 (file)
@@ -5,7 +5,7 @@
 # This code is covered under the terms of the GNU Library General
 # Public License as described in the bup LICENSE file.
 
-from cStringIO import StringIO
+from io import BytesIO
 
 # Variable length integers are encoded as vints -- see jakarta lucene.
 
@@ -77,8 +77,10 @@ def read_vint(port):
         if b & 0x80:
             offset += 6
             c = port.read(1)
+        elif negative:
+            return -result
         else:
-            return -result if negative else result
+            return result
     while c:
         b = ord(c)
         if b & 0x80:
@@ -88,7 +90,10 @@ def read_vint(port):
         else:
             result |= (b << offset)
             break
-    return -result if negative else result
+    if negative:
+        return -result
+    else:
+        return result
 
 
 def write_bvec(port, x):
@@ -108,7 +113,7 @@ def skip_bvec(port):
 def pack(types, *args):
     if len(types) != len(args):
         raise Exception('number of arguments does not match format string')
-    port = StringIO()
+    port = BytesIO()
     for (type, value) in zip(types, args):
         if type == 'V':
             write_vuint(port, value)
@@ -123,7 +128,7 @@ def pack(types, *args):
 
 def unpack(types, data):
     result = []
-    port = StringIO(data)
+    port = BytesIO(data)
     for type in types:
         if type == 'V':
             result.append(read_vuint(port))