1212
1313# Based on https://github.com/karamanolev/bencode3/blob/master/bencode.py
1414
15- __version__ = ' 1.1.2 '
15+ __version__ = ' 1.1.3 '
1616
17- import sys
18- IS_PY2 = sys.version[0 ] == ' 2'
17+ import array
1918
2019try :
2120 from collections import OrderedDict
2221except ImportError :
2322 from ordereddict import OrderedDict
2423
24+ from cpython.version cimport PY_MAJOR_VERSION
25+ IS_PY2 = PY_MAJOR_VERSION == 2
2526if IS_PY2:
2627 END_CHAR = ' e'
28+ ARRAY_TYPECODE = b' b'
2729else :
2830 END_CHAR = ord (' e' )
31+ ARRAY_TYPECODE = ' b'
2932
3033
3134class BTFailure (Exception ):
@@ -34,7 +37,7 @@ class BTFailure(Exception):
3437
3538def decode_int (bytes x , int f ):
3639 f += 1
37- new_f = x.index(b' e' , f)
40+ cdef long new_f = x.index(b' e' , f)
3841 n = int (x[f:new_f])
3942 if x[f] == b' -' [0 ]:
4043 if x[f + 1 ] == b' 0' [0 ]:
@@ -45,8 +48,8 @@ def decode_int(bytes x, int f):
4548
4649
4750def decode_string (bytes x , int f ):
48- colon = x.index(b' :' , f)
49- n = int (x[f:colon])
51+ cdef long colon = x.index(b' :' , f)
52+ cdef long n = int (x[f:colon])
5053 if x[f] == b' 0' [0 ] and colon != f + 1 :
5154 raise ValueError ()
5255 colon += 1
@@ -62,7 +65,8 @@ def decode_list(bytes x, int f):
6265
6366
6467def decode_dict (bytes x , int f ):
65- r, f = OrderedDict(), f + 1
68+ r = OrderedDict()
69+ f += 1
6670 while x[f] != END_CHAR:
6771 k, f = decode_string(x, f)
6872 r[k], f = decode_func[x[f]](x, f)
@@ -107,56 +111,63 @@ def encode(v, r):
107111 )
108112
109113
110- def encode_int (x , list r ):
111- r.extend((b' i' , str (x).encode(), b' e' ))
114+ def encode_int (long x , r ):
115+ r.fromstring(b' i' )
116+ r.fromstring(str (x).encode())
117+ r.fromstring(b' e' )
112118
113119
114- def encode_bool (x , list r ):
115- if x:
116- encode_int(1 , r)
117- else :
118- encode_int(0 , r)
120+ def encode_long (x , r ):
121+ r.fromstring(b' i' )
122+ r.fromstring(str (x).encode())
123+ r.fromstring(b' e' )
124+
125+
126+ def encode_bytes (bytes x , r ):
127+ r.fromstring(str (len (x)).encode())
128+ r.fromstring(b' :' )
129+ r.fromstring(x)
119130
120131
121- def encode_string (x , list r ):
122- if isinstance (x, str ):
123- x = x.encode( )
124- r.extend(( str ( len (x)) .encode(), b ' : ' , x ))
132+ def encode_string (str x , r ):
133+ r.fromstring( str ( len (x)).encode())
134+ r.fromstring(b ' : ' )
135+ r.fromstring(x .encode())
125136
126137
127- def encode_list (x , list r ):
128- r.append (b' l' )
138+ def encode_list (x , r ):
139+ r.fromstring (b' l' )
129140 for i in x:
130141 encode(i, r)
131- r.append (b' e' )
142+ r.fromstring (b' e' )
132143
133144
134- def encode_dict (x , list r ):
135- r.append (b' d' )
145+ def encode_dict (x , r ):
146+ r.fromstring (b' d' )
136147 item_list = list (x.items())
137148 item_list.sort()
138149 for k, v in item_list:
139150 if isinstance (k, str ):
140151 k = k.encode()
141- r.extend(( str ( len (k)).encode(), b ' : ' , k) )
152+ encode_bytes(k, r )
142153 encode(v, r)
143- r.append (b' e' )
154+ r.fromstring (b' e' )
144155
145156
146157encode_func = {
147158 int : encode_int,
148- long : encode_int,
149- bytes: encode_string,
159+ bool : encode_int,
160+ long : encode_long,
161+ bytes: encode_bytes,
150162 str : encode_string,
151163 list : encode_list,
152164 tuple : encode_list,
153165 dict : encode_dict,
154166 OrderedDict: encode_dict,
155- bool : encode_bool,
156167}
157168
158169
159170def bencode (x ):
160- r = []
171+ r = array.array(ARRAY_TYPECODE)
161172 encode(x, r)
162- return b ' ' .join(r )
173+ return r.tostring( )
0 commit comments