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:
"""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"""
import sys
from portage import _encodings, _unicode_decode
+from portage.exception import PortageException
from portage.tests import TestCase
from _emerge.DependencyArg import DependencyArg
# 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)