]> gitweb @ CieloNegro.org - youtube-dl.git/commitdiff
Use our own encoding when writing strings
authorPhilipp Hagemeister <phihag@phihag.de>
Mon, 7 Apr 2014 19:40:34 +0000 (21:40 +0200)
committerPhilipp Hagemeister <phihag@phihag.de>
Mon, 7 Apr 2014 19:40:34 +0000 (21:40 +0200)
youtube_dl/utils.py

index f53db02286b6edbf2047fc2e381488775ba52805..68424aeb6b987bd2c11bf592c706f57a967be7d7 100644 (file)
@@ -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()