[server] fix unicode conversion capability in UndoException
By overriding the __unicode__ method, we ensure unicode builtin will return
Unicode object even for python versions that don't have a __unicode__ method by
default (python < 2.6)
In previous versions, when this method is not defined, string conversion is
attempted, and the result of string conversion is converted to Unicode using
the system default encoding which is ascii the most of the time.
--- a/server/sources/native.py Thu Jun 16 12:31:27 2011 +0200
+++ b/server/sources/native.py Thu Jun 09 16:41:41 2011 +0200
@@ -165,6 +165,15 @@
class UndoException(Exception):
"""something went wrong during undoing"""
+ def __unicode__(self):
+ """Called by the unicode builtin; should return a Unicode object
+
+ Type of UndoException message must be `unicode` by design in CubicWeb.
+
+ .. warning::
+ This method is not available in python2.5"""
+ assert isinstance(self.message, unicode)
+ return self.message
def _undo_check_relation_target(tentity, rdef, role):
"""check linked entity has not been redirected for this relation"""
--- a/server/test/unittest_undo.py Thu Jun 16 12:31:27 2011 +0200
+++ b/server/test/unittest_undo.py Thu Jun 09 16:41:41 2011 +0200
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
@@ -21,9 +22,11 @@
from cubicweb.devtools.testlib import CubicWebTC
from cubicweb.transaction import *
+from cubicweb.server.sources.native import UndoException
+
+
class UndoableTransactionTC(CubicWebTC):
-
def setup_database(self):
req = self.request()
self.session.undo_actions = set('CUDAR')
@@ -285,6 +288,15 @@
# test implicit 'replacement' of an inlined relation
+
+class UndoExceptionInUnicode(CubicWebTC):
+
+ # problem occurs in string manipulation for python < 2.6
+ def test___unicode__method(self):
+ u = UndoException(u"voilĂ ")
+ self.assertIsInstance(unicode(u), unicode)
+
+
if __name__ == '__main__':
from logilab.common.testlib import unittest_main
unittest_main()