diff options
author | Philipp Hagemeister <[email protected]> | 2014-04-07 19:57:42 +0200 |
---|---|---|
committer | Philipp Hagemeister <[email protected]> | 2014-04-07 19:57:42 +0200 |
commit | 734f90bb41baa76d9af84a949d61f3ec669d3a3b (patch) | |
tree | 94a58742dbbd575b4c5c7d1d66c3e00a92fe0cc1 | |
parent | 9afb76c5adc16e1f484f75aff30a57712815dc34 (diff) | |
download | youtube-dl-734f90bb41baa76d9af84a949d61f3ec669d3a3b.tar.gz youtube-dl-734f90bb41baa76d9af84a949d61f3ec669d3a3b.zip |
Use --encoding when outputting
-rw-r--r-- | youtube_dl/YoutubeDL.py | 32 | ||||
-rw-r--r-- | youtube_dl/utils.py | 13 |
2 files changed, 29 insertions, 16 deletions
diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 5794fdbe9..d011ddeb5 100644 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -286,6 +286,9 @@ class YoutubeDL(object): """Print message to stdout if not in quiet mode.""" return self.to_stdout(message, skip_eol, check_quiet=True) + def _write_string(self, s, out=None): + write_string(s, out=out, encoding=self.get_encoding()) + def to_stdout(self, message, skip_eol=False, check_quiet=False): """Print message to stdout if not in quiet mode.""" if self.params.get('logger'): @@ -295,7 +298,7 @@ class YoutubeDL(object): terminator = ['\n', ''][skip_eol] output = message + terminator - write_string(output, self._screen_file) + self._write_string(output, self._screen_file) def to_stderr(self, message): """Print message to stderr.""" @@ -305,7 +308,7 @@ class YoutubeDL(object): else: message = self._bidi_workaround(message) output = message + '\n' - write_string(output, self._err_file) + self._write_string(output, self._err_file) def to_console_title(self, message): if not self.params.get('consoletitle', False): @@ -315,21 +318,21 @@ class YoutubeDL(object): # already of type unicode() ctypes.windll.kernel32.SetConsoleTitleW(ctypes.c_wchar_p(message)) elif 'TERM' in os.environ: - write_string('\033]0;%s\007' % message, self._screen_file) + self._write_string('\033]0;%s\007' % message, self._screen_file) def save_console_title(self): if not self.params.get('consoletitle', False): return if 'TERM' in os.environ: # Save the title on stack - write_string('\033[22;0t', self._screen_file) + self._write_string('\033[22;0t', self._screen_file) def restore_console_title(self): if not self.params.get('consoletitle', False): return if 'TERM' in os.environ: # Restore the title from stack - write_string('\033[23;0t', self._screen_file) + self._write_string('\033[23;0t', self._screen_file) def __enter__(self): self.save_console_title() @@ -1211,9 +1214,16 @@ class YoutubeDL(object): if not self.params.get('verbose'): return - write_string('[debug] Encodings: locale %s, fs %s, out %s, pref %s\n' % - (locale.getpreferredencoding(), sys.getfilesystemencoding(), sys.stdout.encoding, self.get_encoding())) - write_string('[debug] youtube-dl version ' + __version__ + '\n') + write_string( + '[debug] Encodings: locale %s, fs %s, out %s, pref %s\n' % ( + locale.getpreferredencoding(), + sys.getfilesystemencoding(), + sys.stdout.encoding, + self.get_encoding()), + encoding=None + ) + + self._write_string('[debug] youtube-dl version ' + __version__ + '\n') try: sp = subprocess.Popen( ['git', 'rev-parse', '--short', 'HEAD'], @@ -1222,20 +1232,20 @@ class YoutubeDL(object): out, err = sp.communicate() out = out.decode().strip() if re.match('[0-9a-f]+', out): - write_string('[debug] Git HEAD: ' + out + '\n') + self._write_string('[debug] Git HEAD: ' + out + '\n') except: try: sys.exc_clear() except: pass - write_string('[debug] Python version %s - %s' % + self._write_string('[debug] Python version %s - %s' % (platform.python_version(), platform_name()) + '\n') proxy_map = {} for handler in self._opener.handlers: if hasattr(handler, 'proxies'): proxy_map.update(handler.proxies) - write_string('[debug] Proxy map: ' + compat_str(proxy_map) + '\n') + self._write_string('[debug] Proxy map: ' + compat_str(proxy_map) + '\n') def _setup_opener(self): timeout_val = self.params.get('socket_timeout') 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 |