summaryrefslogtreecommitdiffstats
path: root/pym/_emerge/emergelog.py
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2011-07-10 16:26:24 -0700
committerZac Medico <zmedico@gentoo.org>2011-07-10 16:55:05 -0700
commit8cc8d12a674ab6271183e5c35202263a36497279 (patch)
tree22365e2c613d04151a2d2da5ff3e25e37f84c554 /pym/_emerge/emergelog.py
parent906b62b24d8a845356d59abc5acd39db2174ce0f (diff)
downloadportage-8cc8d12a674ab6271183e5c35202263a36497279.tar.gz
portage-8cc8d12a674ab6271183e5c35202263a36497279.tar.bz2
portage-8cc8d12a674ab6271183e5c35202263a36497279.zip
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.
Diffstat (limited to 'pym/_emerge/emergelog.py')
-rw-r--r--pym/_emerge/emergelog.py8
1 files changed, 5 insertions, 3 deletions
diff --git a/pym/_emerge/emergelog.py b/pym/_emerge/emergelog.py
index 9cac3b222..a195c6f84 100644
--- a/pym/_emerge/emergelog.py
+++ b/pym/_emerge/emergelog.py
@@ -3,12 +3,13 @@
from __future__ import print_function
-import codecs
+import io
import sys
import time
import portage
from portage import os
from portage import _encodings
+from portage import _unicode_decode
from portage import _unicode_encode
from portage.data import secpass
from portage.output import xtermTitle
@@ -36,7 +37,7 @@ def emergelog(xterm_titles, mystr, short_msg=None):
try:
file_path = os.path.join(_emerge_log_dir, 'emerge.log')
existing_log = os.path.isfile(file_path)
- mylogfile = codecs.open(_unicode_encode(file_path,
+ mylogfile = io.open(_unicode_encode(file_path,
encoding=_encodings['fs'], errors='strict'),
mode='a', encoding=_encodings['content'],
errors='backslashreplace')
@@ -50,7 +51,8 @@ def emergelog(xterm_titles, mystr, short_msg=None):
# seek because we may have gotten held up by the lock.
# if so, we may not be positioned at the end of the file.
mylogfile.seek(0, 2)
- mylogfile.write(str(time.time())[:10]+": "+mystr+"\n")
+ mylogfile.write(_unicode_decode(
+ str(time.time())[:10]+": "+mystr+"\n"))
mylogfile.flush()
finally:
if mylock: