1
0
Fork 0
mirror of https://github.com/ytdl-org/youtube-dl.git synced 2024-12-23 01:08:10 +00:00

[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

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 and sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
if not for_subprocess: return s
return s
else: return s.encode(get_subprocess_encoding(), 'ignore')
# For subprocess calls, encode with locale encoding
# Refer to http://stackoverflow.com/a/9951851/35070
encoding = preferredencoding() def decodeFilename(b, for_subprocess=False):
else:
encoding = sys.getfilesystemencoding() if sys.version_info >= (3, 0):
if encoding is None: return b
encoding = 'utf-8'
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