blob: 28428401c51c7ff2ae28f66284918869470dd524 [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:
27 from cStringIO import StringIO
28except ImportError:
Behdad Esfahbod3e8795d2013-11-27 21:10:27 -050029 try:
30 from StringIO import StringIO
31 except ImportError:
32 from io import BytesIO as StringIO
Behdad Esfahbodac4672e2013-11-27 16:44:53 -050033
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050034def strjoin(iterable):
35 return ''.join(iterable)
Behdad Esfahbodac4672e2013-11-27 16:44:53 -050036if str == bytes:
37 class Tag(str):
38 def tobytes(self):
39 if isinstance(self, bytes):
40 return self
41 else:
Behdad Esfahbodc0762612013-11-28 06:46:59 -050042 return self.encode('latin1')
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050043
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050044 def tostr(s, encoding='ascii'):
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050045 if not isinstance(s, str):
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050046 return s.encode(encoding)
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050047 else:
48 return s
49 tobytes = tostr
Behdad Esfahbod821572c2013-11-27 21:09:03 -050050
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050051 bytesjoin = strjoin
Behdad Esfahbodac4672e2013-11-27 16:44:53 -050052else:
53 class Tag(str):
54
55 @staticmethod
56 def transcode(blob):
57 if not isinstance(blob, str):
58 blob = blob.decode('latin-1')
59 return blob
60
61 def __new__(self, content):
62 return str.__new__(self, self.transcode(content))
63 def __eq__(self, other):
64 return str.__eq__(self, self.transcode(other))
65
66 def __hash__(self):
67 return str.__hash__(self)
68
69 def tobytes(self):
70 return self.encode('latin-1')
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050071
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050072 def tostr(s, encoding='ascii'):
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050073 if not isinstance(s, str):
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050074 return s.decode(encoding)
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050075 else:
76 return s
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050077 def tobytes(s, encoding='ascii'):
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050078 if not isinstance(s, bytes):
Behdad Esfahbod18316aa2013-11-27 21:17:35 -050079 return s.encode(encoding)
Behdad Esfahbod5cf40082013-11-27 19:51:59 -050080 else:
81 return s
Behdad Esfahbod821572c2013-11-27 21:09:03 -050082
83 def bytesjoin(iterable):
Denis Jacqueryedb08ee22013-11-29 14:11:19 +010084 return b''.join(tobytes(item) for item in iterable)