summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2015-05-20 11:23:00 -0700
committerVictor Stinner <vstinner@redhat.com>2015-05-20 11:23:00 -0700
commit22ace8fd78738a33b2e08c47ab37384b90e43de2 (patch)
treedeb7ffa0f368658e1f2191308385d66d1062ae86
parent7f6a18b041eba88133db7ac5518cb2163bee7e03 (diff)
downloadglance_store-22ace8fd78738a33b2e08c47ab37384b90e43de2.tar.gz
Port exception_to_str() to Python 3
On Python 3, exception messages are unicode strings. There is no need to decode them. Skip the test on the "ignore" error handler on Python 3, it's specific to Python 2. Change-Id: Ieec9e1e1f7a0fc901271fd74eca357fe1cf6a975
-rw-r--r--glance_store/common/utils.py4
-rw-r--r--tests/unit/test_utils.py13
2 files changed, 13 insertions, 4 deletions
diff --git a/glance_store/common/utils.py b/glance_store/common/utils.py
index 48618dc..47571c0 100644
--- a/glance_store/common/utils.py
+++ b/glance_store/common/utils.py
@@ -150,4 +150,6 @@ def exception_to_str(exc):
except UnicodeError:
error = ("Caught '%(exception)s' exception." %
{"exception": exc.__class__.__name__})
- return encodeutils.safe_encode(error, errors='ignore')
+ if six.PY2:
+ error = encodeutils.safe_encode(error, errors='ignore')
+ return error
diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py
index 6bfb089..e3a4042 100644
--- a/tests/unit/test_utils.py
+++ b/tests/unit/test_utils.py
@@ -14,6 +14,7 @@
# under the License.
from oslotest import base
+import six
from glance_store.common import utils
@@ -29,9 +30,15 @@ class TestUtils(base.BaseTestCase):
ret = utils.exception_to_str(Exception('error message'))
self.assertEqual(ret, 'error message')
- ret = utils.exception_to_str(Exception('\xa5 error message'))
- self.assertEqual(ret, ' error message')
-
ret = utils.exception_to_str(FakeException('\xa5 error message'))
self.assertEqual(ret, "Caught '%(exception)s' exception." %
{'exception': 'FakeException'})
+
+ def test_exception_to_str_ignore(self):
+ if six.PY3:
+ # On Python 3, exception messages are unicode strings, they are not
+ # decoded from an encoding and so it's not possible to test the
+ # "ignore" error handler
+ self.skipTest("test specific to Python 2")
+ ret = utils.exception_to_str(Exception('\xa5 error message'))
+ self.assertEqual(ret, ' error message')