From 594cb8deb77384576df8cb2b293a96d5ff7e49bd 2016-07-19 13:33:47 From: Thomas Kluyver Date: 2016-07-19 13:33:47 Subject: [PATCH] Be more lenient when bytes are sent to stdout/stderr on Py2+Windows Closes gh-9768 --- diff --git a/IPython/terminal/interactiveshell.py b/IPython/terminal/interactiveshell.py index 5626a61..462dc46 100644 --- a/IPython/terminal/interactiveshell.py +++ b/IPython/terminal/interactiveshell.py @@ -320,14 +320,36 @@ class TerminalInteractiveShell(InteractiveShell): pre_run=self.pre_prompt, reset_current_buffer=True) return document.text + def enable_win_unicode_console(self): + import win_unicode_console + + if PY3: + win_unicode_console.enable() + else: + # https://github.com/ipython/ipython/issues/9768 + from win_unicode_console.streams import (TextStreamWrapper, + stdout_text_transcoded, stderr_text_transcoded) + + class LenientStrStreamWrapper(TextStreamWrapper): + def write(self, s): + if isinstance(s, bytes): + s = s.decode(self.encoding, 'replace') + + self.base.write(s) + + stdout_text_str = LenientStrStreamWrapper(stdout_text_transcoded) + stderr_text_str = LenientStrStreamWrapper(stderr_text_transcoded) + + win_unicode_console.enable(stdout=stdout_text_str, + stderr=stderr_text_str) + def init_io(self): if sys.platform not in {'win32', 'cli'}: return - import win_unicode_console - import colorama + self.enable_win_unicode_console() - win_unicode_console.enable() + import colorama colorama.init() # For some reason we make these wrappers around stdout/stderr.