From 8cc8d12a674ab6271183e5c35202263a36497279 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Sun, 10 Jul 2011 16:26:24 -0700 Subject: Migrate from codecs.open() to io.open(). The io.open() function is the same as the built-in open() function in python3, and its implementation is optimized in python-2.7 and later. In addition to the possible performance improvement, this also allows us to avoid any future compatibility issues with codecs.open() that may arise if it is delegated to the built-in open() function as discussed in PEP 400. The main caveat involved with io.open() is that TextIOWrapper.write() raises TypeError if given raw bytes, unlike the streams returned from codecs.open(). This is mainly an issue for python2 since literal strings are raw bytes. We handle this by wrapping TextIOWrapper.write() arguments with our _unicode_decode() function. Also, the atomic_ofstream class overrides the write() method in python2 so that it performs automatic coercion to unicode when necessary. --- pym/portage/elog/messages.py | 7 +++---- pym/portage/elog/mod_save.py | 8 ++++---- pym/portage/elog/mod_save_summary.py | 16 +++++++++------- 3 files changed, 16 insertions(+), 15 deletions(-) (limited to 'pym/portage/elog') diff --git a/pym/portage/elog/messages.py b/pym/portage/elog/messages.py index a8a3e9a5f..6c1580a37 100644 --- a/pym/portage/elog/messages.py +++ b/pym/portage/elog/messages.py @@ -1,5 +1,5 @@ # elog/messages.py - elog core functions -# Copyright 2006-2009 Gentoo Foundation +# Copyright 2006-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 import portage @@ -15,7 +15,7 @@ from portage import _encodings from portage import _unicode_encode from portage import _unicode_decode -import codecs +import io import sys def collect_ebuild_messages(path): @@ -43,7 +43,7 @@ def collect_ebuild_messages(path): logentries[msgfunction] = [] lastmsgtype = None msgcontent = [] - for l in codecs.open(_unicode_encode(filename, + for l in io.open(_unicode_encode(filename, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], errors='replace'): if not l: @@ -167,7 +167,6 @@ def _make_msgfunction(level, color): _elog_base(level, msg, phase=phase, key=key, color=color, out=out) return _elog -import sys for f in _functions: setattr(sys.modules[__name__], f, _make_msgfunction(_functions[f][0], _functions[f][1])) del f, _functions diff --git a/pym/portage/elog/mod_save.py b/pym/portage/elog/mod_save.py index ac21fb8c5..0f0979466 100644 --- a/pym/portage/elog/mod_save.py +++ b/pym/portage/elog/mod_save.py @@ -1,8 +1,8 @@ # elog/mod_save.py - elog dispatch module -# Copyright 2006-2007 Gentoo Foundation +# Copyright 2006-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 -import codecs +import io import time from portage import os from portage import _encodings @@ -34,10 +34,10 @@ def process(mysettings, key, logentries, fulltext): ensure_dirs(os.path.dirname(elogfilename), uid=portage_uid, gid=portage_gid, mode=0o2770) - elogfile = codecs.open(_unicode_encode(elogfilename, + elogfile = io.open(_unicode_encode(elogfilename, encoding=_encodings['fs'], errors='strict'), mode='w', encoding=_encodings['content'], errors='backslashreplace') - elogfile.write(fulltext) + elogfile.write(_unicode_decode(fulltext)) elogfile.close() return elogfilename diff --git a/pym/portage/elog/mod_save_summary.py b/pym/portage/elog/mod_save_summary.py index ea8233fda..8970f06d0 100644 --- a/pym/portage/elog/mod_save_summary.py +++ b/pym/portage/elog/mod_save_summary.py @@ -1,8 +1,8 @@ # elog/mod_save_summary.py - elog dispatch module -# Copyright 2006-2007 Gentoo Foundation +# Copyright 2006-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 -import codecs +import io import time from portage import os from portage import _encodings @@ -21,7 +21,7 @@ def process(mysettings, key, logentries, fulltext): # TODO: Locking elogfilename = elogdir+"/summary.log" - elogfile = codecs.open(_unicode_encode(elogfilename, + elogfile = io.open(_unicode_encode(elogfilename, encoding=_encodings['fs'], errors='strict'), mode='a', encoding=_encodings['content'], errors='backslashreplace') apply_permissions(elogfilename, mode=0o60, mask=0) @@ -30,10 +30,12 @@ def process(mysettings, key, logentries, fulltext): # Avoid potential UnicodeDecodeError later. time_str = _unicode_decode(time_str, encoding=_encodings['content'], errors='replace') - elogfile.write(_(">>> Messages generated by process %(pid)d on %(time)s for package %(pkg)s:\n\n") % - {"pid": os.getpid(), "time": time_str, "pkg": key}) - elogfile.write(fulltext) - elogfile.write("\n") + elogfile.write(_unicode_decode( + _(">>> Messages generated by process " + + "%(pid)d on %(time)s for package %(pkg)s:\n\n") % + {"pid": os.getpid(), "time": time_str, "pkg": key})) + elogfile.write(_unicode_decode(fulltext)) + elogfile.write(_unicode_decode("\n")) elogfile.close() return elogfilename -- cgit v1.2.3-1-g7c22