Bug #290625 - Manually encode output to stdout in python3, in order to avoid
authorZac Medico <zmedico@gentoo.org>
Tue, 27 Oct 2009 22:55:54 +0000 (22:55 -0000)
committerZac Medico <zmedico@gentoo.org>
Tue, 27 Oct 2009 22:55:54 +0000 (22:55 -0000)
potential UnicodeEncodeError exceptions. (trunk r14734)

svn path=/main/branches/2.1.7/; revision=14741

pym/_emerge/JobStatusDisplay.py
pym/portage/elog/messages.py
pym/portage/output.py
pym/portage/util.py

index 1c80c5ffa0a699e6fb1a06883f16cd82d98a4c06..bcc682b234308642570732cdb3421c0168eb8a9c 100644 (file)
@@ -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):
                """
index a563ad271d3c3606961cc976053864a5dbe2c97a..b2a2dc4cf4c2106acd63fcf11220e4b63281c732 100644 (file)
@@ -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)
 
index 6044f2bbc366d56ebc4ce3e4aeb255e045452383..6d4e108aa3674346c78b9bf56043c63341aa54bf 100644 (file)
@@ -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()
 
index 3efc2156af8aa2449683d4d1821fa04ef9fc0900..c6e0a31206b6b35565049131b6ca6449844a3518 100644 (file)
@@ -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()