Add unicode conversions in various logging code.
authorZac Medico <zmedico@gentoo.org>
Thu, 6 Aug 2009 09:18:49 +0000 (09:18 -0000)
committerZac Medico <zmedico@gentoo.org>
Thu, 6 Aug 2009 09:18:49 +0000 (09:18 -0000)
svn path=/main/trunk/; revision=13931

pym/_emerge/Scheduler.py
pym/_emerge/emergelog.py
pym/portage/elog/messages.py

index 0ad91675f4a15a75bb62c4e1c61ca5d44ff56cee..ec6135d32d566d9be6afadc028047ece6c460335 100644 (file)
@@ -477,6 +477,10 @@ class Scheduler(PollScheduler):
                return self._pkg(cpv, type_name, root_config, installed=installed)
 
        def _append_to_log_path(self, log_path, msg):
+
+               if not isinstance(msg, unicode):
+                       msg = unicode(msg, encoding='utf_8', errors='replace')
+
                f = codecs.open(log_path, mode='a',
                        encoding='utf_8', errors='replace')
                try:
index 1469f5265ccc4958acda7bd133a103ff495269f9..1a26cc9fa5aae70f2fc33cc62ccd81a27afed135 100644 (file)
@@ -19,6 +19,13 @@ from portage.output import xtermTitle
 _emerge_log_dir = '/var/log'
 
 def emergelog(xterm_titles, mystr, short_msg=None):
+
+       if not isinstance(mystr, unicode):
+               mystr = unicode(mystr, encoding='utf_8', errors='replace')
+
+       if short_msg is not None and not isinstance(short_msg, unicode):
+               short_msg = unicode(short_msg, encoding='utf_8', errors='replace')
+
        if xterm_titles and short_msg:
                if "HOSTNAME" in os.environ:
                        short_msg = os.environ["HOSTNAME"]+": "+short_msg
index 9832f26600f3b1bd3e7fed98b24b201e697bbcb8..3b44e0fe812f6ab8458ce10679ff9eee17f02a41 100644 (file)
@@ -79,15 +79,23 @@ def _elog_base(level, msg, phase="other", key=None, color=None, out=None):
 
        global _msgbuffer
 
-       if color == None:
+       if out is None:
+               out = sys.stdout
+
+       if color is None:
                color = "GOOD"
 
+       if not isinstance(msg, unicode):
+               msg = unicode(msg, encoding='utf_8', errors='replace')
+
        formatted_msg = colorize(color, " * ") + msg + "\n"
 
-       if out is None:
-               sys.stdout.write(formatted_msg)
-       else:
-               out.write(formatted_msg)
+       if sys.hexversion < 0x3000000 and \
+               out in (sys.stdout, sys.stderr) and isinstance(formatted_msg, unicode):
+               # avoid potential UnicodeEncodeError
+               formatted_msg = formatted_msg.encode('utf_8', 'replace')
+
+       out.write(formatted_msg)
 
        if key not in _msgbuffer:
                _msgbuffer[key] = {}