Skip to content

Commit

Permalink
use list instead of array
Browse files Browse the repository at this point in the history
  • Loading branch information
whtsky committed Feb 11, 2018
1 parent a774cca commit 121a82c
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions bencoder.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

__version__ = '1.2.0'

import array

try:
from collections import OrderedDict
Expand Down Expand Up @@ -101,7 +100,7 @@ def bdecode(bytes x):
raise BTFailure("invalid bencoded value (data after valid prefix)")
return r

def encode(v, r):
cdef encode(v, list r):
tp = type(v)
if tp in encode_func:
return encode_func[tp](v, r)
Expand All @@ -114,47 +113,47 @@ def encode(v, r):
)


def encode_int(long x, r):
r.fromstring(b'i')
r.fromstring(str(x).encode())
r.fromstring(b'e')
cdef encode_int(long x, list r):
r.append(b'i')
r.append(str(x).encode())
r.append(b'e')


def encode_long(x, r):
r.fromstring(b'i')
r.fromstring(str(x).encode())
r.fromstring(b'e')
cdef encode_long(x, list r):
r.append(b'i')
r.append(str(x).encode())
r.append(b'e')


def encode_bytes(bytes x, r):
r.fromstring(str(len(x)).encode())
r.fromstring(b':')
r.fromstring(x)
cdef encode_bytes(bytes x, list r):
r.append(str(len(x)).encode())
r.append(b':')
r.append(x)


def encode_string(str x, r):
r.fromstring(str(len(x)).encode())
r.fromstring(b':')
r.fromstring(x.encode())
cdef encode_string(str x, list r):
r.append(str(len(x)).encode())
r.append(b':')
r.append(x.encode())


def encode_list(x, r):
r.fromstring(b'l')
cdef encode_list(x, list r):
r.append(b'l')
for i in x:
encode(i, r)
r.fromstring(b'e')
r.append(b'e')


def encode_dict(x, r):
r.fromstring(b'd')
cdef encode_dict(x, list r):
r.append(b'd')
item_list = list(x.items())
item_list.sort()
for k, v in item_list:
if isinstance(k, str):
k = k.encode()
encode_bytes(k, r)
encode(v, r)
r.fromstring(b'e')
r.append(b'e')


encode_func = {
Expand All @@ -171,6 +170,6 @@ encode_func = {


def bencode(x):
r = array.array(ARRAY_TYPECODE)
r = []
encode(x, r)
return r.tostring()
return b''.join(r)

0 comments on commit 121a82c

Please sign in to comment.