aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'youtube_dl/utils.py')
-rw-r--r--youtube_dl/utils.py13
1 files changed, 8 insertions, 5 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py
index 92fee966f..f53db0228 100644
--- a/youtube_dl/utils.py
+++ b/youtube_dl/utils.py
@@ -910,22 +910,25 @@ def platform_name():
return res
-def write_string(s, out=None):
+def write_string(s, out=None, encoding=None):
if out is None:
out = sys.stderr
assert type(s) == compat_str
if ('b' in getattr(out, 'mode', '') or
sys.version_info[0] < 3): # Python 2 lies about mode of sys.stderr
- s = s.encode(preferredencoding(), 'ignore')
+ s = s.encode(encoding or preferredencoding(), 'ignore')
try:
out.write(s)
except UnicodeEncodeError:
# In Windows shells, this can fail even when the codec is just charmap!?
# See https://wiki.python.org/moin/PrintFails#Issue
- if sys.platform == 'win32' and hasattr(out, 'encoding'):
- s = s.encode(out.encoding, 'ignore').decode(out.encoding)
- out.write(s)
+ if sys.platform == 'win32':
+ if not encoding and hasattr(out, 'encoding'):
+ encoding = out.encoding
+ if encoding:
+ b = s.encode(encoding, 'ignore').decode(encoding)
+ out.write(b)
else:
raise