From 2f9b8514e4d4b15569ebc88e7da36f5ae972c37e Mon Sep 17 00:00:00 2001 From: Arfrever Frehtes Taifersar Arahesis Date: Mon, 21 Sep 2009 14:15:49 +0000 Subject: [PATCH] Support both Python 2 and 3 in portage._unicode_encode() and portage._unicode_decode(). Update decoding in some files. svn path=/main/trunk/; revision=14321 --- bin/portageq | 2 +- pym/_emerge/EbuildMetadataPhase.py | 3 ++- pym/_emerge/main.py | 2 +- pym/portage/__init__.py | 12 +++++------- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/bin/portageq b/bin/portageq index 59eedc59f..34c4fb401 100755 --- a/bin/portageq +++ b/bin/portageq @@ -610,7 +610,7 @@ def main(): os.environ["ROOT"] = sys.argv[2] args = sys.argv[2:] - if args and not isinstance(args[0], unicode): + if args and sys.hexversion < 0x3000000 and not isinstance(args[0], unicode): for i in range(len(args)): args[i] = portage._unicode_decode(args[i]) diff --git a/pym/_emerge/EbuildMetadataPhase.py b/pym/_emerge/EbuildMetadataPhase.py index 07d993b7d..1231b1b11 100644 --- a/pym/_emerge/EbuildMetadataPhase.py +++ b/pym/_emerge/EbuildMetadataPhase.py @@ -9,6 +9,7 @@ from portage.cache.mappings import slot_dict_class import portage from portage import os from portage import _encodings +from portage import _unicode_decode from portage import _unicode_encode import fcntl import codecs @@ -122,7 +123,7 @@ class EbuildMetadataPhase(SubProcess): def _set_returncode(self, wait_retval): SubProcess._set_returncode(self, wait_retval) if self.returncode == os.EX_OK: - metadata_lines = ''.join(unicode(chunk, + metadata_lines = ''.join(_unicode_decode(chunk, encoding=_encodings['repo.content'], errors='replace') for chunk in self._raw_metadata).splitlines() if len(portage.auxdbkeys) != len(metadata_lines): diff --git a/pym/_emerge/main.py b/pym/_emerge/main.py index 9a101803f..83b50df63 100644 --- a/pym/_emerge/main.py +++ b/pym/_emerge/main.py @@ -769,7 +769,7 @@ def parse_opts(tmpcmdline, silent=False): if myaction is None and myoptions.deselect is True: myaction = 'deselect' - if myargs and not isinstance(myargs[0], unicode): + if sys.hexversion < 0x3000000 and not isinstance(myargs[0], unicode): for i in range(len(myargs)): myargs[i] = portage._unicode_decode(myargs[i]) diff --git a/pym/portage/__init__.py b/pym/portage/__init__.py index 4b19cb9d9..c8dff4c5c 100644 --- a/pym/portage/__init__.py +++ b/pym/portage/__init__.py @@ -142,18 +142,16 @@ if _encodings['merge'] is None: def _unicode_encode(s, encoding=_encodings['content'], errors='backslashreplace'): - if isinstance(s, unicode): + if not isinstance(s, bytes): s = s.encode(encoding, errors) return s def _unicode_decode(s, encoding=_encodings['content'], errors='replace'): - if not isinstance(s, unicode): - if sys.hexversion < 0x3000000: - if isinstance(s, basestring): - s = unicode(s, encoding=encoding, errors=errors) + if isinstance(s, bytes): + if sys.hexversion >= 0x3000000: + s = str(s, encoding=encoding, errors=errors) else: - if isinstance(s, bytes): - s = unicode(s, encoding=encoding, errors=errors) + s = unicode(s, encoding=encoding, errors=errors) return s class _unicode_func_wrapper(object): -- 2.26.2