2013-11-27 14:36:57 -05:00
|
|
|
"""Python 2/3 compat layer."""
|
|
|
|
|
2014-01-14 15:07:50 +08:00
|
|
|
from __future__ import print_function, division, absolute_import
|
2013-11-27 17:27:45 -05:00
|
|
|
|
2013-11-27 14:36:57 -05:00
|
|
|
try:
|
|
|
|
basestring
|
|
|
|
except NameError:
|
|
|
|
basestring = str
|
|
|
|
|
|
|
|
try:
|
|
|
|
unicode
|
|
|
|
except NameError:
|
|
|
|
unicode = str
|
|
|
|
|
|
|
|
try:
|
|
|
|
unichr
|
|
|
|
bytechr = chr
|
2013-11-27 18:13:48 -05:00
|
|
|
byteord = ord
|
2013-11-27 14:36:57 -05:00
|
|
|
except:
|
|
|
|
unichr = chr
|
|
|
|
def bytechr(n):
|
|
|
|
return bytes([n])
|
2013-11-27 18:13:48 -05:00
|
|
|
def byteord(c):
|
2013-11-27 21:13:05 -05:00
|
|
|
return c if isinstance(c, int) else ord(c)
|
2013-11-27 14:36:57 -05:00
|
|
|
|
|
|
|
try:
|
2013-12-04 04:11:06 -05:00
|
|
|
from StringIO import StringIO
|
2013-11-27 14:36:57 -05:00
|
|
|
except ImportError:
|
2013-12-04 04:12:57 -05:00
|
|
|
from io import BytesIO as StringIO
|
2013-11-27 16:44:53 -05:00
|
|
|
|
2013-11-27 21:17:35 -05:00
|
|
|
def strjoin(iterable):
|
|
|
|
return ''.join(iterable)
|
2013-11-27 16:44:53 -05:00
|
|
|
if str == bytes:
|
|
|
|
class Tag(str):
|
|
|
|
def tobytes(self):
|
|
|
|
if isinstance(self, bytes):
|
|
|
|
return self
|
|
|
|
else:
|
2013-11-28 06:46:59 -05:00
|
|
|
return self.encode('latin1')
|
2013-11-27 19:51:59 -05:00
|
|
|
|
2013-11-27 21:17:35 -05:00
|
|
|
def tostr(s, encoding='ascii'):
|
2013-11-27 19:51:59 -05:00
|
|
|
if not isinstance(s, str):
|
2013-11-27 21:17:35 -05:00
|
|
|
return s.encode(encoding)
|
2013-11-27 19:51:59 -05:00
|
|
|
else:
|
|
|
|
return s
|
|
|
|
tobytes = tostr
|
2013-11-27 21:09:03 -05:00
|
|
|
|
2013-11-27 21:17:35 -05:00
|
|
|
bytesjoin = strjoin
|
2013-11-27 16:44:53 -05:00
|
|
|
else:
|
|
|
|
class Tag(str):
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def transcode(blob):
|
|
|
|
if not isinstance(blob, str):
|
|
|
|
blob = blob.decode('latin-1')
|
|
|
|
return blob
|
|
|
|
|
|
|
|
def __new__(self, content):
|
|
|
|
return str.__new__(self, self.transcode(content))
|
2013-12-06 22:25:48 -05:00
|
|
|
def __ne__(self, other):
|
|
|
|
return not self.__eq__(other)
|
2013-11-27 16:44:53 -05:00
|
|
|
def __eq__(self, other):
|
|
|
|
return str.__eq__(self, self.transcode(other))
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return str.__hash__(self)
|
|
|
|
|
|
|
|
def tobytes(self):
|
|
|
|
return self.encode('latin-1')
|
2013-11-27 19:51:59 -05:00
|
|
|
|
2013-11-27 21:17:35 -05:00
|
|
|
def tostr(s, encoding='ascii'):
|
2013-11-27 19:51:59 -05:00
|
|
|
if not isinstance(s, str):
|
2013-11-27 21:17:35 -05:00
|
|
|
return s.decode(encoding)
|
2013-11-27 19:51:59 -05:00
|
|
|
else:
|
|
|
|
return s
|
2013-11-27 21:17:35 -05:00
|
|
|
def tobytes(s, encoding='ascii'):
|
2013-11-27 19:51:59 -05:00
|
|
|
if not isinstance(s, bytes):
|
2013-11-27 21:17:35 -05:00
|
|
|
return s.encode(encoding)
|
2013-11-27 19:51:59 -05:00
|
|
|
else:
|
|
|
|
return s
|
2013-11-27 21:09:03 -05:00
|
|
|
|
|
|
|
def bytesjoin(iterable):
|
2013-11-29 14:11:19 +01:00
|
|
|
return b''.join(tobytes(item) for item in iterable)
|