1414
1515__version__ = ' 1.1.2'
1616
17- import sys
18- IS_PY2 = sys.version[ 0 ] == ' 2 '
17+ from cpython cimport array
18+ import array
1919
2020try :
2121 from collections import OrderedDict
2222except ImportError :
2323 from ordereddict import OrderedDict
2424
25+ from cpython.version cimport PY_MAJOR_VERSION
26+ IS_PY2 = PY_MAJOR_VERSION == 2
2527if IS_PY2:
2628 END_CHAR = ' e'
2729else :
@@ -34,7 +36,7 @@ class BTFailure(Exception):
3436
3537def decode_int (bytes x , int f ):
3638 f += 1
37- new_f = x.index(b' e' , f)
39+ cdef long new_f = x.index(b' e' , f)
3840 n = int (x[f:new_f])
3941 if x[f] == b' -' [0 ]:
4042 if x[f + 1 ] == b' 0' [0 ]:
@@ -45,8 +47,8 @@ def decode_int(bytes x, int f):
4547
4648
4749def decode_string (bytes x , int f ):
48- colon = x.index(b' :' , f)
49- n = int (x[f:colon])
50+ cdef long colon = x.index(b' :' , f)
51+ cdef long n = int (x[f:colon])
5052 if x[f] == b' 0' [0 ] and colon != f + 1 :
5153 raise ValueError ()
5254 colon += 1
@@ -62,7 +64,8 @@ def decode_list(bytes x, int f):
6264
6365
6466def decode_dict (bytes x , int f ):
65- r, f = OrderedDict(), f + 1
67+ r = OrderedDict()
68+ f += 1
6669 while x[f] != END_CHAR:
6770 k, f = decode_string(x, f)
6871 r[k], f = decode_func[x[f]](x, f)
@@ -107,39 +110,47 @@ def encode(v, r):
107110 )
108111
109112
110- cdef encode_int(long x, list r):
111- r.extend((b' i' , str (x).encode(), b' e' ))
113+ def encode_int (long x , r ):
114+ r.frombytes(b' i' )
115+ r.frombytes(str (x).encode())
116+ r.frombytes(b' e' )
112117
113118
114- def encode_long (x , list r ):
115- r.extend((b' i' , str (x).encode(), b' e' ))
119+ def encode_long (x , r ):
120+ r.frombytes(b' i' )
121+ r.frombytes(str (x).encode())
122+ r.frombytes(b' e' )
116123
117124
118- cdef encode_bytes(bytes x, list r):
119- r.extend((str (len (x)).encode(), b' :' , x))
125+ def encode_bytes (bytes x , r ):
126+ r.frombytes(str (len (x)).encode())
127+ r.frombytes(b' :' )
128+ r.frombytes(x)
120129
121130
122- def encode_string (str x , list r ):
123- r.extend((str (len (x)).encode(), b' :' , x.encode()))
131+ def encode_string (str x , r ):
132+ r.frombytes(str (len (x)).encode())
133+ r.frombytes(b' :' )
134+ r.frombytes(x.encode())
124135
125136
126- def encode_list (x , list r ):
127- r.append (b' l' )
137+ def encode_list (x , r ):
138+ r.frombytes (b' l' )
128139 for i in x:
129140 encode(i, r)
130- r.append (b' e' )
141+ r.frombytes (b' e' )
131142
132143
133- def encode_dict (x , list r ):
134- r.append (b' d' )
144+ def encode_dict (x , r ):
145+ r.frombytes (b' d' )
135146 item_list = list (x.items())
136147 item_list.sort()
137148 for k, v in item_list:
138149 if isinstance (k, str ):
139150 k = k.encode()
140- r.extend(( str ( len (k)).encode(), b ' : ' , k) )
151+ encode_bytes(k, r )
141152 encode(v, r)
142- r.append (b' e' )
153+ r.frombytes (b' e' )
143154
144155
145156encode_func = {
@@ -156,6 +167,6 @@ encode_func = {
156167
157168
158169def bencode (x ):
159- r = []
170+ cdef array.array r = array.array( ' b ' )
160171 encode(x, r)
161- return b ' ' .join(r )
172+ return r.tobytes( )
0 commit comments