summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2009-10-27 22:55:54 +0000
committerZac Medico <zmedico@gentoo.org>2009-10-27 22:55:54 +0000
commitb3670f856ad211e43d5d1c8434877ad1a35eab4b (patch)
treebf1ea5293215b31e25ba6c0426bf94a13c1fd666
parentAdd a reference to bug #141118 inside _expand_new_virtuals(). (trunk r14732) (diff)
downloadportage-multirepo-b3670f856ad211e43d5d1c8434877ad1a35eab4b.tar.gz
portage-multirepo-b3670f856ad211e43d5d1c8434877ad1a35eab4b.tar.bz2
portage-multirepo-b3670f856ad211e43d5d1c8434877ad1a35eab4b.zip
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
-rw-r--r--pym/_emerge/JobStatusDisplay.py14
-rw-r--r--pym/portage/elog/messages.py11
-rw-r--r--pym/portage/output.py34
-rw-r--r--pym/portage/util.py9
4 files changed, 41 insertions, 27 deletions
diff --git a/pym/_emerge/JobStatusDisplay.py b/pym/_emerge/JobStatusDisplay.py
index 1c80c5ff..bcc682b2 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 a563ad27..b2a2dc4c 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 6044f2bb..6d4e108a 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 3efc2156..c6e0a312 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()