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
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):
"""
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)
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
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):
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()
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()