aboutsummaryrefslogtreecommitdiff
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/portage/cache
parentManifest: fix NameError in updateAllHashes (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/portage/cache')
-rw-r--r--pym/portage/cache/flat_hash.py19
-rw-r--r--pym/portage/cache/flat_list.py18
2 files changed, 25 insertions, 12 deletions
diff --git a/pym/portage/cache/flat_hash.py b/pym/portage/cache/flat_hash.py
index 6be0fe4b2..b6bc0744e 100644
--- a/pym/portage/cache/flat_hash.py
+++ b/pym/portage/cache/flat_hash.py
@@ -1,21 +1,26 @@
-# Copyright: 2005 Gentoo Foundation
+# Copyright: 2005-2011 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
# Author(s): Brian Harring (ferringb@gentoo.org)
-# License: GPL2
-import codecs
from portage.cache import fs_template
from portage.cache import cache_errors
import errno
+import io
import stat
import sys
import os as _os
from portage import os
from portage import _encodings
+from portage import _unicode_decode
from portage import _unicode_encode
if sys.hexversion >= 0x3000000:
long = int
+# Coerce to unicode, in order to prevent TypeError when writing
+# raw bytes to TextIOWrapper with python2.
+_setitem_fmt = _unicode_decode("%s=%s\n")
+
class database(fs_template.FsBased):
autocommits = True
@@ -35,7 +40,7 @@ class database(fs_template.FsBased):
# Don't use os.path.join, for better performance.
fp = self.location + _os.sep + cpv
try:
- myf = codecs.open(_unicode_encode(fp,
+ myf = io.open(_unicode_encode(fp,
encoding=_encodings['fs'], errors='strict'),
mode='r', encoding=_encodings['repo.content'],
errors='replace')
@@ -68,7 +73,7 @@ class database(fs_template.FsBased):
s = cpv.rfind("/")
fp = os.path.join(self.location,cpv[:s],".update.%i.%s" % (os.getpid(), cpv[s+1:]))
try:
- myf = codecs.open(_unicode_encode(fp,
+ myf = io.open(_unicode_encode(fp,
encoding=_encodings['fs'], errors='strict'),
mode='w', encoding=_encodings['repo.content'],
errors='backslashreplace')
@@ -76,7 +81,7 @@ class database(fs_template.FsBased):
if errno.ENOENT == e.errno:
try:
self._ensure_dirs(cpv)
- myf = codecs.open(_unicode_encode(fp,
+ myf = io.open(_unicode_encode(fp,
encoding=_encodings['fs'], errors='strict'),
mode='w', encoding=_encodings['repo.content'],
errors='backslashreplace')
@@ -90,7 +95,7 @@ class database(fs_template.FsBased):
v = values.get(k)
if not v:
continue
- myf.write("%s=%s\n" % (k, v))
+ myf.write(_setitem_fmt % (k, v))
finally:
myf.close()
self._ensure_access(fp)
diff --git a/pym/portage/cache/flat_list.py b/pym/portage/cache/flat_list.py
index eb7558398..728830753 100644
--- a/pym/portage/cache/flat_list.py
+++ b/pym/portage/cache/flat_list.py
@@ -1,16 +1,24 @@
+# Copyright 2005-2011 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
from portage.cache import fs_template
from portage.cache import cache_errors
from portage import os
from portage import _encodings
+from portage import _unicode_decode
from portage import _unicode_encode
-import codecs
import errno
+import io
import stat
import sys
if sys.hexversion >= 0x3000000:
long = int
+# Coerce to unicode, in order to prevent TypeError when writing
+# raw bytes to TextIOWrapper with python2.
+_setitem_fmt = _unicode_decode("%s\n")
+
# store the current key order *here*.
class database(fs_template.FsBased):
@@ -36,7 +44,7 @@ class database(fs_template.FsBased):
def _getitem(self, cpv):
d = {}
try:
- myf = codecs.open(_unicode_encode(os.path.join(self.location, cpv),
+ myf = io.open(_unicode_encode(os.path.join(self.location, cpv),
encoding=_encodings['fs'], errors='strict'),
mode='r', encoding=_encodings['repo.content'],
errors='replace')
@@ -60,7 +68,7 @@ class database(fs_template.FsBased):
s = cpv.rfind("/")
fp=os.path.join(self.location,cpv[:s],".update.%i.%s" % (os.getpid(), cpv[s+1:]))
try:
- myf = codecs.open(_unicode_encode(fp,
+ myf = io.open(_unicode_encode(fp,
encoding=_encodings['fs'], errors='strict'),
mode='w', encoding=_encodings['repo.content'],
errors='backslashreplace')
@@ -68,7 +76,7 @@ class database(fs_template.FsBased):
if errno.ENOENT == e.errno:
try:
self._ensure_dirs(cpv)
- myf = codecs.open(_unicode_encode(fp,
+ myf = io.open(_unicode_encode(fp,
encoding=_encodings['fs'], errors='strict'),
mode='w', encoding=_encodings['repo.content'],
errors='backslashreplace')
@@ -79,7 +87,7 @@ class database(fs_template.FsBased):
for x in self.auxdbkey_order:
- myf.write(values.get(x,"")+"\n")
+ myf.write(_setitem_fmt % (values.get(x, ""),))
myf.close()
self._ensure_access(fp, mtime=values["_mtime_"])