aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/utils.py
diff options
context:
space:
mode:
authorPhilipp Hagemeister <phihag@phihag.de>2014-04-07 21:40:34 +0200
committerPhilipp Hagemeister <phihag@phihag.de>2014-04-07 21:40:34 +0200
commit104aa7388a1d667877976a561d47c2b61be75f82 (patch)
treeb27905ad54108387dcffa3a2972b1bb70c255fcd /youtube_dl/utils.py
parentc3855d28b043ccb61ea38ef108831bd395fba4db (diff)
Use our own encoding when writing strings
Diffstat (limited to 'youtube_dl/utils.py')
-rw-r--r--youtube_dl/utils.py21
1 files changed, 7 insertions, 14 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py
index f53db0228..68424aeb6 100644
--- a/youtube_dl/utils.py
+++ b/youtube_dl/utils.py
@@ -917,21 +917,14 @@ def write_string(s, out=None, encoding=None):
if ('b' in getattr(out, 'mode', '') or
sys.version_info[0] < 3): # Python 2 lies about mode of sys.stderr
- s = s.encode(encoding or preferredencoding(), 'ignore')
- try:
+ byt = s.encode(encoding or preferredencoding(), 'ignore')
+ out.write(byt)
+ elif hasattr(out, 'buffer'):
+ enc = encoding or getattr(out, 'encoding', None) or preferredencoding()
+ byt = s.encode(enc, 'ignore')
+ out.buffer.write(byt)
+ else:
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':
- if not encoding and hasattr(out, 'encoding'):
- encoding = out.encoding
- if encoding:
- b = s.encode(encoding, 'ignore').decode(encoding)
- out.write(b)
- else:
- raise
-
out.flush()