From b3670f856ad211e43d5d1c8434877ad1a35eab4b Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Tue, 27 Oct 2009 22:55:54 +0000 Subject: Bug #290625 - Manually encode output to stdout in python3, in order to avoid potential UnicodeEncodeError exceptions. (trunk r14734) svn path=/main/branches/2.1.7/; revision=14741 --- pym/_emerge/JobStatusDisplay.py | 14 +++++++++----- pym/portage/elog/messages.py | 11 ++++++----- pym/portage/output.py | 34 +++++++++++++++++++++------------- pym/portage/util.py | 9 +++++---- 4 files changed, 41 insertions(+), 27 deletions(-) diff --git a/pym/_emerge/JobStatusDisplay.py b/pym/_emerge/JobStatusDisplay.py index 1c80c5ffa..bcc682b23 100644 --- a/pym/_emerge/JobStatusDisplay.py +++ b/pym/_emerge/JobStatusDisplay.py @@ -17,6 +17,7 @@ import portage from portage import os from portage import _encodings from portage import _unicode_decode +from portage import _unicode_encode from portage.output import xtermTitle from _emerge.getloadavg import getloadavg @@ -75,11 +76,14 @@ class JobStatusDisplay(object): return sys.stdout def _write(self, s): - if sys.hexversion < 0x3000000 and isinstance(s, unicode): - # avoid potential UnicodeEncodeError - s = s.encode(_encodings['stdio'], 'backslashreplace') - self.out.write(s) - self.out.flush() + # avoid potential UnicodeEncodeError + s = _unicode_encode(s, + encoding=_encodings['stdio'], errors='backslashreplace') + out = self.out + if sys.hexversion >= 0x3000000: + out = out.buffer + out.write(s) + out.flush() def _init_term(self): """ diff --git a/pym/portage/elog/messages.py b/pym/portage/elog/messages.py index a563ad271..b2a2dc4cf 100644 --- a/pym/portage/elog/messages.py +++ b/pym/portage/elog/messages.py @@ -96,11 +96,12 @@ def _elog_base(level, msg, phase="other", key=None, color=None, out=None): formatted_msg = colorize(color, " * ") + msg + "\n" - if sys.hexversion < 0x3000000 and \ - out in (sys.stdout, sys.stderr) and isinstance(formatted_msg, unicode): - # avoid potential UnicodeEncodeError - formatted_msg = formatted_msg.encode( - _encodings['stdio'], 'backslashreplace') + # avoid potential UnicodeEncodeError + if out in (sys.stdout, sys.stderr): + formatted_msg = _unicode_encode(formatted_msg, + encoding=_encodings['stdio'], errors='backslashreplace') + if sys.hexversion >= 0x3000000: + out = out.buffer out.write(formatted_msg) 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() diff --git a/pym/portage/util.py b/pym/portage/util.py index 3efc2156a..c6e0a3120 100644 --- a/pym/portage/util.py +++ b/pym/portage/util.py @@ -67,10 +67,11 @@ def writemsg(mystr,noiselevel=0,fd=None): if fd is None: fd = sys.stderr if noiselevel <= noiselimit: - if sys.hexversion < 0x3000000: - # avoid potential UnicodeEncodeError - mystr = _unicode_encode(mystr, - encoding=_encodings['stdio'], errors='backslashreplace') + # avoid potential UnicodeEncodeError + mystr = _unicode_encode(mystr, + encoding=_encodings['stdio'], errors='backslashreplace') + if sys.hexversion >= 0x3000000: + fd = fd.buffer fd.write(mystr) fd.flush() -- cgit v1.2.3-1-g7c22