From 3d11b48d840be203945ff86e46a36fd813008d27 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Fri, 21 Aug 2009 04:03:15 +0000 Subject: [PATCH] Use _encodings where appropriate and add _encodings['stdio'] for stdout encoding. svn path=/main/trunk/; revision=14111 --- pym/_emerge/JobStatusDisplay.py | 3 ++- pym/portage/__init__.py | 1 + pym/portage/elog/messages.py | 15 ++++++++++----- pym/portage/elog/mod_save.py | 7 +++++-- pym/portage/elog/mod_save_summary.py | 7 +++++-- pym/portage/elog/mod_syslog.py | 4 +++- pym/portage/output.py | 9 ++++----- pym/portage/util.py | 3 ++- 8 files changed, 32 insertions(+), 17 deletions(-) diff --git a/pym/_emerge/JobStatusDisplay.py b/pym/_emerge/JobStatusDisplay.py index 6aa2d99b7..9218a15ac 100644 --- a/pym/_emerge/JobStatusDisplay.py +++ b/pym/_emerge/JobStatusDisplay.py @@ -13,6 +13,7 @@ except ImportError: import portage from portage import os +from portage import _encodings from portage.output import xtermTitle from _emerge.getloadavg import getloadavg @@ -70,7 +71,7 @@ class JobStatusDisplay(object): def _write(self, s): if sys.hexversion < 0x3000000 and isinstance(s, unicode): # avoid potential UnicodeEncodeError - s = portage._unicode_encode(s) + s = s.encode(_encodings['stdio'], errors='backslashreplace') self.out.write(s) self.out.flush() diff --git a/pym/portage/__init__.py b/pym/portage/__init__.py index 03a093654..5fefa505c 100644 --- a/pym/portage/__init__.py +++ b/pym/portage/__init__.py @@ -125,6 +125,7 @@ _encodings = { 'fs' : 'utf_8', 'merge' : sys.getfilesystemencoding(), 'repo.content' : 'utf_8', + 'stdio' : 'utf_8', } # This can happen if python is built with USE=build (stage 1). diff --git a/pym/portage/elog/messages.py b/pym/portage/elog/messages.py index a51f0864e..a563ad271 100644 --- a/pym/portage/elog/messages.py +++ b/pym/portage/elog/messages.py @@ -12,6 +12,9 @@ portage.proxy.lazyimport.lazyimport(globals(), from portage.const import EBUILD_PHASES from portage.localization import _ from portage import os +from portage import _encodings +from portage import _unicode_encode +from portage import _unicode_decode import codecs import sys @@ -41,8 +44,9 @@ def collect_ebuild_messages(path): logentries[msgfunction] = [] lastmsgtype = None msgcontent = [] - for l in codecs.open(filename, mode='r', - encoding='utf_8', errors='replace'): + for l in codecs.open(_unicode_encode(filename, + encoding=_encodings['fs'], errors='strict'), + mode='r', encoding=_encodings['repo.content'], errors='replace'): if not l: continue try: @@ -87,15 +91,16 @@ def _elog_base(level, msg, phase="other", key=None, color=None, out=None): if color is None: color = "GOOD" - if not isinstance(msg, unicode): - msg = unicode(msg, encoding='utf_8', errors='replace') + msg = _unicode_decode(msg, + encoding=_encodings['content'], errors='replace') 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('utf_8', 'replace') + formatted_msg = formatted_msg.encode( + _encodings['stdio'], 'backslashreplace') out.write(formatted_msg) diff --git a/pym/portage/elog/mod_save.py b/pym/portage/elog/mod_save.py index 1a11fd927..0707b00f8 100644 --- a/pym/portage/elog/mod_save.py +++ b/pym/portage/elog/mod_save.py @@ -6,6 +6,8 @@ import codecs import time from portage import os +from portage import _encodings +from portage import _unicode_encode from portage.data import portage_uid, portage_gid from portage.util import ensure_dirs @@ -19,8 +21,9 @@ def process(mysettings, key, logentries, fulltext): ensure_dirs(elogdir, uid=portage_uid, gid=portage_gid, mode=02770) elogfilename = elogdir+"/"+path+":"+time.strftime("%Y%m%d-%H%M%S", time.gmtime(time.time()))+".log" - elogfile = codecs.open(elogfilename, mode='w', - encoding='utf_8', errors='replace') + elogfile = codecs.open(_unicode_encode(elogfilename, + encoding=_encodings['fs'], errors='strict'), + mode='w', encoding=_encodings['content'], errors='backslashreplace') elogfile.write(fulltext) elogfile.close() diff --git a/pym/portage/elog/mod_save_summary.py b/pym/portage/elog/mod_save_summary.py index 421fd29ed..f35c71c83 100644 --- a/pym/portage/elog/mod_save_summary.py +++ b/pym/portage/elog/mod_save_summary.py @@ -6,6 +6,8 @@ import codecs import time from portage import os +from portage import _encodings +from portage import _unicode_encode from portage.data import portage_uid, portage_gid from portage.localization import _ from portage.util import ensure_dirs, apply_permissions @@ -19,8 +21,9 @@ def process(mysettings, key, logentries, fulltext): # TODO: Locking elogfilename = elogdir+"/summary.log" - elogfile = codecs.open(elogfilename, mode='a', - encoding='utf_8', errors='replace') + elogfile = codecs.open(_unicode_encode(elogfilename, + encoding=_encodings['fs'], errors='strict'), + mode='a', encoding=_encodings['content'], errors='backslashreplace') apply_permissions(elogfilename, mode=060, mask=0) elogfile.write(_(">>> Messages generated by process %(pid)d on %(time)s for package %(pkg)s:\n\n") % {"pid": os.getpid(), "time": time.strftime("%Y-%m-%d %H:%M:%S %Z", time.localtime(time.time())), "pkg": key}) diff --git a/pym/portage/elog/mod_syslog.py b/pym/portage/elog/mod_syslog.py index 0fe205644..d7e955f81 100644 --- a/pym/portage/elog/mod_syslog.py +++ b/pym/portage/elog/mod_syslog.py @@ -6,6 +6,7 @@ import sys import syslog from portage.const import EBUILD_PHASES +from portage import _encodings _pri = { "INFO" : syslog.LOG_INFO, @@ -25,6 +26,7 @@ def process(mysettings, key, logentries, fulltext): msgtext = "%s: %s: %s" % (key, phase, msgtext) if sys.hexversion < 0x3000000 and isinstance(msgtext, unicode): # Avoid TypeError from syslog.syslog() - msgtext = msgtext.encode('utf_8', 'replace') + msgtext = msgtext.encode(_encodings['content'], + 'backslashreplace') syslog.syslog(_pri[msgtype], msgtext) syslog.closelog() diff --git a/pym/portage/output.py b/pym/portage/output.py index 6ebbc0bdf..0e016c5c6 100644 --- a/pym/portage/output.py +++ b/pym/portage/output.py @@ -17,8 +17,7 @@ portage.proxy.lazyimport.lazyimport(globals(), ) from portage import os -from portage import _content_encoding -from portage import _fs_encoding +from portage import _encodings from portage import _unicode_encode from portage.const import COLOR_MAP_FILE from portage.exception import CommandNotFound, FileNotFound, \ @@ -169,8 +168,8 @@ def _parse_color_map(config_root='/', onerror=None): try: lineno=0 for line in codecs.open(_unicode_encode(myfile, - encoding=_fs_encoding, errors='strict'), - mode='r', encoding=_content_encoding, errors='replace'): + encoding=_encodings['fs'], errors='strict'), + mode='r', encoding=_encodings['content'], errors='replace'): lineno += 1 commenter_pos = line.find("#") @@ -470,7 +469,7 @@ class EOutput(object): def _write(self, f, s): if sys.hexversion < 0x3000000 and isinstance(s, unicode): # avoid potential UnicodeEncodeError - s = s.encode(_content_encoding, 'replace') + s = s.encode(_encodings['stdio'], 'backslashreplace') f.write(s) f.flush() diff --git a/pym/portage/util.py b/pym/portage/util.py index 036191040..6f11934e8 100644 --- a/pym/portage/util.py +++ b/pym/portage/util.py @@ -63,7 +63,8 @@ def writemsg(mystr,noiselevel=0,fd=None): if noiselevel <= noiselimit: if sys.hexversion < 0x3000000: # avoid potential UnicodeEncodeError - mystr = _unicode_encode(mystr) + mystr = _unicode_encode(mystr, + encoding=_encodings['stdio'], errors='backslashreplace') fd.write(mystr) fd.flush() -- 2.26.2