Make UseFlagDisplay __str__ and __unicode__ methods more like
authorZac Medico <zmedico@gentoo.org>
Thu, 16 Sep 2010 10:18:44 +0000 (03:18 -0700)
committerZac Medico <zmedico@gentoo.org>
Thu, 16 Sep 2010 10:18:44 +0000 (03:18 -0700)
DependencyArg, and add tests.

pym/_emerge/UseFlagDisplay.py
pym/_emerge/actions.py
pym/portage/tests/unicode/test_string_format.py

index db2d6ca1620f472302232dd188f3766db2fa909d..3721ef09ecfde698f558bfe8e796aac471e1ed5c 100644 (file)
@@ -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.
index 7c151b3212959f2cb5bfcc757d9e5a0f57969a4e..5c4647e61e030243e1cd0641d1534912cb2a84db 100644 (file)
@@ -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():
index d2eb81d9a6accc6a4ba49e2aa28096e055399bdf..fb6e8e02ed6622997820c29d320b5047aae762b8 100644 (file)
@@ -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)