avoid deep recursion in bdecode3, brings performance closer to py2 version

This commit is contained in:
j 2016-08-30 10:16:18 +02:00
parent 5168459936
commit 4b3a449e82

View file

@ -3,85 +3,93 @@
# bencode.py python3 compatable bencode / bdecode # bencode.py python3 compatable bencode / bdecode
# #
## ##
from collections import OrderedDict
def _decode_int(data):
class Decoder(object):
def _decode_int(self):
""" """
decode integer from bytearray decode integer from bytearray
return int, remaining data return int
""" """
data = data[1:] self.idx += 1
end = data.index(b'e') start = self.idx
return int(data[:end], 10), data[end+1:] end = self.data.index(b'e', self.idx)
self.idx = end + 1
return int(self.data[start:end])
def _decode_str(data): def _decode_str(self):
""" """
decode string from bytearray decode string from bytearray
return string, remaining data return string
""" """
start = data.index(b':') start = self.data.index(b':', self.idx)
l = int(data[:start].decode(), 10) l = int(self.data[self.idx:start].decode(), 10)
if l <= 0: if l <= 0:
raise Exception('invalid string size: %d' % l) raise Exception('invalid string size: %d' % l)
start += 1 start += 1
ret = bytes(data[start:start+l]) ret = self.data[start:start+l]
try: try:
ret = ret.decode('utf-8') ret = ret.decode('utf-8')
except: except:
pass pass
data = data[start+l:] self.idx = start + l
return ret, data return ret
def _decode_list(data): def _decode_list(self):
""" """
decode list from bytearray decode list from bytearray
return list, remaining data return list
""" """
ls = [] ls = []
data = data[1:] self.idx += 1
while data[0] != ord(b'e'): while self.data[self.idx] != ord(b'e'):
elem, data = _decode(data) ls.append(self._decode())
ls.append(elem) self.idx += 1
return ls, data[1:] return ls
def _decode_dict(data): def _decode_dict(self):
""" """
decode dict from bytearray decode dict from bytearray
return dict, remaining data return dict
""" """
d = {} d = OrderedDict()
data = data[1:] self.idx += 1
while data[0] != ord(b'e'): while self.data[self.idx] != ord(b'e'):
k, data = _decode_str(data) k = self._decode_str()
v, data = _decode(data) v = self._decode()
d[k] = v d[k] = v
return d, data[1:] self.idx += 1
return d
def _decode(data): def _decode(self):
""" ch = chr(self.data[self.idx])
decode a bytearray
return deserialized object, remaining data
"""
ch = chr(data[0])
if ch == 'l': if ch == 'l':
return _decode_list(data) return self._decode_list()
elif ch == 'i': elif ch == 'i':
return _decode_int(data) return self._decode_int()
elif ch == 'd': elif ch == 'd':
return _decode_dict(data) return self._decode_dict()
elif ch.isdigit(): elif ch.isdigit():
return _decode_str(data) return self._decode_str()
else: else:
raise Exception('could not deserialize data: %s' % data) raise Exception('could not decode data: %s' % data)
def decode(self, data):
self.idx = 0
self.data = data
obj = self._decode()
if len(data) != self.idx:
raise Exception('failed to decode, extra data: %s' % data)
return obj
def bdecode(data): def bdecode(data):
""" """
decode a bytearray decode a bytearray
return deserialized object return decoded object
""" """
obj, data = _decode(data) return Decoder().decode(data)
if len(data) > 0:
raise Exception('failed to deserialize, extra data: %s' % data)
return obj
def _encode_str(s, buff): def _encode_str(s, buff):
""" """
@ -114,10 +122,10 @@ def _encode_dict(d, buff):
encode dict encode dict
""" """
buff.append(b'd') buff.append(b'd')
l = list(d.keys()) for k in sorted(d):
l.sort() if not isinstance(k, [bytes, str]):
for k in l: k = str(k)
_encode(str(k), buff) _encode(k, buff)
_encode(d[k], buff) _encode(d[k], buff)
buff.append(b'e') buff.append(b'e')