From: Zac Medico Date: Sun, 10 Jul 2011 23:26:24 +0000 (-0700) Subject: Migrate from codecs.open() to io.open(). X-Git-Tag: v2.2.0_alpha44~25 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=8cc8d12a674ab6271183e5c35202263a36497279;p=portage.git 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. --- diff --git a/bin/binhost-snapshot b/bin/binhost-snapshot index 825a11672..9d2697d03 100755 --- a/bin/binhost-snapshot +++ b/bin/binhost-snapshot @@ -1,8 +1,8 @@ #!/usr/bin/python -# Copyright 2010 Gentoo Foundation +# Copyright 2010-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 -import codecs +import io import optparse import os import sys @@ -109,7 +109,7 @@ def main(argv): if not (os.WIFEXITED(ret) and os.WEXITSTATUS(ret) == os.EX_OK): return 1 - infile = codecs.open(portage._unicode_encode(src_pkgs_index, + infile = io.open(portage._unicode_encode(src_pkgs_index, encoding=portage._encodings['fs'], errors='strict'), mode='r', encoding=portage._encodings['repo.content'], errors='strict') diff --git a/bin/egencache b/bin/egencache index 5307cd5a2..1b4265df1 100755 --- a/bin/egencache +++ b/bin/egencache @@ -20,7 +20,7 @@ try: except KeyboardInterrupt: sys.exit(128 + signal.SIGINT) -import codecs +import io import logging import optparse import subprocess @@ -391,10 +391,10 @@ class GenUseLocalDesc(object): output = open(_unicode_encode(desc_path, encoding=_encodings['fs'], errors='strict'), 'r+b') else: - output = codecs.open(_unicode_encode(desc_path, + output = io.open(_unicode_encode(desc_path, encoding=_encodings['fs'], errors='strict'), mode='w', encoding=_encodings['repo.content'], - errors='replace') + errors='backslashreplace') except IOError as e: if not self._preserve_comments or \ os.path.isfile(desc_path): @@ -413,10 +413,10 @@ class GenUseLocalDesc(object): level=logging.WARNING, noiselevel=-1) self._preserve_comments = False try: - output = codecs.open(_unicode_encode(desc_path, + output = io.open(_unicode_encode(desc_path, encoding=_encodings['fs'], errors='strict'), mode='w', encoding=_encodings['repo.content'], - errors='replace') + errors='backslashreplace') except IOError as e: writemsg_level( "ERROR: failed to open output file %s: %s\n" \ @@ -437,18 +437,18 @@ class GenUseLocalDesc(object): # Finished probing comments in binary mode, now append # in text mode. - output = codecs.open(_unicode_encode(desc_path, + output = io.open(_unicode_encode(desc_path, encoding=_encodings['fs'], errors='strict'), mode='a', encoding=_encodings['repo.content'], - errors='replace') - output.write('\n') + errors='backslashreplace') + output.write(_unicode_decode('\n')) else: - output.write(''' + output.write(_unicode_decode(''' # This file is deprecated as per GLEP 56 in favor of metadata.xml. Please add # your descriptions to your package's metadata.xml ONLY. # * generated automatically using egencache * -'''.lstrip()) +'''.lstrip())) # The cmp function no longer exists in python3, so we'll # implement our own here under a slightly different name @@ -522,7 +522,8 @@ class GenUseLocalDesc(object): resatoms = sorted(reskeys, key=cmp_sort_key(atomcmp)) resdesc = resdict[reskeys[resatoms[-1]]] - output.write('%s:%s - %s\n' % (cp, flag, resdesc)) + output.write(_unicode_decode( + '%s:%s - %s\n' % (cp, flag, resdesc))) output.close() @@ -609,9 +610,9 @@ class GenChangeLogs(object): def generate_changelog(self, cp): try: - output = codecs.open('ChangeLog', + output = io.open('ChangeLog', mode='w', encoding=_encodings['repo.content'], - errors='replace') + errors='backslashreplace') except IOError as e: writemsg_level( "ERROR: failed to open ChangeLog for %s: %s\n" % (cp,e,), @@ -619,7 +620,7 @@ class GenChangeLogs(object): self.returncode |= 2 return - output.write((''' + output.write(_unicode_decode(''' # ChangeLog for %s # Copyright 1999-%s Gentoo Foundation; Distributed under the GPL v2 # $Header: $ @@ -688,10 +689,11 @@ class GenChangeLogs(object): # Reverse the sort order for headers. for c in reversed(changed): if c.startswith('+') and c.endswith('.ebuild'): - output.write('*%s (%s)\n' % (c[1:-7], date)) + output.write(_unicode_decode( + '*%s (%s)\n' % (c[1:-7], date))) wroteheader = True if wroteheader: - output.write('\n') + output.write(_unicode_decode('\n')) # strip ': ', '[] ', and similar body[0] = re.sub(r'^\W*' + re.escape(cp) + r'\W+', '', body[0]) @@ -711,10 +713,13 @@ class GenChangeLogs(object): # don't break filenames on hyphens self._wrapper.break_on_hyphens = False - output.write(self._wrapper.fill('%s; %s %s:' % (date, author, ', '.join(changed)))) + output.write(_unicode_decode( + self._wrapper.fill( + '%s; %s %s:' % (date, author, ', '.join(changed))))) # but feel free to break commit messages there self._wrapper.break_on_hyphens = True - output.write('\n%s\n\n' % '\n'.join([self._wrapper.fill(x) for x in body])) + output.write(_unicode_decode( + '\n%s\n\n' % '\n'.join(self._wrapper.fill(x) for x in body))) output.close() diff --git a/bin/repoman b/bin/repoman index d1d393a82..3e0203681 100755 --- a/bin/repoman +++ b/bin/repoman @@ -9,10 +9,10 @@ from __future__ import print_function import calendar -import codecs import copy import errno import formatter +import io import logging import optparse import re @@ -700,7 +700,7 @@ for path in portdb.porttrees: desc_path = os.path.join(path, 'profiles', 'profiles.desc') try: - desc_file = codecs.open(_unicode_encode(desc_path, + desc_file = io.open(_unicode_encode(desc_path, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], errors='replace') except EnvironmentError: @@ -1209,7 +1209,7 @@ for x in scanlist: continue try: line = 1 - for l in codecs.open(_unicode_encode(os.path.join(checkdir, y), + for l in io.open(_unicode_encode(os.path.join(checkdir, y), encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content']): line +=1 @@ -1822,7 +1822,7 @@ for x in scanlist: pkg.mtime = None try: # All ebuilds should have utf_8 encoding. - f = codecs.open(_unicode_encode(full_path, + f = io.open(_unicode_encode(full_path, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content']) try: @@ -2319,7 +2319,7 @@ else: commitmessage = options.commitmsg if options.commitmsgfile: try: - f = codecs.open(_unicode_encode(options.commitmsgfile, + f = io.open(_unicode_encode(options.commitmsgfile, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], errors='replace') commitmessage = f.read() diff --git a/pym/_emerge/Binpkg.py b/pym/_emerge/Binpkg.py index 84eda21ba..b83341941 100644 --- a/pym/_emerge/Binpkg.py +++ b/pym/_emerge/Binpkg.py @@ -14,8 +14,9 @@ from portage.util import writemsg import portage from portage import os from portage import _encodings +from portage import _unicode_decode from portage import _unicode_encode -import codecs +import io import logging from portage.output import colorize @@ -239,20 +240,22 @@ class Binpkg(CompositeTask): else: continue - f = codecs.open(_unicode_encode(os.path.join(infloc, k), + f = io.open(_unicode_encode(os.path.join(infloc, k), encoding=_encodings['fs'], errors='strict'), - mode='w', encoding=_encodings['content'], errors='replace') + mode='w', encoding=_encodings['content'], + errors='backslashreplace') try: - f.write(v + "\n") + f.write(_unicode_decode(v + "\n")) finally: f.close() # Store the md5sum in the vdb. - f = codecs.open(_unicode_encode(os.path.join(infloc, 'BINPKGMD5'), + f = io.open(_unicode_encode(os.path.join(infloc, 'BINPKGMD5'), encoding=_encodings['fs'], errors='strict'), mode='w', encoding=_encodings['content'], errors='strict') try: - f.write(str(portage.checksum.perform_md5(pkg_path)) + "\n") + f.write(_unicode_decode( + str(portage.checksum.perform_md5(pkg_path)) + "\n")) finally: f.close() diff --git a/pym/_emerge/EbuildFetcher.py b/pym/_emerge/EbuildFetcher.py index 215024165..51d2f5a10 100644 --- a/pym/_emerge/EbuildFetcher.py +++ b/pym/_emerge/EbuildFetcher.py @@ -1,10 +1,11 @@ -# Copyright 1999-2010 Gentoo Foundation +# Copyright 1999-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 import traceback from _emerge.SpawnProcess import SpawnProcess import copy +import io import signal import sys import portage @@ -12,7 +13,6 @@ from portage import os from portage import _encodings from portage import _unicode_encode from portage import _unicode_decode -import codecs from portage.elog.messages import eerror from portage.package.ebuild.fetch import fetch from portage.util._pty import _create_pty_or_pipe @@ -160,12 +160,13 @@ class EbuildFetcher(SpawnProcess): # fetch code will be skipped, so we need to generate equivalent # output here. if self.logfile is not None: - f = codecs.open(_unicode_encode(self.logfile, + f = io.open(_unicode_encode(self.logfile, encoding=_encodings['fs'], errors='strict'), - mode='a', encoding=_encodings['content'], errors='replace') + mode='a', encoding=_encodings['content'], + errors='backslashreplace') for filename in uri_map: - f.write((' * %s size ;-) ...' % \ - filename).ljust(73) + '[ ok ]\n') + f.write(_unicode_decode((' * %s size ;-) ...' % \ + filename).ljust(73) + '[ ok ]\n')) f.close() return True diff --git a/pym/_emerge/EbuildMetadataPhase.py b/pym/_emerge/EbuildMetadataPhase.py index 284622d69..e53298bae 100644 --- a/pym/_emerge/EbuildMetadataPhase.py +++ b/pym/_emerge/EbuildMetadataPhase.py @@ -11,7 +11,7 @@ from portage import _encodings from portage import _unicode_decode from portage import _unicode_encode import fcntl -import codecs +import io class EbuildMetadataPhase(SubProcess): @@ -37,7 +37,7 @@ class EbuildMetadataPhase(SubProcess): if eapi is None and \ 'parse-eapi-ebuild-head' in settings.features: eapi = portage._parse_eapi_ebuild_head( - codecs.open(_unicode_encode(ebuild_path, + io.open(_unicode_encode(ebuild_path, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], errors='replace')) diff --git a/pym/_emerge/depgraph.py b/pym/_emerge/depgraph.py index d5971d1ba..d0b8fb722 100644 --- a/pym/_emerge/depgraph.py +++ b/pym/_emerge/depgraph.py @@ -3,10 +3,9 @@ from __future__ import print_function -import codecs import difflib import errno -import gc +import io import logging import re import stat @@ -5937,7 +5936,7 @@ class depgraph(object): def write_changes(root, changes, file_to_write_to): file_contents = None try: - file_contents = codecs.open( + file_contents = io.open( _unicode_encode(file_to_write_to, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], 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: diff --git a/pym/_emerge/resolver/output_helpers.py b/pym/_emerge/resolver/output_helpers.py index 874660f69..b7e73766c 100644 --- a/pym/_emerge/resolver/output_helpers.py +++ b/pym/_emerge/resolver/output_helpers.py @@ -7,7 +7,7 @@ in output.py __all__ = ( ) -import codecs +import io import re import sys @@ -502,7 +502,7 @@ def _calc_changelog(ebuildpath,current,next): next = next[:-3] changelogpath = os.path.join(os.path.split(ebuildpath)[0],'ChangeLog') try: - changelog = codecs.open(_unicode_encode(changelogpath, + changelog = io.open(_unicode_encode(changelogpath, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], errors='replace' ).read() 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_"]) diff --git a/pym/portage/cvstree.py b/pym/portage/cvstree.py index de580f57e..9ba22f315 100644 --- a/pym/portage/cvstree.py +++ b/pym/portage/cvstree.py @@ -4,7 +4,7 @@ from __future__ import print_function -import codecs +import io import re import stat import sys @@ -53,7 +53,7 @@ def isadded(entries, path): filename=os.path.basename(path) try: - myfile = codecs.open( + myfile = io.open( _unicode_encode(os.path.join(basedir, 'CVS', 'Entries'), encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], errors='strict') @@ -207,7 +207,7 @@ def getentries(mydir,recursive=0): if not os.path.exists(mydir): return entries try: - myfile = codecs.open(_unicode_encode(myfn, + myfile = io.open(_unicode_encode(myfn, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], errors='strict') mylines=myfile.readlines() diff --git a/pym/portage/dbapi/bintree.py b/pym/portage/dbapi/bintree.py index ebec11fdd..62fc62354 100644 --- a/pym/portage/dbapi/bintree.py +++ b/pym/portage/dbapi/bintree.py @@ -34,6 +34,7 @@ from portage import _unicode_encode import codecs import errno +import io import re import stat import subprocess @@ -765,7 +766,7 @@ class binarytree(object): host, parsed_url.path.lstrip("/"), "Packages") pkgindex = self._new_pkgindex() try: - f = codecs.open(_unicode_encode(pkgindex_file, + f = io.open(_unicode_encode(pkgindex_file, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], errors='replace') @@ -1288,7 +1289,7 @@ class binarytree(object): def _load_pkgindex(self): pkgindex = self._new_pkgindex() try: - f = codecs.open(_unicode_encode(self._pkgindex_file, + f = io.open(_unicode_encode(self._pkgindex_file, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], errors='replace') diff --git a/pym/portage/dbapi/porttree.py b/pym/portage/dbapi/porttree.py index 33c6a3b1d..ecf275cd4 100644 --- a/pym/portage/dbapi/porttree.py +++ b/pym/portage/dbapi/porttree.py @@ -37,8 +37,7 @@ from _emerge.EbuildMetadataPhase import EbuildMetadataPhase from _emerge.PollScheduler import PollScheduler import os as _os -import codecs -import logging +import io import stat import sys import traceback @@ -480,7 +479,7 @@ class portdbapi(dbapi): if eapi is None and \ 'parse-eapi-ebuild-head' in self.doebuild_settings.features: - eapi = portage._parse_eapi_ebuild_head(codecs.open( + eapi = portage._parse_eapi_ebuild_head(io.open( _unicode_encode(myebuild, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], diff --git a/pym/portage/dbapi/vartree.py b/pym/portage/dbapi/vartree.py index 5a86291f8..d5c055420 100644 --- a/pym/portage/dbapi/vartree.py +++ b/pym/portage/dbapi/vartree.py @@ -37,7 +37,6 @@ from portage.const import CACHE_PATH, CONFIG_MEMORY_FILE, \ PORTAGE_PACKAGE_ATOM, PRIVATE_PATH, VDB_PATH from portage.const import _ENABLE_DYN_LINK_MAP, _ENABLE_PRESERVE_LIBS from portage.dbapi import dbapi -from portage.dep import _slot_separator from portage.exception import CommandNotFound, \ InvalidData, InvalidLocation, InvalidPackageName, \ FileNotFound, PermissionDenied, UnsupportedAPIException @@ -54,18 +53,19 @@ from portage import _selinux_merge from portage import _unicode_decode from portage import _unicode_encode -from _emerge.AsynchronousLock import AsynchronousLock from _emerge.EbuildBuildDir import EbuildBuildDir from _emerge.EbuildPhase import EbuildPhase from _emerge.emergelog import emergelog from _emerge.PollScheduler import PollScheduler from _emerge.MiscFunctionsProcess import MiscFunctionsProcess -import codecs +import errno import gc -import re, shutil, stat, errno, subprocess +import io import logging import os as _os +import re +import shutil import stat import sys import tempfile @@ -692,7 +692,7 @@ class vardbapi(dbapi): results.append(st[stat.ST_MTIME]) continue try: - myf = codecs.open( + myf = io.open( _unicode_encode(os.path.join(mydir, x), encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], @@ -760,7 +760,7 @@ class vardbapi(dbapi): new_vdb = False counter = -1 try: - cfile = codecs.open( + cfile = io.open( _unicode_encode(self._counter_path, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], @@ -1417,7 +1417,7 @@ class dblink(object): return self.contentscache pkgfiles = {} try: - myc = codecs.open(_unicode_encode(contents_file, + myc = io.open(_unicode_encode(contents_file, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], errors='replace') @@ -3027,7 +3027,7 @@ class dblink(object): continue try: - val = codecs.open(_unicode_encode( + val = io.open(_unicode_encode( os.path.join(inforoot, var_name), encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], @@ -3383,10 +3383,10 @@ class dblink(object): # write local package counter for recording if counter is None: counter = self.vartree.dbapi.counter_tick(mycpv=self.mycpv) - codecs.open(_unicode_encode(os.path.join(self.dbtmpdir, 'COUNTER'), + io.open(_unicode_encode(os.path.join(self.dbtmpdir, 'COUNTER'), encoding=_encodings['fs'], errors='strict'), 'w', encoding=_encodings['repo.content'], errors='backslashreplace' - ).write(str(counter)) + ).write(_unicode_decode(str(counter))) self.updateprotect() @@ -3671,7 +3671,10 @@ class dblink(object): cfgfiledict_orig = cfgfiledict.copy() # open CONTENTS file (possibly overwriting old one) for recording - outfile = codecs.open(_unicode_encode( + # Use atomic_ofstream for automatic coercion of raw bytes to + # unicode, in order to prevent TypeError when writing raw bytes + # to TextIOWrapper with python2. + outfile = atomic_ofstream(_unicode_encode( os.path.join(self.dbtmpdir, 'CONTENTS'), encoding=_encodings['fs'], errors='strict'), mode='w', encoding=_encodings['repo.content'], @@ -4119,7 +4122,7 @@ class dblink(object): "returns contents of a file with whitespace converted to spaces" if not os.path.exists(self.dbdir+"/"+name): return "" - mydata = codecs.open( + mydata = io.open( _unicode_encode(os.path.join(self.dbdir, name), encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], errors='replace' @@ -4132,7 +4135,7 @@ class dblink(object): def getfile(self,fname): if not os.path.exists(self.dbdir+"/"+fname): return "" - return codecs.open(_unicode_encode(os.path.join(self.dbdir, fname), + return io.open(_unicode_encode(os.path.join(self.dbdir, fname), encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], errors='replace' ).read() @@ -4149,7 +4152,7 @@ class dblink(object): def getelements(self,ename): if not os.path.exists(self.dbdir+"/"+ename): return [] - mylines = codecs.open(_unicode_encode( + mylines = io.open(_unicode_encode( os.path.join(self.dbdir, ename), encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], errors='replace' @@ -4161,13 +4164,13 @@ class dblink(object): return myreturn def setelements(self,mylist,ename): - myelement = codecs.open(_unicode_encode( + myelement = io.open(_unicode_encode( os.path.join(self.dbdir, ename), encoding=_encodings['fs'], errors='strict'), mode='w', encoding=_encodings['repo.content'], errors='backslashreplace') for x in mylist: - myelement.write(x+"\n") + myelement.write(_unicode_decode(x+"\n")) myelement.close() def isregular(self): 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 diff --git a/pym/portage/env/loaders.py b/pym/portage/env/loaders.py index 81dd5b172..b540fbbd3 100644 --- a/pym/portage/env/loaders.py +++ b/pym/portage/env/loaders.py @@ -1,9 +1,9 @@ # config.py -- Portage Config -# Copyright 2007 Gentoo Foundation +# Copyright 2007-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 -import codecs import errno +import io import stat from portage import os from portage import _encodings @@ -149,7 +149,7 @@ class FileLoader(DataLoader): func = self.lineParser for fn in RecursiveFileLoader(self.fname): try: - f = codecs.open(_unicode_encode(fn, + f = io.open(_unicode_encode(fn, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], errors='replace') except EnvironmentError as e: diff --git a/pym/portage/glsa.py b/pym/portage/glsa.py index 7e7f4976e..a784d14e1 100644 --- a/pym/portage/glsa.py +++ b/pym/portage/glsa.py @@ -1,9 +1,9 @@ -# Copyright 2003-2010 Gentoo Foundation +# Copyright 2003-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 from __future__ import absolute_import -import codecs +import io import sys try: from urllib.request import urlopen as urllib_request_urlopen @@ -668,12 +668,12 @@ class Glsa: @returns: None """ if not self.isApplied(): - checkfile = codecs.open( + checkfile = io.open( _unicode_encode(os.path.join(self.config["EROOT"], CACHE_PATH, "glsa"), encoding=_encodings['fs'], errors='strict'), mode='a+', encoding=_encodings['content'], errors='strict') - checkfile.write(self.nr+"\n") + checkfile.write(_unicode_decode(self.nr + "\n")) checkfile.close() return None diff --git a/pym/portage/manifest.py b/pym/portage/manifest.py index 4714da032..13efab791 100644 --- a/pym/portage/manifest.py +++ b/pym/portage/manifest.py @@ -1,8 +1,8 @@ # Copyright 1999-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 -import codecs import errno +import io import portage portage.proxy.lazyimport.lazyimport(globals(), @@ -141,7 +141,7 @@ class Manifest(object): """Parse a manifest. If myhashdict is given then data will be added too it. Otherwise, a new dict will be created and returned.""" try: - fd = codecs.open(_unicode_encode(file_path, + fd = io.open(_unicode_encode(file_path, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], errors='replace') if myhashdict is None: @@ -229,7 +229,7 @@ class Manifest(object): update_manifest = True if not force: try: - f = codecs.open(_unicode_encode(self.getFullname(), + f = io.open(_unicode_encode(self.getFullname(), encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], errors='replace') @@ -519,7 +519,7 @@ class Manifest(object): mfname = self.getFullname() if not os.path.exists(mfname): return rVal - myfile = codecs.open(_unicode_encode(mfname, + myfile = io.open(_unicode_encode(mfname, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], errors='replace') lines = myfile.readlines() diff --git a/pym/portage/news.py b/pym/portage/news.py index f500aa3b8..866e5b025 100644 --- a/pym/portage/news.py +++ b/pym/portage/news.py @@ -1,12 +1,12 @@ # portage: news management code -# Copyright 2006-2010 Gentoo Foundation +# Copyright 2006-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 __all__ = ["NewsManager", "NewsItem", "DisplayRestriction", "DisplayProfileRestriction", "DisplayKeywordRestriction", "DisplayInstalledRestriction"] -import codecs +import io import logging import os as _os import re @@ -250,7 +250,7 @@ class NewsItem(object): return self._valid def parse(self): - lines = codecs.open(_unicode_encode(self.path, + lines = io.open(_unicode_encode(self.path, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], errors='replace' ).readlines() diff --git a/pym/portage/output.py b/pym/portage/output.py index a0846b392..1bceb0e90 100644 --- a/pym/portage/output.py +++ b/pym/portage/output.py @@ -3,12 +3,12 @@ __docformat__ = "epytext" -import codecs try: from subprocess import getstatusoutput as subprocess_getstatusoutput except ImportError: from commands import getstatusoutput as subprocess_getstatusoutput import errno +import io import formatter import re import sys @@ -168,7 +168,7 @@ def _parse_color_map(config_root='/', onerror=None): return token try: lineno=0 - for line in codecs.open(_unicode_encode(myfile, + for line in io.open(_unicode_encode(myfile, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], errors='replace'): lineno += 1 diff --git a/pym/portage/package/ebuild/_config/LocationsManager.py b/pym/portage/package/ebuild/_config/LocationsManager.py index 14cfaa672..c2b115bb0 100644 --- a/pym/portage/package/ebuild/_config/LocationsManager.py +++ b/pym/portage/package/ebuild/_config/LocationsManager.py @@ -1,11 +1,11 @@ -# Copyright 2010 Gentoo Foundation +# Copyright 2010-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 __all__ = ( 'LocationsManager', ) -import codecs +import io from portage import os, eapi_is_supported, _encodings, _unicode_encode from portage.const import CUSTOM_PROFILE_PATH, GLOBAL_CONFIG_PATH, \ PROFILE_PATH, USER_CONFIG_PATH @@ -90,7 +90,7 @@ class LocationsManager(object): parentsFile = os.path.join(currentPath, "parent") eapi_file = os.path.join(currentPath, "eapi") try: - eapi = codecs.open(_unicode_encode(eapi_file, + eapi = io.open(_unicode_encode(eapi_file, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], errors='replace' ).readline().strip() diff --git a/pym/portage/package/ebuild/deprecated_profile_check.py b/pym/portage/package/ebuild/deprecated_profile_check.py index 7da5fe848..3fab4da6e 100644 --- a/pym/portage/package/ebuild/deprecated_profile_check.py +++ b/pym/portage/package/ebuild/deprecated_profile_check.py @@ -1,9 +1,9 @@ -# Copyright 2010 Gentoo Foundation +# Copyright 2010-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 __all__ = ['deprecated_profile_check'] -import codecs +import io from portage import os, _encodings, _unicode_encode from portage.const import DEPRECATED_PROFILE_FILE @@ -19,7 +19,7 @@ def deprecated_profile_check(settings=None): DEPRECATED_PROFILE_FILE) if not os.access(deprecated_profile_file, os.R_OK): return False - dcontent = codecs.open(_unicode_encode(deprecated_profile_file, + dcontent = io.open(_unicode_encode(deprecated_profile_file, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], errors='replace').readlines() writemsg(colorize("BAD", _("\n!!! Your current profile is " diff --git a/pym/portage/package/ebuild/doebuild.py b/pym/portage/package/ebuild/doebuild.py index 28ae459b7..53a3f9acf 100644 --- a/pym/portage/package/ebuild/doebuild.py +++ b/pym/portage/package/ebuild/doebuild.py @@ -3,9 +3,9 @@ __all__ = ['doebuild', 'doebuild_environment', 'spawn', 'spawnebuild'] -import codecs import gzip import errno +import io from itertools import chain import logging import os as _os @@ -30,7 +30,7 @@ portage.proxy.lazyimport.lazyimport(globals(), 'portage.util.ExtractKernelVersion:ExtractKernelVersion' ) -from portage import auxdbkeys, bsd_chflags, dep_check, \ +from portage import auxdbkeys, bsd_chflags, \ eapi_is_supported, merge, os, selinux, \ unmerge, _encodings, _parse_eapi_ebuild_head, _os_merge, \ _shell_quote, _unicode_decode, _unicode_encode @@ -39,7 +39,6 @@ from portage.const import EBUILD_SH_ENV_FILE, EBUILD_SH_ENV_DIR, \ from portage.data import portage_gid, portage_uid, secpass, \ uid, userpriv_groups from portage.dbapi.porttree import _parse_uri_map -from portage.dbapi.virtual import fakedbapi from portage.dep import Atom, check_required_use, \ human_readable_required_use, paren_enclose, use_reduce from portage.eapi import eapi_exports_KV, eapi_exports_merge_type, \ @@ -290,7 +289,7 @@ def doebuild_environment(myebuild, mydo, myroot=None, settings=None, if mydo == 'depend' and 'EAPI' not in mysettings.configdict['pkg']: if eapi is None and 'parse-eapi-ebuild-head' in mysettings.features: eapi = _parse_eapi_ebuild_head( - codecs.open(_unicode_encode(ebuild_path, + io.open(_unicode_encode(ebuild_path, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], errors='replace')) @@ -1586,15 +1585,15 @@ def _post_src_install_uid_fix(mysettings, out): build_info_dir = os.path.join(mysettings['PORTAGE_BUILDDIR'], 'build-info') - codecs.open(_unicode_encode(os.path.join(build_info_dir, + io.open(_unicode_encode(os.path.join(build_info_dir, 'SIZE'), encoding=_encodings['fs'], errors='strict'), 'w', encoding=_encodings['repo.content'], - errors='strict').write(str(size) + '\n') + errors='strict').write(_unicode_decode(str(size) + '\n')) - codecs.open(_unicode_encode(os.path.join(build_info_dir, + io.open(_unicode_encode(os.path.join(build_info_dir, 'BUILD_TIME'), encoding=_encodings['fs'], errors='strict'), 'w', encoding=_encodings['repo.content'], - errors='strict').write(str(int(time.time())) + '\n') + errors='strict').write(_unicode_decode(str(int(time.time())) + '\n')) use = frozenset(mysettings['PORTAGE_USE'].split()) for k in _vdb_use_conditional_keys: @@ -1620,10 +1619,10 @@ def _post_src_install_uid_fix(mysettings, out): except OSError: pass continue - codecs.open(_unicode_encode(os.path.join(build_info_dir, + io.open(_unicode_encode(os.path.join(build_info_dir, k), encoding=_encodings['fs'], errors='strict'), mode='w', encoding=_encodings['repo.content'], - errors='strict').write(v + '\n') + errors='strict').write(_unicode_decode(v + '\n')) _reapply_bsdflags_to_image(mysettings) @@ -1649,7 +1648,7 @@ def _post_src_install_soname_symlinks(mysettings, out): "build-info", "NEEDED.ELF.2") try: - lines = codecs.open(_unicode_encode(needed_filename, + lines = io.open(_unicode_encode(needed_filename, encoding=_encodings['fs'], errors='strict'), 'r', encoding=_encodings['repo.content'], errors='replace').readlines() diff --git a/pym/portage/package/ebuild/fetch.py b/pym/portage/package/ebuild/fetch.py index 7bc95eb4f..658b3eb2b 100644 --- a/pym/portage/package/ebuild/fetch.py +++ b/pym/portage/package/ebuild/fetch.py @@ -5,8 +5,8 @@ from __future__ import print_function __all__ = ['fetch'] -import codecs import errno +import io import logging import random import re @@ -31,7 +31,7 @@ from portage.const import BASH_BINARY, CUSTOM_MIRRORS_FILE, \ GLOBAL_CONFIG_PATH from portage.data import portage_gid, portage_uid, secpass, userpriv_groups from portage.exception import FileNotFound, OperationNotPermitted, \ - PermissionDenied, PortageException, TryAgain + PortageException, TryAgain from portage.localization import _ from portage.locks import lockfile, unlockfile from portage.manifest import Manifest @@ -1008,7 +1008,7 @@ def fetch(myuris, mysettings, listonly=0, fetchonly=0, # Fetch failed... Try the next one... Kill 404 files though. if (mystat[stat.ST_SIZE]<100000) and (len(myfile)>4) and not ((myfile[-5:]==".html") or (myfile[-4:]==".htm")): html404=re.compile(".*(not found|404).*",re.I|re.M) - if html404.search(codecs.open( + if html404.search(io.open( _unicode_encode(myfile_path, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], errors='replace' diff --git a/pym/portage/repository/config.py b/pym/portage/repository/config.py index 4461901a4..5538343dc 100644 --- a/pym/portage/repository/config.py +++ b/pym/portage/repository/config.py @@ -1,7 +1,7 @@ # Copyright 2010-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 -import codecs +import io import logging import re @@ -10,7 +10,7 @@ try: except ImportError: from ConfigParser import SafeConfigParser, ParsingError from portage import os -from portage.const import USER_CONFIG_PATH, GLOBAL_CONFIG_PATH, REPO_NAME_LOC +from portage.const import USER_CONFIG_PATH, REPO_NAME_LOC from portage.env.loaders import KeyValuePairFileLoader from portage.util import normalize_path, writemsg, writemsg_level, shlex_split from portage.localization import _ @@ -133,7 +133,7 @@ class RepoConfig(object): """ repo_name_path = os.path.join(repo_path, REPO_NAME_LOC) try: - return codecs.open( + return io.open( _unicode_encode(repo_name_path, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], diff --git a/pym/portage/tests/ebuild/test_spawn.py b/pym/portage/tests/ebuild/test_spawn.py index 89a6c2807..fea4738d4 100644 --- a/pym/portage/tests/ebuild/test_spawn.py +++ b/pym/portage/tests/ebuild/test_spawn.py @@ -1,8 +1,8 @@ -# Copyright 1998-2007 Gentoo Foundation +# Copyright 1998-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 -import codecs import errno +import io import sys import tempfile from portage import os @@ -32,7 +32,7 @@ class SpawnTestCase(TestCase): proc.start() os.close(null_fd) self.assertEqual(proc.wait(), os.EX_OK) - f = codecs.open(_unicode_encode(logfile, + f = io.open(_unicode_encode(logfile, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], errors='strict') log_content = f.read() diff --git a/pym/portage/update.py b/pym/portage/update.py index 4e5e78e5b..52ab50645 100644 --- a/pym/portage/update.py +++ b/pym/portage/update.py @@ -1,8 +1,8 @@ # Copyright 1999-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 -import codecs import errno +import io import re import stat import sys @@ -13,9 +13,9 @@ from portage import _unicode_decode from portage import _unicode_encode import portage portage.proxy.lazyimport.lazyimport(globals(), - 'portage.dep:Atom,dep_getkey,get_operator,isjustname,isvalidatom,' + \ + 'portage.dep:Atom,dep_getkey,isvalidatom,' + \ 'remove_slot', - 'portage.util:ConfigProtect,grabfile,new_protect_filename,' + \ + 'portage.util:ConfigProtect,new_protect_filename,' + \ 'normalize_path,write_atomic,writemsg', 'portage.util.listdir:_ignorecvs_dirs', 'portage.versions:ververify' @@ -86,7 +86,7 @@ def fixdbentries(update_iter, dbdir): mydata = {} for myfile in [f for f in os.listdir(dbdir) if f not in ignored_dbentries]: file_path = os.path.join(dbdir, myfile) - mydata[myfile] = codecs.open(_unicode_encode(file_path, + mydata[myfile] = io.open(_unicode_encode(file_path, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], errors='replace').read() @@ -132,7 +132,7 @@ def grab_updates(updpath, prev_mtimes=None): if update_data or \ file_path not in prev_mtimes or \ long(prev_mtimes[file_path]) != mystat[stat.ST_MTIME]: - content = codecs.open(_unicode_encode(file_path, + content = io.open(_unicode_encode(file_path, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['repo.content'], errors='replace' ).read() @@ -253,7 +253,7 @@ def update_config_files(config_root, protect, protect_mask, update_iter, match_c myxfiles = recursivefiles for x in myxfiles: try: - file_contents[x] = codecs.open( + file_contents[x] = io.open( _unicode_encode(os.path.join(abs_user_config, x), encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], diff --git a/pym/portage/util/ExtractKernelVersion.py b/pym/portage/util/ExtractKernelVersion.py index 1cdb8bf13..5cb9747e6 100644 --- a/pym/portage/util/ExtractKernelVersion.py +++ b/pym/portage/util/ExtractKernelVersion.py @@ -1,9 +1,9 @@ -# Copyright 2010 Gentoo Foundation +# Copyright 2010-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 __all__ = ['ExtractKernelVersion'] -import codecs +import io from portage import os, _encodings, _unicode_encode from portage.util import getconfig, grabfile @@ -22,7 +22,7 @@ def ExtractKernelVersion(base_dir): lines = [] pathname = os.path.join(base_dir, 'Makefile') try: - f = codecs.open(_unicode_encode(pathname, + f = io.open(_unicode_encode(pathname, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], errors='replace') except OSError as details: diff --git a/pym/portage/util/__init__.py b/pym/portage/util/__init__.py index 5468e28c9..b641d3ed2 100644 --- a/pym/portage/util/__init__.py +++ b/pym/portage/util/__init__.py @@ -11,9 +11,9 @@ __all__ = ['apply_permissions', 'apply_recursive_permissions', 'stack_dicts', 'stack_lists', 'unique_array', 'unique_everseen', 'varexpand', 'write_atomic', 'writedict', 'writemsg', 'writemsg_level', 'writemsg_stdout'] -import codecs from copy import deepcopy import errno +import io try: from itertools import filterfalse except ImportError: @@ -475,7 +475,7 @@ def grablines(myfilename, recursive=0, remember_source_file=False): os.path.join(myfilename, f), recursive, remember_source_file)) else: try: - myfile = codecs.open(_unicode_encode(myfilename, + myfile = io.open(_unicode_encode(myfilename, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], errors='replace') if remember_source_file: @@ -1091,7 +1091,7 @@ class atomic_ofstream(ObjectProxy): if 'b' in mode: open_func = open else: - open_func = codecs.open + open_func = io.open kargs.setdefault('encoding', _encodings['content']) kargs.setdefault('errors', 'backslashreplace') @@ -1122,10 +1122,29 @@ class atomic_ofstream(ObjectProxy): def _get_target(self): return object.__getattribute__(self, '_file') - def __getattribute__(self, attr): - if attr in ('close', 'abort', '__del__'): - return object.__getattribute__(self, attr) - return getattr(object.__getattribute__(self, '_file'), attr) + if sys.hexversion >= 0x3000000: + + def __getattribute__(self, attr): + if attr in ('close', 'abort', '__del__'): + return object.__getattribute__(self, attr) + return getattr(object.__getattribute__(self, '_file'), attr) + + else: + + # For TextIOWrapper, automatically coerce write calls to + # unicode, in order to avoid TypeError when writing raw + # bytes with python2. + + def __getattribute__(self, attr): + if attr in ('close', 'abort', 'write', '__del__'): + return object.__getattribute__(self, attr) + return getattr(object.__getattribute__(self, '_file'), attr) + + def write(self, s): + f = object.__getattribute__(self, '_file') + if isinstance(f, io.TextIOWrapper): + s = _unicode_decode(s) + return f.write(s) def close(self): """Closes the temporary file, copies permissions (if possible), diff --git a/pym/portage/util/env_update.py b/pym/portage/util/env_update.py index 3e295f00b..eb8a0d951 100644 --- a/pym/portage/util/env_update.py +++ b/pym/portage/util/env_update.py @@ -3,8 +3,8 @@ __all__ = ['env_update'] -import codecs import errno +import io import stat import sys import time @@ -125,7 +125,7 @@ def env_update(makelinks=1, target_root=None, prev_mtimes=None, contents=None, ldsoconf_path = os.path.join(target_root, "etc", "ld.so.conf") try: - myld = codecs.open(_unicode_encode(ldsoconf_path, + myld = io.open(_unicode_encode(ldsoconf_path, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], errors='replace') myldlines=myld.readlines() diff --git a/pym/repoman/utilities.py b/pym/repoman/utilities.py index 9d4898e8a..232739ea0 100644 --- a/pym/repoman/utilities.py +++ b/pym/repoman/utilities.py @@ -1,5 +1,5 @@ # repoman: Utilities -# Copyright 2007-2010 Gentoo Foundation +# Copyright 2007-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 """This module contains utility functions to help repoman find ebuilds to @@ -20,8 +20,8 @@ __all__ = [ "check_metadata" ] -import codecs import errno +import io import logging import sys from portage import os @@ -326,7 +326,7 @@ def get_commit_message_with_editor(editor, message=None): if not (os.WIFEXITED(retval) and os.WEXITSTATUS(retval) == os.EX_OK): return None try: - mylines = codecs.open(_unicode_encode(filename, + mylines = io.open(_unicode_encode(filename, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], errors='replace' ).readlines()