unicode = str
long = int
basestring = (str, bytes)
def _fallback_bytes(s):
return bytes(ord(c) & 0xFF for c in s)
def _fallback_str(b):
return "".join(chr(c) for c in b)
def asbytes(s):
if isinstance(s, bytes):
return s
try:
return s.encode("utf-8")
except (LookupError, UnicodeEncodeError):
return _fallback_bytes(s)
def asstr(s):
if isinstance(s, str):
return s
try:
return s.decode("utf-8")
except (LookupError, UnicodeDecodeError):
return _fallback_str(s)
def asunicode(s):
return asstr(s)
__all__ = [
"unicode",
"long",
"basestring",
"asbytes",
"asstr",
"asunicode",
]