From 206f9156b2c0694fa89a916ca2493c6210834f16 Mon Sep 17 00:00:00 2001 From: Mehdi Abaakouk Date: Tue, 6 Aug 2013 12:16:10 +0200 Subject: [PATCH] Support unicode message in ClientSideError This change add support to unicode message in ClientSideError. Before the change a UnicodeEncodeError is raised when a unicode string is used in a ClientSideError Exception: Traceback (most recent call last): File "/data/os_dev/stack-master/wsme/.tox/py27/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest self.test(*self.arg) File "/data/os_dev/stack-master/wsme/wsme/tests/test_exc.py", line 16, in test_unicode_clientside_error assert e.faultstring == u("\u30d5\u30a1\u30b7\u30ea") File "/data/os_dev/stack-master/wsme/wsme/exc.py", line 12, in faultstring return str(self) UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128) --HG-- branch : sileht/unicode-clientsideerror --- wsme/exc.py | 19 +++++++++++-------- wsme/tests/test_exc.py | 8 +++++++- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/wsme/exc.py b/wsme/exc.py index 841cdc7..4b62476 100644 --- a/wsme/exc.py +++ b/wsme/exc.py @@ -5,19 +5,24 @@ from wsme.utils import _ class ClientSideError(RuntimeError): def __init__(self, msg=None): - super(ClientSideError, self).__init__(msg if msg else self.faultstring) + self.msg = msg + super(ClientSideError, self).__init__(self.faultstring) @property def faultstring(self): - return str(self) + if self.msg is None: + return str(self) + elif isinstance(self.msg, six.text_type): + return self.msg + else: + return six.u(self.msg) class InvalidInput(ClientSideError): def __init__(self, fieldname, value, msg=''): self.fieldname = fieldname self.value = value - self.msg = msg - super(InvalidInput, self).__init__() + super(InvalidInput, self).__init__(msg) @property def faultstring(self): @@ -29,8 +34,7 @@ class InvalidInput(ClientSideError): class MissingArgument(ClientSideError): def __init__(self, argname, msg=''): self.argname = argname - self.msg = msg - super(MissingArgument, self).__init__() + super(MissingArgument, self).__init__(msg) @property def faultstring(self): @@ -41,8 +45,7 @@ class MissingArgument(ClientSideError): class UnknownArgument(ClientSideError): def __init__(self, argname, msg=''): self.argname = argname - self.msg = msg - super(UnknownArgument, self).__init__() + super(UnknownArgument, self).__init__(msg) @property def faultstring(self): diff --git a/wsme/tests/test_exc.py b/wsme/tests/test_exc.py index d57081b..c5c59af 100644 --- a/wsme/tests/test_exc.py +++ b/wsme/tests/test_exc.py @@ -7,7 +7,13 @@ from six import u def test_clientside_error(): e = ClientSideError("Test") - assert e.faultstring == "Test" + assert e.faultstring == u("Test") + + +def test_unicode_clientside_error(): + e = ClientSideError(u("\u30d5\u30a1\u30b7\u30ea")) + + assert e.faultstring == u("\u30d5\u30a1\u30b7\u30ea") def test_invalidinput():