1212
1313"""bencode.py - bencode encoder + decoder."""
1414
15- import pathlib
16-
17- from collections import deque , OrderedDict
18-
1915from bencode .BTL import BTFailure
2016from bencode .exceptions import BencodeDecodeError
2117
18+ from collections import deque
2219import sys
2320
21+ try :
22+ from collections import OrderedDict
23+ except ImportError :
24+ OrderedDict = None
25+
26+ try :
27+ import pathlib
28+ except ImportError :
29+ pathlib = None
30+
2431__all__ = (
2532 'BTFailure' ,
2633 'BencodeDecodeError' ,
@@ -90,6 +97,16 @@ def decode_list(x, f):
9097 return r , f + 1
9198
9299
100+ def decode_dict_py26 (x , f ):
101+ r , f = {}, f + 1
102+
103+ while x [f ] != 'e' :
104+ k , f = decode_string (x , f )
105+ r [k ], f = decode_func [x [f ]](x , f )
106+
107+ return r , f + 1
108+
109+
93110def decode_dict (x , f , force_sort = True ):
94111 """
95112 decode bencoded data to an OrderedDict
@@ -120,7 +137,6 @@ def decode_dict(x, f, force_sort=True):
120137# noinspection PyDictCreation
121138decode_func = {}
122139decode_func [b'l' ] = decode_list
123- decode_func [b'd' ] = decode_dict
124140decode_func [b'i' ] = decode_int
125141decode_func [b'0' ] = decode_string
126142decode_func [b'1' ] = decode_string
@@ -133,6 +149,11 @@ def decode_dict(x, f, force_sort=True):
133149decode_func [b'8' ] = decode_string
134150decode_func [b'9' ] = decode_string
135151
152+ if sys .version_info [0 ] == 2 and sys .version_info [1 ] == 6 :
153+ decode_func [b'd' ] = decode_dict_py26
154+ else :
155+ decode_func [b'd' ] = decode_dict
156+
136157
137158def bdecode (value ):
138159 """
@@ -213,24 +234,29 @@ def encode_dict(x, r):
213234encode_func [Bencached ] = encode_bencached
214235
215236if sys .version_info [0 ] == 2 :
216- from types import DictType , IntType , ListType , LongType , StringType , TupleType
237+ from types import DictType , IntType , ListType , LongType , StringType , TupleType , UnicodeType
217238
218239 encode_func [DictType ] = encode_dict
219240 encode_func [IntType ] = encode_int
220241 encode_func [ListType ] = encode_list
221242 encode_func [LongType ] = encode_int
222243 encode_func [StringType ] = encode_string
223244 encode_func [TupleType ] = encode_list
245+ encode_func [UnicodeType ] = encode_string
246+
247+ if OrderedDict is not None :
248+ encode_func [OrderedDict ] = encode_dict
224249
225250 try :
226251 from types import BooleanType
252+
227253 encode_func [BooleanType ] = encode_bool
228254 except ImportError :
229255 pass
230256else :
257+ encode_func [OrderedDict ] = encode_dict
231258 encode_func [bool ] = encode_bool
232259 encode_func [dict ] = encode_dict
233- encode_func [OrderedDict ] = encode_dict
234260 encode_func [int ] = encode_int
235261 encode_func [list ] = encode_list
236262 encode_func [str ] = encode_string
@@ -268,7 +294,7 @@ def bread(fd):
268294 if isinstance (fd , (bytes , str )):
269295 with open (fd , 'rb' ) as fd :
270296 return bdecode (fd .read ())
271- elif isinstance (fd , (pathlib .Path , pathlib .PurePath )):
297+ elif pathlib is not None and isinstance (fd , (pathlib .Path , pathlib .PurePath )):
272298 with open (str (fd ), 'rb' ) as fd :
273299 return bdecode (fd .read ())
274300 else :
@@ -285,7 +311,7 @@ def bwrite(data, fd):
285311 if isinstance (fd , (bytes , str )):
286312 with open (fd , 'wb' ) as fd :
287313 fd .write (bencode (data ))
288- elif isinstance (fd , (pathlib .Path , pathlib .PurePath )):
314+ elif pathlib is not None and isinstance (fd , (pathlib .Path , pathlib .PurePath )):
289315 with open (str (fd ), 'wb' ) as fd :
290316 fd .write (bencode (data ))
291317 else :
0 commit comments