@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):
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:
# 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
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
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):