]> arthur.barton.de Git - bup.git/commitdiff
vint: fix bytes for Python 3
authorRob Browning <rlb@defaultvalue.org>
Sun, 6 Oct 2019 15:54:41 +0000 (10:54 -0500)
committerRob Browning <rlb@defaultvalue.org>
Sun, 13 Oct 2019 17:27:49 +0000 (12:27 -0500)
Signed-off-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/compat.py
lib/bup/t/tvint.py
lib/bup/vint.py

index e598853302d94a5f660388b737d6f15ccf09f275..53f52869e2c2d9dbd8d3b7987b4e202d3b3bae20 100644 (file)
@@ -26,6 +26,9 @@ if py3:
     def items(x):
         return x.items()
 
+    def bytes_from_uint(i):
+        return bytes((i,))
+
 else:  # Python 2
 
     from pipes import quote
@@ -69,6 +72,9 @@ else:  # Python 2
     def items(x):
         return x.iteritems()
 
+    def bytes_from_uint(i):
+        return chr(i)
+
 
 def wrap_main(main):
     """Run main() and raise a SystemExit with the return value if it
index 51cd91ee63852034ce36655d1687b45e49162f52..0cb8d732b90446c355c907655b49ca673b1c3b4e 100644 (file)
@@ -49,18 +49,18 @@ def encode_and_decode_bvec(x):
 @wvtest
 def test_bvec():
     with no_lingering_errors():
-        values = ('', 'x', 'foo', '\0', '\0foo', 'foo\0bar\0')
+        values = (b'', b'x', b'foo', b'\0', b'\0foo', b'foo\0bar\0')
         for x in values:
             WVPASSEQ(encode_and_decode_bvec(x), x)
         WVEXCEPT(EOFError, vint.read_bvec, BytesIO())
         outf = BytesIO()
-        for x in ('foo', 'bar', 'baz', 'bax'):
+        for x in (b'foo', b'bar', b'baz', b'bax'):
             vint.write_bvec(outf, x)
         inf = BytesIO(outf.getvalue())
-        WVPASSEQ(vint.read_bvec(inf), 'foo')
-        WVPASSEQ(vint.read_bvec(inf), 'bar')
+        WVPASSEQ(vint.read_bvec(inf), b'foo')
+        WVPASSEQ(vint.read_bvec(inf), b'bar')
         vint.skip_bvec(inf)
-        WVPASSEQ(vint.read_bvec(inf), 'bax')
+        WVPASSEQ(vint.read_bvec(inf), b'bax')
 
 
 def pack_and_unpack(types, *values):
@@ -72,16 +72,16 @@ def pack_and_unpack(types, *values):
 def test_pack_and_unpack():
     with no_lingering_errors():
         tests = [('', []),
-                 ('s', ['foo']),
-                 ('ss', ['foo', 'bar']),
-                 ('sV', ['foo', 0]),
-                 ('sv', ['foo', -1]),
+                 ('s', [b'foo']),
+                 ('ss', [b'foo', b'bar']),
+                 ('sV', [b'foo', 0]),
+                 ('sv', [b'foo', -1]),
                  ('V', [0]),
-                 ('Vs', [0, 'foo']),
+                 ('Vs', [0, b'foo']),
                  ('VV', [0, 1]),
                  ('Vv', [0, -1]),
                  ('v', [0]),
-                 ('vs', [0, 'foo']),
+                 ('vs', [0, b'foo']),
                  ('vV', [0, 1]),
                  ('vv', [0, -1])]
         for test in tests:
index e0e33804c395437f769c992ca7cbac72b0c8ca2c..cb7225d88e3ddf801c5ce727b3f60560892ae804 100644 (file)
@@ -5,24 +5,30 @@
 # This code is covered under the terms of the GNU Library General
 # Public License as described in the bup LICENSE file.
 
+# Variable length integers are encoded as vints -- see lucene.
+
 from __future__ import absolute_import
 from io import BytesIO
+import sys
+
+from bup import compat
 
-# Variable length integers are encoded as vints -- see jakarta lucene.
 
 def write_vuint(port, x):
+    write = port.write
+    bytes_from_uint = compat.bytes_from_uint
     if x < 0:
         raise Exception("vuints must not be negative")
     elif x == 0:
-        port.write('\0')
+        write(bytes_from_uint(0))
     else:
         while True:
             seven_bits = x & 0x7f
             x >>= 7
             if x:
-                port.write(chr(0x80 | seven_bits))
+                write(bytes_from_uint(0x80 | seven_bits))
             else:
-                port.write(chr(seven_bits))
+                write(bytes_from_uint(seven_bits))
                 break
 
 
@@ -51,8 +57,10 @@ def read_vuint(port):
 def write_vint(port, x):
     # Sign is handled with the second bit of the first byte.  All else
     # matches vuint.
+    write = port.write
+    bytes_from_uint = compat.bytes_from_uint
     if x == 0:
-        port.write('\0')
+        write(bytes_from_uint(0))
     else:
         if x < 0:
             x = -x
@@ -61,10 +69,10 @@ def write_vint(port, x):
             sign_and_six_bits = x & 0x3f
         x >>= 6
         if x:
-            port.write(chr(0x80 | sign_and_six_bits))
+            write(bytes_from_uint(0x80 | sign_and_six_bits))
             write_vuint(port, x)
         else:
-            port.write(chr(sign_and_six_bits))
+            write(bytes_from_uint(sign_and_six_bits))
 
 
 def read_vint(port):