From aadbaae1c3cd3c2166df593d984923dc491893b2 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Wed, 15 Sep 2010 20:01:21 -0700 Subject: Make PortageException __str__ and __unicode__ methods more like DependencyArg, and add tests. --- pym/portage/exception.py | 21 ++++++++++++-------- pym/portage/tests/unicode/test_string_format.py | 26 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/pym/portage/exception.py b/pym/portage/exception.py index 6fa975ae6..e9e61e2ac 100644 --- a/pym/portage/exception.py +++ b/pym/portage/exception.py @@ -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""" diff --git a/pym/portage/tests/unicode/test_string_format.py b/pym/portage/tests/unicode/test_string_format.py index 95ef9cb12..d2eb81d9a 100644 --- a/pym/portage/tests/unicode/test_string_format.py +++ b/pym/portage/tests/unicode/test_string_format.py @@ -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) -- cgit v1.2.3-1-g7c22