raise an exception if a number cannot be encoded as vint

This commit is contained in:
Alan Boudreault
2017-05-09 17:12:52 -04:00
parent 941dab462b
commit 7a29379312

View File

@@ -127,8 +127,8 @@ def vints_unpack(term): # noqa
def vints_pack(values):
revbytes = bytearray()
values = [int(v) for v in values[::-1]]
for v in values:
v = encode_zig_zag(v)
for value in values:
v = encode_zig_zag(value)
if v < 128:
revbytes.append(v)
else:
@@ -145,6 +145,9 @@ def vints_pack(values):
revbytes.append(v & 0xff)
v >>= 8
if num_extra_bytes > 8:
raise ValueError('Value %d is too big and cannot be encoded as vint' % value)
# We can now store the last bits in the first byte
n = 8 - num_extra_bytes
v |= (0xff >> n << n)