[utils] Add `get_subprocess_encoding` and filename/argument decode counterparts

This commit is contained in:
Sergey M․ 2015-04-26 04:29:41 +06:00
parent 642f23bd81
commit aa49acd15a
1 changed files with 34 additions and 15 deletions

View File

@ -371,6 +371,18 @@ def unescapeHTML(s):
r'&([^;]+);', lambda m: _htmlentity_transform(m.group(1)), s) r'&([^;]+);', lambda m: _htmlentity_transform(m.group(1)), s)
def get_subprocess_encoding():
if sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
# For subprocess calls, encode with locale encoding
# Refer to http://stackoverflow.com/a/9951851/35070
encoding = preferredencoding()
else:
encoding = sys.getfilesystemencoding()
if encoding is None:
encoding = 'utf-8'
return encoding
def encodeFilename(s, for_subprocess=False): def encodeFilename(s, for_subprocess=False):
""" """
@param s The name of the file @param s The name of the file
@ -382,21 +394,24 @@ def encodeFilename(s, for_subprocess=False):
if sys.version_info >= (3, 0): if sys.version_info >= (3, 0):
return s return s
if sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
# Pass '' directly to use Unicode APIs on Windows 2000 and up # Pass '' directly to use Unicode APIs on Windows 2000 and up
# (Detecting Windows NT 4 is tricky because 'major >= 4' would # (Detecting Windows NT 4 is tricky because 'major >= 4' would
# match Windows 9x series as well. Besides, NT 4 is obsolete.) # match Windows 9x series as well. Besides, NT 4 is obsolete.)
if not for_subprocess: if not for_subprocess and sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
return s return s
else:
# For subprocess calls, encode with locale encoding return s.encode(get_subprocess_encoding(), 'ignore')
# Refer to http://stackoverflow.com/a/9951851/35070
encoding = preferredencoding()
else: def decodeFilename(b, for_subprocess=False):
encoding = sys.getfilesystemencoding()
if encoding is None: if sys.version_info >= (3, 0):
encoding = 'utf-8' return b
return s.encode(encoding, 'ignore')
if not isinstance(b, bytes):
return b
return b.decode(get_subprocess_encoding(), 'ignore')
def encodeArgument(s): def encodeArgument(s):
@ -408,6 +423,10 @@ def encodeArgument(s):
return encodeFilename(s, True) return encodeFilename(s, True)
def decodeArgument(b):
return decodeFilename(b, True)
def decodeOption(optval): def decodeOption(optval):
if optval is None: if optval is None:
return optval return optval