From 49f2f54d2a9c4ab395eadba7c7b007fa73d5bc20 Mon Sep 17 00:00:00 2001 From: Jodok Batlogg Date: Fri, 1 Jun 2018 09:45:27 +0200 Subject: [PATCH 1/2] handle unicode strings properly (https://github.com/tartley/colorama/issues/36, https://github.com/Azure/azure-cli/issues/6408) --- colorama/ansitowin32.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/colorama/ansitowin32.py b/colorama/ansitowin32.py index daf95ca..db2671b 100644 --- a/colorama/ansitowin32.py +++ b/colorama/ansitowin32.py @@ -173,7 +173,10 @@ def write_and_convert(self, text): def write_plain_text(self, text, start, end): if start < end: - self.wrapped.write(text[start:end]) + if isinstance(text, unicode): + self.wrapped.write(text[start:end].encode('ascii', 'ignore')) + else: + self.wrapped.write(text[start:end]) self.wrapped.flush() From 3562d1afa4a51cc3b046b17f9dbf50b0949fff0a Mon Sep 17 00:00:00 2001 From: Jodok Batlogg Date: Sat, 16 Jun 2018 13:36:41 +0300 Subject: [PATCH 2/2] !fixup python3 compatibility --- colorama/ansitowin32.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/colorama/ansitowin32.py b/colorama/ansitowin32.py index db2671b..415d25f 100644 --- a/colorama/ansitowin32.py +++ b/colorama/ansitowin32.py @@ -12,6 +12,8 @@ if windll is not None: winterm = WinTerm() +is_python2 = sys.version_info.major == 2 + def is_stream_closed(stream): return not hasattr(stream, 'closed') or stream.closed @@ -173,7 +175,7 @@ def write_and_convert(self, text): def write_plain_text(self, text, start, end): if start < end: - if isinstance(text, unicode): + if is_python2 and isinstance(text, unicode): self.wrapped.write(text[start:end].encode('ascii', 'ignore')) else: self.wrapped.write(text[start:end])