# HG changeset patch # User Julien Jehannet # Date 1307630501 -7200 # Node ID 15178bf89fb6ab8e51afa5ee3a81888a87ee9d5c # Parent 2fdc310be7cd3f51955d9268a7dac233f201084f [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. diff -r 2fdc310be7cd -r 15178bf89fb6 server/sources/native.py --- 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""" diff -r 2fdc310be7cd -r 15178bf89fb6 server/test/unittest_undo.py --- 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()