summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2009-09-22 18:37:31 +0000
committerZac Medico <zmedico@gentoo.org>2009-09-22 18:37:31 +0000
commit443217dc61b0d112babf2a5f8e938b6058ff3a83 (patch)
tree42b02d5ea1664f64bc31c30cb65144744841f61b
parent3ce239a8b5e1a7b5cf000931ca7650b02bc92cf8 (diff)
downloadportage-443217dc61b0d112babf2a5f8e938b6058ff3a83.tar.gz
portage-443217dc61b0d112babf2a5f8e938b6058ff3a83.tar.bz2
portage-443217dc61b0d112babf2a5f8e938b6058ff3a83.zip
Optimize unicode wrappers. Thanks to Marat Radchenko <marat@slonopotamus.org>
for this patch from bug #276813. svn path=/main/trunk/; revision=14376
-rw-r--r--pym/portage/__init__.py58
1 files changed, 30 insertions, 28 deletions
diff --git a/pym/portage/__init__.py b/pym/portage/__init__.py
index 5141e9818..3adf55923 100644
--- a/pym/portage/__init__.py
+++ b/pym/portage/__init__.py
@@ -141,22 +141,26 @@ _encodings = {
if _encodings['merge'] is None:
_encodings['merge'] = 'ascii'
-def _unicode_encode(s, encoding=_encodings['content'],
- errors='backslashreplace'):
- if sys.hexversion >= 0x3000000:
+if sys.hexversion >= 0x3000000:
+ def _unicode_encode(s, encoding=_encodings['content'], errors='backslashreplace'):
if isinstance(s, str):
s = s.encode(encoding, errors)
- elif isinstance(s, unicode):
- s = s.encode(encoding, errors)
- return s
+ return s
-def _unicode_decode(s, encoding=_encodings['content'], errors='replace'):
- if isinstance(s, bytes):
- if sys.hexversion >= 0x3000000:
+ def _unicode_decode(s, encoding=_encodings['content'], errors='replace'):
+ if isinstance(s, bytes):
s = str(s, encoding=encoding, errors=errors)
- else:
+ return s
+else:
+ def _unicode_encode(s, encoding=_encodings['content'], errors='backslashreplace'):
+ if isinstance(s, unicode):
+ s = s.encode(encoding, errors)
+ return s
+
+ def _unicode_decode(s, encoding=_encodings['content'], errors='replace'):
+ if isinstance(s, bytes):
s = unicode(s, encoding=encoding, errors=errors)
- return s
+ return s
class _unicode_func_wrapper(object):
"""
@@ -189,24 +193,22 @@ class _unicode_func_wrapper(object):
rval = self._func(*wrapped_args, **wrapped_kwargs)
- if isinstance(rval, (bytes, basestring, list, tuple)):
- if isinstance(rval, (bytes, basestring)):
- rval = _unicode_decode(rval,
- encoding=encoding, errors='replace')
- else:
- decoded_rval = []
- for x in rval:
- try:
- x = _unicode_decode(x, encoding=encoding, errors='strict')
- except UnicodeDecodeError:
- pass
- else:
- decoded_rval.append(x)
-
- if isinstance(rval, tuple):
- rval = tuple(decoded_rval)
+ if isinstance(rval, (list, tuple)):
+ decoded_rval = []
+ for x in rval:
+ try:
+ x = _unicode_decode(x, encoding=encoding, errors='strict')
+ except UnicodeDecodeError:
+ pass
else:
- rval = decoded_rval
+ decoded_rval.append(x)
+
+ if isinstance(rval, tuple):
+ rval = tuple(decoded_rval)
+ else:
+ rval = decoded_rval
+ else:
+ rval = _unicode_decode(rval, encoding=encoding, errors='replace')
return rval