From ad9e5fe8a557b1cb0b54aeb5268eb209abfaf0c8 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Mon, 26 Oct 2009 22:26:31 +0000 Subject: Bug #290625 - Manually encode output to stdout in python3, in order to avoid potential UnicodeEncodeError exceptions. svn path=/main/trunk/; revision=14734 --- pym/portage/output.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'pym/portage/output.py') diff --git a/pym/portage/output.py b/pym/portage/output.py index 6044f2bbc..6d4e108aa 100644 --- a/pym/portage/output.py +++ b/pym/portage/output.py @@ -251,11 +251,15 @@ def xtermTitle(mystr, raw=False): mystr = mystr[:_max_xtermTitle_len] if not raw: mystr = '\x1b]0;%s\x07' % mystr - if sys.hexversion < 0x3000000 and isinstance(mystr, unicode): - # avoid potential UnicodeEncodeError - mystr = mystr.encode(_encodings['stdio'], 'backslashreplace') - sys.stderr.write(mystr) - sys.stderr.flush() + + # avoid potential UnicodeEncodeError + mystr = _unicode_encode(mystr, + encoding=_encodings['stdio'], errors='backslashreplace') + f = sys.stderr + if sys.hexversion >= 0x3000000: + f = f.buffer + f.write(mystr) + f.flush() default_xterm_title = None @@ -374,11 +378,12 @@ class ConsoleStyleFile(object): self._write(self.write_listener, s) def _write(self, f, s): - if sys.hexversion < 0x3000000 and \ - isinstance(s, unicode) and \ - f in (sys.stdout, sys.stderr): - # avoid potential UnicodeEncodeError - s = s.encode(_encodings['stdio'], 'backslashreplace') + # avoid potential UnicodeEncodeError + if f in (sys.stdout, sys.stderr): + s = _unicode_encode(s, + encoding=_encodings['stdio'], errors='backslashreplace') + if sys.hexversion >= 0x3000000: + f = f.buffer f.write(s) def writelines(self, lines): @@ -484,9 +489,12 @@ class EOutput(object): sys.stderr.flush() def _write(self, f, s): - if sys.hexversion < 0x3000000 and isinstance(s, unicode): - # avoid potential UnicodeEncodeError - s = s.encode(_encodings['stdio'], 'backslashreplace') + # avoid potential UnicodeEncodeError + s = _unicode_encode(s, + encoding=_encodings['stdio'], errors='backslashreplace') + f = sys.stderr + if sys.hexversion >= 0x3000000: + f = f.buffer f.write(s) f.flush() -- cgit v1.2.3-1-g7c22