Use unicode sys paths always

This commit is contained in:
Dave Halter
2017-12-24 02:42:14 +01:00
parent 7bfca5bcd7
commit a38acdbe08
4 changed files with 37 additions and 9 deletions
+18 -5
View File
@@ -226,13 +226,26 @@ def u(string):
have to cast back to a unicode (and we now that we always deal with valid
unicode, because we check that in the beginning).
"""
if is_py3:
return str(string)
if not isinstance(string, unicode):
return unicode(str(string), 'UTF-8')
if isinstance(string, bytes):
return str(string, encoding='UTF-8')
return string
def cast_path(obj):
"""
Take a bytes or str path and cast it to unicode.
Apparently it is perfectly fine to pass both byte and unicode objects into
the sys.path. This probably means that byte paths are normal at other
places as well.
Since this just really complicates everything and Python 2.7 will be EOL
soon anyway, just go with always strings.
"""
return unicode(obj, encoding='utf-8', errors='replace') \
if isinstance(obj, bytes) else obj
try:
import builtins # module name in python 3
except ImportError: