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

pym/portage/exception.py
pym/portage/tests/unicode/test_string_format.py

index 6fa975ae65781c0ea95a8694799d788152e5ee6c..e9e61e2acf4cf343f8919a1e2642f2662c44418c 100644 (file)
@@ -3,7 +3,7 @@
 
 import signal
 import sys
-from portage import _unicode_encode, _unicode_decode
+from portage import _encodings, _unicode_encode, _unicode_decode
 from portage.localization import _
 
 if sys.hexversion >= 0x3000000:
@@ -13,19 +13,24 @@ class PortageException(Exception):
        """General superclass for portage exceptions"""
        def __init__(self,value):
                self.value = value[:]
-               if sys.hexversion < 0x3000000 and isinstance(self.value, unicode):
-                       # Workaround for string formatting operator and unicode value
-                       # attribute triggering empty output in formatted string.
-                       self.value = _unicode_encode(self.value)
+               if isinstance(self.value, basestring):
+                       self.value = _unicode_decode(self.value,
+                               encoding=_encodings['content'], errors='replace')
+
        def __str__(self):
                if isinstance(self.value, basestring):
                        return self.value
                else:
-                       return repr(self.value)
+                       return _unicode_decode(repr(self.value),
+                               encoding=_encodings['content'], errors='replace')
 
        if sys.hexversion < 0x3000000:
-               def __unicode__(self):
-                       return _unicode_decode(self.__str__())
+
+               __unicode__ = __str__
+
+               def __str__(self):
+                       return _unicode_encode(self.__unicode__(),
+                               encoding=_encodings['content'], errors='backslashreplace')
 
 class CorruptionError(PortageException):
        """Corruption indication"""
index 95ef9cb12a3fdd2e13490f2415adfb4e75840b07..d2eb81d9a6accc6a4ba49e2aa28096e055399bdf 100644 (file)
@@ -4,6 +4,7 @@
 import sys
 
 from portage import _encodings, _unicode_decode
+from portage.exception import PortageException
 from portage.tests import TestCase
 from _emerge.DependencyArg import DependencyArg
 
@@ -49,3 +50,28 @@ class StringFormatTestCase(TestCase):
                                # Test the __str__ method which returns encoded bytes in python2
                                formatted_bytes = "%s" % (dependency_arg,)
                                self.assertEqual(formatted_bytes, arg_bytes)
+
+       def testPortageException(self):
+
+               self.assertEqual(_encodings['content'], 'utf_8')
+
+               for arg_bytes in self.unicode_strings:
+                       arg_unicode = _unicode_decode(arg_bytes, encoding=_encodings['content'])
+                       e = PortageException(arg_unicode)
+
+                       # Force unicode format string so that __unicode__() is
+                       # called in python2.
+                       formatted_str = _unicode_decode("%s") % (e,)
+                       self.assertEqual(formatted_str, arg_unicode)
+
+                       if STR_IS_UNICODE:
+
+                               # Test the __str__ method which returns unicode in python3
+                               formatted_str = "%s" % (e,)
+                               self.assertEqual(formatted_str, arg_unicode)
+
+                       else:
+
+                               # Test the __str__ method which returns encoded bytes in python2
+                               formatted_bytes = "%s" % (e,)
+                               self.assertEqual(formatted_bytes, arg_bytes)