diff options
author | Zac Medico <zmedico@gentoo.org> | 2009-09-20 22:23:58 +0000 |
---|---|---|
committer | Zac Medico <zmedico@gentoo.org> | 2009-09-20 22:23:58 +0000 |
commit | a0093ae8eea62c089bf41ae6dc31eddb1619ad76 (patch) | |
tree | e9d13f9c1ea842e0fde05bd8ec95781583f54c49 | |
parent | d76337003072f96a2aada7b72715efbb80a3ca3b (diff) | |
download | portage-a0093ae8eea62c089bf41ae6dc31eddb1619ad76.tar.gz portage-a0093ae8eea62c089bf41ae6dc31eddb1619ad76.tar.bz2 portage-a0093ae8eea62c089bf41ae6dc31eddb1619ad76.zip |
Add support in ConsoleStyleFile.write() for unicode encoding when writing to
stdout/stderr in python-2.x.
svn path=/main/trunk/; revision=14303
-rw-r--r-- | pym/portage/output.py | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/pym/portage/output.py b/pym/portage/output.py index c21dc07ba..570de78e9 100644 --- a/pym/portage/output.py +++ b/pym/portage/output.py @@ -362,14 +362,24 @@ class ConsoleStyleFile(object): s = _unicode_decode(s) global havecolor if havecolor and self._styles: + styled_s = [] for style in self._styles: - self._file.write(style_to_ansi_code(style)) - self._file.write(s) - self._file.write(codes["reset"]) + styled_s.append(style_to_ansi_code(style)) + styled_s.append(s) + styled_s.append(codes["reset"]) + self._write(self._file, "".join(styled_s)) else: - self._file.write(s) + self._write(self._file, s) if self.write_listener: - self.write_listener.write(s) + 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') + f.write(s) def writelines(self, lines): for s in lines: |