blob: ff3398040f446e6b9141b4601cab52b1fe1376e6 [file] [log] [blame]
Behdad Esfahbod503179f2013-11-27 14:36:57 -05001"""Python 2/3 compat layer."""
2
Behdad Esfahbod32c10ee2013-11-27 17:46:17 -05003from __future__ import print_function, division
Behdad Esfahbod30e691e2013-11-27 17:27:45 -05004
Behdad Esfahbod503179f2013-11-27 14:36:57 -05005try:
6 basestring
7except NameError:
8 basestring = str
9
10try:
11 unicode
12except NameError:
13 unicode = str
14
15try:
16 unichr
17 bytechr = chr
Behdad Esfahbod319c5fd2013-11-27 18:13:48 -050018 byteord = ord
Behdad Esfahbod503179f2013-11-27 14:36:57 -050019except:
20 unichr = chr
21 def bytechr(n):
22 return bytes([n])
Behdad Esfahbod319c5fd2013-11-27 18:13:48 -050023 def byteord(c):
Behdad Esfahbodd3689392013-11-27 21:13:05 -050024 return c if isinstance(c, int) else ord(c)
Behdad Esfahbod503179f2013-11-27 14:36:57 -050025
26try:
Behdad Esfahbod94968f32013-12-04 04:11:06 -050027 from StringIO import StringIO
Behdad Esfahbod503179f2013-11-27 14:36:57 -050028except ImportError:
Behdad Esfahbodcc13b782013-12-04 04:12:57 -050029 from io import BytesIO as StringIO
Behdad Esfahbodac4672e2013-11-27 16:44:53 -050030
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050031def strjoin(iterable):
32 return ''.join(iterable)
Behdad Esfahbodac4672e2013-11-27 16:44:53 -050033if str == bytes:
34 class Tag(str):
35 def tobytes(self):
36 if isinstance(self, bytes):
37 return self
38 else:
Behdad Esfahbodc0762612013-11-28 06:46:59 -050039 return self.encode('latin1')
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050040
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050041 def tostr(s, encoding='ascii'):
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050042 if not isinstance(s, str):
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050043 return s.encode(encoding)
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050044 else:
45 return s
46 tobytes = tostr
Behdad Esfahbod821572c2013-11-27 21:09:03 -050047
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050048 bytesjoin = strjoin
Behdad Esfahbodac4672e2013-11-27 16:44:53 -050049else:
50 class Tag(str):
51
52 @staticmethod
53 def transcode(blob):
54 if not isinstance(blob, str):
55 blob = blob.decode('latin-1')
56 return blob
57
58 def __new__(self, content):
59 return str.__new__(self, self.transcode(content))
60 def __eq__(self, other):
61 return str.__eq__(self, self.transcode(other))
62
63 def __hash__(self):
64 return str.__hash__(self)
65
66 def tobytes(self):
67 return self.encode('latin-1')
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050068
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050069 def tostr(s, encoding='ascii'):
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050070 if not isinstance(s, str):
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050071 return s.decode(encoding)
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050072 else:
73 return s
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050074 def tobytes(s, encoding='ascii'):
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050075 if not isinstance(s, bytes):
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050076 return s.encode(encoding)
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050077 else:
78 return s
Behdad Esfahbod821572c2013-11-27 21:09:03 -050079
80 def bytesjoin(iterable):
Denis Jacqueryedb08ee22013-11-29 14:11:19 +010081 return b''.join(tobytes(item) for item in iterable)