bit_length python 2.6 compat

This commit is contained in:
Andrew Noyes 2019-01-18 15:40:59 -08:00
parent e5c9d6f1ea
commit 5a6f82a75b
1 changed files with 11 additions and 2 deletions

View File

@ -24,6 +24,7 @@ import ctypes
import uuid
import struct
import math
import sys
from bisect import bisect_left
from fdb import six
@ -306,6 +307,14 @@ def _reduce_children(child_values):
return bytes_list, version_pos
if sys.version_info < (2, 7):
def _bit_length(x):
return len(bin(x)) - 2
else:
def _bit_length(x):
return x.bit_length()
def _encode(value, nested=False):
# returns [code][data] (code != 0xFF)
# encoded values are self-terminating
@ -324,7 +333,7 @@ def _encode(value, nested=False):
return b''.join([six.int2byte(INT_ZERO_CODE)]), -1
elif value > 0:
if value >= _size_limits[-1]:
length = (value.bit_length() + 7) // 8
length = (_bit_length(value) + 7) // 8
data = [six.int2byte(POS_INT_END), six.int2byte(length)]
for i in _range(length - 1, -1, -1):
data.append(six.int2byte((value >> (8 * i)) & 0xff))
@ -334,7 +343,7 @@ def _encode(value, nested=False):
return six.int2byte(INT_ZERO_CODE + n) + struct.pack(">Q", value)[-n:], -1
else:
if -value >= _size_limits[-1]:
length = (value.bit_length() + 7) // 8
length = (_bit_length(value) + 7) // 8
value += (1 << (length * 8)) - 1
data = [six.int2byte(NEG_INT_START), six.int2byte(length ^ 0xff)]
for i in _range(length - 1, -1, -1):