summaryrefslogtreecommitdiffstats
path: root/pym
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2009-03-11 06:11:39 +0000
committerZac Medico <zmedico@gentoo.org>2009-03-11 06:11:39 +0000
commitc90268eee1403ad41f55d9df334c20fe4da56ec4 (patch)
tree3c0db7fa360d50729c320253c9162fb90106cb5c /pym
parenta4219b47157101bd7ae0165d4fd86c307ffb2c6f (diff)
downloadportage-c90268eee1403ad41f55d9df334c20fe4da56ec4.tar.gz
portage-c90268eee1403ad41f55d9df334c20fe4da56ec4.tar.bz2
portage-c90268eee1403ad41f55d9df334c20fe4da56ec4.zip
The python that's inside stage 1 or 2 is built with a minimal
configuration which does not include the /usr/lib/pythonX.Y/encodings directory. This results in error like the following: LookupError: no codec search functions registered: can't find encoding In order to solve this problem, detect it early and manually register a search function for the ascii codec. Starting with python-3.0 this problem is more noticeable because of stricter handling of encoding and decoding between strings of characters and bytes. (trunk r12684) svn path=/main/branches/2.1.6/; revision=12944
Diffstat (limited to 'pym')
-rw-r--r--pym/portage/__init__.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/pym/portage/__init__.py b/pym/portage/__init__.py
index 7a8c0f262..0c062f7f2 100644
--- a/pym/portage/__init__.py
+++ b/pym/portage/__init__.py
@@ -7451,6 +7451,62 @@ def portageexit():
atexit_register(portageexit)
+def _ensure_default_encoding():
+ """
+ The python that's inside stage 1 or 2 is built with a minimal
+ configuration which does not include the /usr/lib/pythonX.Y/encodings
+ directory. This results in error like the following:
+
+ LookupError: no codec search functions registered: can't find encoding
+
+ In order to solve this problem, detect it early and manually register
+ a search function for the ascii codec. Starting with python-3.0 this
+ problem is more noticeable because of stricter handling of encoding
+ and decoding between strings of characters and bytes.
+ """
+
+ import codecs
+ try:
+ codecs.lookup(sys.getdefaultencoding())
+ except LookupError:
+ pass
+ else:
+ return
+
+ class IncrementalEncoder(codecs.IncrementalEncoder):
+ def encode(self, input, final=False):
+ return codecs.ascii_encode(input, self.errors)[0]
+
+ class IncrementalDecoder(codecs.IncrementalDecoder):
+ def decode(self, input, final=False):
+ return codecs.ascii_decode(input, self.errors)[0]
+
+ class StreamWriter(codecs.StreamWriter):
+ encode = codecs.ascii_encode
+
+ class StreamReader(codecs.StreamReader):
+ decode = codecs.ascii_decode
+
+ # The sys.setdefaultencoding() function doesn't necessarily exist,
+ # so just setup the ascii codec to correspond to whatever name
+ # happens to be returned by sys.getdefaultencoding().
+ encoding = sys.getdefaultencoding()
+
+ def search_function(name):
+ if name != encoding:
+ return None
+ return codecs.CodecInfo(
+ name=encoding,
+ encode=codecs.ascii_encode,
+ decode=codecs.ascii_decode,
+ incrementalencoder=IncrementalEncoder,
+ incrementaldecoder=IncrementalDecoder,
+ streamwriter=StreamWriter,
+ streamreader=StreamReader,
+ )
+
+ codecs.register(search_function)
+
def _global_updates(trees, prev_mtimes):
"""
Perform new global updates if they exist in $PORTDIR/profiles/updates/.
@@ -7841,6 +7897,8 @@ if True:
"flushmtimedb"):
globals()[k] = _LegacyGlobalProxy(k)
+ _ensure_default_encoding()
+
# Clear the cache
dircache={}