mirror of
https://github.com/Sei-Lisa/LSL-PyOptimizer
synced 2025-07-01 23:58:20 +00:00
Additions for strutil: xrange, python2, python3, any2str
Also output to stderr with str instead of unicode.
This commit is contained in:
parent
4fd4bf71aa
commit
dde9577cea
1 changed files with 21 additions and 2 deletions
23
strutil.py
23
strutil.py
|
@ -25,6 +25,10 @@ import sys
|
||||||
if sys.version_info.major >= 3:
|
if sys.version_info.major >= 3:
|
||||||
unicode = str
|
unicode = str
|
||||||
unichr = chr
|
unichr = chr
|
||||||
|
xrange = range
|
||||||
|
python3 = True
|
||||||
|
python2 = False
|
||||||
|
|
||||||
def str2u(s, enc=None):
|
def str2u(s, enc=None):
|
||||||
"""Convert a native Python3 str to Unicode. This is a NOP."""
|
"""Convert a native Python3 str to Unicode. This is a NOP."""
|
||||||
return s
|
return s
|
||||||
|
@ -43,7 +47,17 @@ if sys.version_info.major >= 3:
|
||||||
return s.decode(getattr(enc, 'encoding', enc) or 'utf8',
|
return s.decode(getattr(enc, 'encoding', enc) or 'utf8',
|
||||||
'replace')
|
'replace')
|
||||||
|
|
||||||
|
def any2str(s, enc=None):
|
||||||
|
"""Convert Bytes or Unicode to native Python 3 str."""
|
||||||
|
return s if type(s) == str else b2str(s, enc)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
unicode = unicode
|
||||||
|
unichr = unichr
|
||||||
|
xrange = xrange
|
||||||
|
python2 = True
|
||||||
|
python3 = False
|
||||||
|
|
||||||
def str2u(s, enc=None):
|
def str2u(s, enc=None):
|
||||||
"""Convert a native Python2 str to Unicode."""
|
"""Convert a native Python2 str to Unicode."""
|
||||||
return s.decode(getattr(enc, 'encoding', enc) or 'utf8',
|
return s.decode(getattr(enc, 'encoding', enc) or 'utf8',
|
||||||
|
@ -62,6 +76,11 @@ else:
|
||||||
"""Convert a Bytes string to native Python 2 str. This is a NOP."""
|
"""Convert a Bytes string to native Python 2 str. This is a NOP."""
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
def any2str(s, enc=None):
|
||||||
|
"""Convert Bytes or Unicode to native Python 2 str."""
|
||||||
|
return s if type(s) == str else u2str(s, enc)
|
||||||
|
|
||||||
|
|
||||||
def b2u(s, enc=None):
|
def b2u(s, enc=None):
|
||||||
"""Bytes to Unicode"""
|
"""Bytes to Unicode"""
|
||||||
return str2u(b2str(s, enc), enc)
|
return str2u(b2str(s, enc), enc)
|
||||||
|
@ -80,10 +99,10 @@ def any2u(s, enc=None):
|
||||||
|
|
||||||
def werr(s):
|
def werr(s):
|
||||||
"""Write any string to stderr"""
|
"""Write any string to stderr"""
|
||||||
sys.stderr.write(any2u(s, sys.stderr))
|
sys.stderr.write(any2str(s, sys.stderr))
|
||||||
|
|
||||||
def wout(s):
|
def wout(s):
|
||||||
"""Write any string to stdout"""
|
"""Write any string to stdout"""
|
||||||
sys.stdout.write(any2u(s, sys.stdout))
|
sys.stdout.write(any2str(s, sys.stdout))
|
||||||
|
|
||||||
strutil_used = True
|
strutil_used = True
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue