From: Zac Medico Date: Thu, 16 Sep 2010 10:18:44 +0000 (-0700) Subject: Make UseFlagDisplay __str__ and __unicode__ methods more like X-Git-Tag: v2.2_rc84~39 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=724e9e28185d35398bbbd445eccfa7592c4f641b;p=portage.git Make UseFlagDisplay __str__ and __unicode__ methods more like DependencyArg, and add tests. --- diff --git a/pym/_emerge/UseFlagDisplay.py b/pym/_emerge/UseFlagDisplay.py index db2d6ca16..3721ef09e 100644 --- a/pym/_emerge/UseFlagDisplay.py +++ b/pym/_emerge/UseFlagDisplay.py @@ -1,9 +1,13 @@ -# Copyright 1999-2009 Gentoo Foundation +# Copyright 1999-2010 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 +import sys + +from portage import _encodings, _unicode_encode from portage.output import red from portage.util import cmp_sort_key from portage.output import blue + class UseFlagDisplay(object): __slots__ = ('name', 'enabled', 'forced') @@ -24,6 +28,14 @@ class UseFlagDisplay(object): s = '(%s)' % s return s + if sys.hexversion < 0x3000000: + + __unicode__ = __str__ + + def __str__(self): + return _unicode_encode(self.__unicode__(), + encoding=_encodings['content']) + def _cmp_combined(a, b): """ Sort by name, combining enabled and disabled flags. diff --git a/pym/_emerge/actions.py b/pym/_emerge/actions.py index 7c151b321..5c4647e61 100644 --- a/pym/_emerge/actions.py +++ b/pym/_emerge/actions.py @@ -1507,6 +1507,7 @@ def action_info(settings, trees, myopts, myfiles): f not in use_expand_flags: use_disabled['USE'].append(f) + flag_displays = [] for varname in var_order: if varname in use_expand_hidden: continue @@ -1519,8 +1520,11 @@ def action_info(settings, trees, myopts, myfiles): flags.sort(key=UseFlagDisplay.sort_combined) else: flags.sort(key=UseFlagDisplay.sort_separated) - print('%s="%s"' % (varname, ' '.join(str(f) for f in flags)), end=' ') - print() + # Use _unicode_decode() to force unicode format string so + # that UseFlagDisplay.__unicode__() is called in python2. + flag_displays.append('%s="%s"' % (varname, + ' '.join(_unicode_decode("%s") % (f,) for f in flags))) + writemsg_stdout('%s\n' % ' '.join(flag_displays)) if pkg_type == "installed": for myvar in mydesiredvars: if metadata[myvar].split() != settings.get(myvar, '').split(): diff --git a/pym/portage/tests/unicode/test_string_format.py b/pym/portage/tests/unicode/test_string_format.py index d2eb81d9a..fb6e8e02e 100644 --- a/pym/portage/tests/unicode/test_string_format.py +++ b/pym/portage/tests/unicode/test_string_format.py @@ -7,6 +7,10 @@ from portage import _encodings, _unicode_decode from portage.exception import PortageException from portage.tests import TestCase from _emerge.DependencyArg import DependencyArg +from _emerge.UseFlagDisplay import UseFlagDisplay + +if sys.hexversion >= 0x3000000: + basestring = str STR_IS_UNICODE = sys.hexversion >= 0x3000000 @@ -75,3 +79,30 @@ class StringFormatTestCase(TestCase): # Test the __str__ method which returns encoded bytes in python2 formatted_bytes = "%s" % (e,) self.assertEqual(formatted_bytes, arg_bytes) + + def testUseFlagDisplay(self): + + self.assertEqual(_encodings['content'], 'utf_8') + + for enabled in (True, False): + for forced in (True, False): + for arg_bytes in self.unicode_strings: + arg_unicode = _unicode_decode(arg_bytes, encoding=_encodings['content']) + e = UseFlagDisplay(arg_unicode, enabled, forced) + + # Force unicode format string so that __unicode__() is + # called in python2. + formatted_str = _unicode_decode("%s") % (e,) + self.assertEqual(isinstance(formatted_str, basestring), True) + + if STR_IS_UNICODE: + + # Test the __str__ method which returns unicode in python3 + formatted_str = "%s" % (e,) + self.assertEqual(isinstance(formatted_str, str), True) + + else: + + # Test the __str__ method which returns encoded bytes in python2 + formatted_bytes = "%s" % (e,) + self.assertEqual(isinstance(formatted_bytes, bytes), True)