Misc small non-user-visible fixes

Some are Unicode vs Str stuff, others are style adjustments, others in unused functions, and so on.
This commit is contained in:
Sei Lisa 2020-11-09 01:51:24 +01:00
parent 59c3f9fc71
commit d9938f1a37
5 changed files with 33 additions and 32 deletions

View file

@ -31,8 +31,7 @@ if sys.version_info.major >= 3:
def str2b(s, enc=None):
"""Convert a native Python3 str to bytes, with the given encoding."""
return s.encode(enc if type(enc) == str
else getattr(enc, 'encoding', 'utf8'),
return s.encode(getattr(enc, 'encoding', enc) or 'utf8',
'backslashreplace')
def u2str(s, enc=None):
@ -56,8 +55,7 @@ else:
def u2str(s, enc=None):
"""Convert a Unicode string to native Python 2 str."""
return s.encode(enc if type(enc) == str
else getattr(enc, 'encoding', 'utf8'),
return s.encode(getattr(enc, 'encoding', enc) or 'utf8',
'backslashreplace')
def b2str(s, enc=None):
@ -70,7 +68,7 @@ def b2u(s, enc=None):
def u2b(s, enc=None):
"""Unicode to Bytes"""
return u2str(str2b(s, enc), enc)
return str2b(u2str(s, enc), enc)
def any2b(s, enc=None):
"""Bytes or Unicode to Bytes"""