Fix str(Unauthorized) 3.24
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 20 Jan 2017 15:16:18 +0100
branch3.24
changeset 11917 c38e13988c10
parent 11909 244cd7f407b8
child 11921 a03376213747
Fix str(Unauthorized) which is really problematic on python 3 where __unicode__ is never called anymore. This was caused because in PY3, CubicWebException.__str__ was referencing __unicode__ implementation of CubicWebException, not of its subclass that implements it. Fix a flake8 style warning about lambda assignment as a bonus.
cubicweb/_exceptions.py
cubicweb/test/unittest_utils.py
--- a/cubicweb/_exceptions.py	Fri Jan 20 03:54:43 2017 +0100
+++ b/cubicweb/_exceptions.py	Fri Jan 20 15:16:18 2017 +0100
@@ -21,7 +21,7 @@
 
 from warnings import warn
 
-from six import PY3, text_type
+from six import PY2, text_type
 
 from logilab.common.decorators import cachedproperty
 
@@ -40,7 +40,13 @@
                 return self.msg
         else:
             return u' '.join(text_type(arg) for arg in self.args)
-    __str__ = __unicode__ if PY3 else lambda self: self.__unicode__().encode('utf-8')
+
+    def __str__(self):
+        res = self.__unicode__()
+        if PY2:
+            res = res.encode('utf-8')
+        return res
+
 
 class ConfigurationError(CubicWebException):
     """a misconfiguration error"""
--- a/cubicweb/test/unittest_utils.py	Fri Jan 20 03:54:43 2017 +0100
+++ b/cubicweb/test/unittest_utils.py	Fri Jan 20 15:16:18 2017 +0100
@@ -1,4 +1,4 @@
-# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -27,9 +27,10 @@
 except ImportError:  # Python3
     from unittest import TestCase
 
+from six import PY2
 from six.moves import range
 
-from cubicweb import Binary
+from cubicweb import Binary, Unauthorized
 from cubicweb.devtools.testlib import CubicWebTC
 from cubicweb.utils import (make_uid, UStringIO, RepeatList, HTMLHead,
                             QueryCache, parse_repo_uri)
@@ -40,6 +41,7 @@
 except ImportError:
     json = None
 
+
 class MakeUidTC(TestCase):
     def test_1(self):
         self.assertNotEqual(make_uid('xyz'), make_uid('abcd'))
@@ -325,6 +327,26 @@
             self.config.global_set_option('concat-resources', True)
 
 
+def UnauthorizedTC(TestCase):
+
+    def _test(self, func):
+        self.assertEqual(func(Unauthorized()),
+                         'You are not allowed to perform this operation')
+        self.assertEqual(func(Unauthorized('a')),
+                         'a')
+        self.assertEqual(func(Unauthorized('a', 'b')),
+                         'You are not allowed to perform a operation on b')
+        self.assertEqual(func(Unauthorized('a', 'b', 'c')),
+                         'a b c')
+
+    def test_str(self):
+        self._test(str)
+
+    if PY2:
+        def test_unicode(self):
+            self._test(unicode)
+
+
 def load_tests(loader, tests, ignore):
     import cubicweb.utils
     tests.addTests(doctest.DocTestSuite(cubicweb.utils))