raise an exception if a number cannot be encoded as vint
This commit is contained in:
@@ -127,8 +127,8 @@ def vints_unpack(term): # noqa
|
|||||||
def vints_pack(values):
|
def vints_pack(values):
|
||||||
revbytes = bytearray()
|
revbytes = bytearray()
|
||||||
values = [int(v) for v in values[::-1]]
|
values = [int(v) for v in values[::-1]]
|
||||||
for v in values:
|
for value in values:
|
||||||
v = encode_zig_zag(v)
|
v = encode_zig_zag(value)
|
||||||
if v < 128:
|
if v < 128:
|
||||||
revbytes.append(v)
|
revbytes.append(v)
|
||||||
else:
|
else:
|
||||||
@@ -145,6 +145,9 @@ def vints_pack(values):
|
|||||||
revbytes.append(v & 0xff)
|
revbytes.append(v & 0xff)
|
||||||
v >>= 8
|
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
|
# We can now store the last bits in the first byte
|
||||||
n = 8 - num_extra_bytes
|
n = 8 - num_extra_bytes
|
||||||
v |= (0xff >> n << n)
|
v |= (0xff >> n << n)
|
||||||
|
Reference in New Issue
Block a user