2017-08-26 16:53:49 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
if sys.version_info < (3,):
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-08-26 16:53:49 +00:00
|
|
|
def isbasestring(s):
|
|
|
|
return isinstance(s, basestring)
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-08-26 16:53:49 +00:00
|
|
|
def open_utf8(filename, mode):
|
|
|
|
return open(filename, mode)
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-08-26 16:53:49 +00:00
|
|
|
def byte_to_str(x):
|
|
|
|
return str(ord(x))
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-08-26 16:53:49 +00:00
|
|
|
import cStringIO
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-08-26 16:53:49 +00:00
|
|
|
def StringIO():
|
|
|
|
return cStringIO.StringIO()
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-08-26 16:53:49 +00:00
|
|
|
def encode_utf8(x):
|
|
|
|
return x
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-10-29 16:55:05 +00:00
|
|
|
def decode_utf8(x):
|
|
|
|
return x
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-08-26 16:53:49 +00:00
|
|
|
def iteritems(d):
|
|
|
|
return d.iteritems()
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2018-05-28 17:41:16 +00:00
|
|
|
def itervalues(d):
|
|
|
|
return d.itervalues()
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-10-04 21:21:32 +00:00
|
|
|
def escape_string(s):
|
|
|
|
if isinstance(s, unicode):
|
2020-03-30 06:28:32 +00:00
|
|
|
s = s.encode("ascii")
|
|
|
|
result = ""
|
2017-10-04 21:21:32 +00:00
|
|
|
for c in s:
|
2020-03-30 06:28:32 +00:00
|
|
|
if not (32 <= ord(c) < 127) or c in ("\\", '"'):
|
|
|
|
result += "\\%03o" % ord(c)
|
2017-10-04 21:21:32 +00:00
|
|
|
else:
|
|
|
|
result += c
|
|
|
|
return result
|
|
|
|
|
2020-06-11 18:15:48 +00:00
|
|
|
def qualname(obj):
|
|
|
|
# Not properly equivalent to __qualname__ in py3, but it doesn't matter.
|
|
|
|
return obj.__name__
|
|
|
|
|
2017-08-26 16:53:49 +00:00
|
|
|
else:
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-08-26 16:53:49 +00:00
|
|
|
def isbasestring(s):
|
|
|
|
return isinstance(s, (str, bytes))
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-08-26 16:53:49 +00:00
|
|
|
def open_utf8(filename, mode):
|
|
|
|
return open(filename, mode, encoding="utf-8")
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-08-26 16:53:49 +00:00
|
|
|
def byte_to_str(x):
|
|
|
|
return str(x)
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-08-26 16:53:49 +00:00
|
|
|
import io
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-08-26 16:53:49 +00:00
|
|
|
def StringIO():
|
|
|
|
return io.StringIO()
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-08-26 16:53:49 +00:00
|
|
|
import codecs
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-08-26 16:53:49 +00:00
|
|
|
def encode_utf8(x):
|
|
|
|
return codecs.utf_8_encode(x)[0]
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-10-29 16:55:05 +00:00
|
|
|
def decode_utf8(x):
|
|
|
|
return codecs.utf_8_decode(x)[0]
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-08-26 16:53:49 +00:00
|
|
|
def iteritems(d):
|
|
|
|
return iter(d.items())
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2018-05-28 17:41:16 +00:00
|
|
|
def itervalues(d):
|
|
|
|
return iter(d.values())
|
2020-03-30 06:28:32 +00:00
|
|
|
|
2017-10-04 21:21:32 +00:00
|
|
|
def charcode_to_c_escapes(c):
|
|
|
|
rev_result = []
|
|
|
|
while c >= 256:
|
|
|
|
c, low = (c // 256, c % 256)
|
2020-03-30 06:28:32 +00:00
|
|
|
rev_result.append("\\%03o" % low)
|
|
|
|
rev_result.append("\\%03o" % c)
|
|
|
|
return "".join(reversed(rev_result))
|
|
|
|
|
2017-10-04 21:21:32 +00:00
|
|
|
def escape_string(s):
|
2020-03-30 06:28:32 +00:00
|
|
|
result = ""
|
2017-10-04 21:21:32 +00:00
|
|
|
if isinstance(s, str):
|
2020-03-30 06:28:32 +00:00
|
|
|
s = s.encode("utf-8")
|
2017-10-04 21:21:32 +00:00
|
|
|
for c in s:
|
2020-03-30 06:28:32 +00:00
|
|
|
if not (32 <= c < 127) or c in (ord("\\"), ord('"')):
|
2017-10-04 21:21:32 +00:00
|
|
|
result += charcode_to_c_escapes(c)
|
|
|
|
else:
|
|
|
|
result += chr(c)
|
|
|
|
return result
|
2020-06-11 18:15:48 +00:00
|
|
|
|
|
|
|
def qualname(obj):
|
|
|
|
return obj.__qualname__
|