[py3k] __unicode__ and __str__
authorRémi Cardona <remi.cardona@logilab.fr>
Thu, 17 Sep 2015 12:02:40 +0200
changeset 10703 3cb87b61b067
parent 10702 f94c812c3669
child 10704 73367a56ee41
[py3k] __unicode__ and __str__
_exceptions.py
uilib.py
--- a/_exceptions.py	Thu Sep 17 11:07:36 2015 +0200
+++ b/_exceptions.py	Thu Sep 17 12:02:40 2015 +0200
@@ -21,6 +21,8 @@
 
 from warnings import warn
 
+from six import PY3, text_type
+
 from logilab.common.decorators import cachedproperty
 
 from yams import ValidationError
@@ -30,14 +32,15 @@
 class CubicWebException(Exception):
     """base class for cubicweb server exception"""
     msg = ""
-    def __str__(self):
+    def __unicode__(self):
         if self.msg:
             if self.args:
                 return self.msg % tuple(self.args)
             else:
                 return self.msg
         else:
-            return u' '.join(unicode(arg) for arg in self.args)
+            return u' '.join(text_type(arg) for arg in self.args)
+    __str__ = __unicode__ if PY3 else lambda self: self.__unicode__().encode('utf-8')
 
 class ConfigurationError(CubicWebException):
     """a misconfiguration error"""
--- a/uilib.py	Thu Sep 17 11:07:36 2015 +0200
+++ b/uilib.py	Thu Sep 17 12:02:40 2015 +0200
@@ -340,8 +340,7 @@
         if self.parent:
             return u'%s.%s' % (self.parent, self.id)
         return text_type(self.id)
-    def __str__(self):
-        return text_type(self).encode('utf8')
+    __str__ = __unicode__ if PY3 else lambda self: self.__unicode__().encode('utf-8')
     def __getattr__(self, attr):
         return _JSId(attr, self)
     def __call__(self, *args):
@@ -359,6 +358,7 @@
         if self.parent:
             return u'%s(%s)' % (self.parent, ','.join(args))
         return ','.join(args)
+    __str__ = __unicode__ if PY3 else lambda self: self.__unicode__().encode('utf-8')
 
 class _JS(object):
     def __getattr__(self, attr):