blob: 90217a3379309fe48c7f915c63144a02984cbf1a [file] [log] [blame]
Behdad Esfahbod503179f2013-11-27 14:36:57 -05001"""Python 2/3 compat layer."""
2
Behdad Esfahbod1ae29592014-01-14 15:07:50 +08003from __future__ import print_function, division, absolute_import
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))
Behdad Esfahbod8ea64392013-12-06 22:25:48 -050060 def __ne__(self, other):
61 return not self.__eq__(other)
Behdad Esfahbodac4672e2013-11-27 16:44:53 -050062 def __eq__(self, other):
63 return str.__eq__(self, self.transcode(other))
64
65 def __hash__(self):
66 return str.__hash__(self)
67
68 def tobytes(self):
69 return self.encode('latin-1')
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050070
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050071 def tostr(s, encoding='ascii'):
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050072 if not isinstance(s, str):
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050073 return s.decode(encoding)
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050074 else:
75 return s
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050076 def tobytes(s, encoding='ascii'):
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050077 if not isinstance(s, bytes):
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050078 return s.encode(encoding)
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050079 else:
80 return s
Behdad Esfahbod821572c2013-11-27 21:09:03 -050081
82 def bytesjoin(iterable):
Denis Jacqueryedb08ee22013-11-29 14:11:19 +010083 return b''.join(tobytes(item) for item in iterable)