def _encode(s):
'''This is a helper for output of unicode. With Python2 it is necessary to
do encoding to the LOCAL_ENCODING since by default unicode will be encoded
to ascii. In python3 this conversion is not necessary for the user to
to perform; in fact sys.std*.write, for example, requires unicode strings
be passed in. This function will encode for python2 and do nothing
for python3 (except assert that ``s`` is a unicode type).'''
if PY2:
if isinstance(s, unicode):
try:
return s.encode(LOCAL_ENCODING)
except:
log.exception()
return s.encode(LOCAL_ENCODING, "replace")
elif isinstance(s, str):
return s
else:
raise TypeError("Argument must be str or unicode")
else:
assert(isinstance(s, str))
return s
评论列表
文章目录