diff options
author | Victor Stinner <victor.stinner@enovance.com> | 2014-03-14 11:00:15 +0100 |
---|---|---|
committer | Victor Stinner <victor.stinner@enovance.com> | 2014-03-14 11:00:15 +0100 |
commit | 423a040e3dcbd12c229da57e91ac47805d69cc7a (patch) | |
tree | 1ce4d13441f0724d042b5d628d9be3066eb20ce7 | |
parent | 2c61721479fb69f6a43d2c1c46fdad4cd9e21920 (diff) | |
download | oslo-i18n-423a040e3dcbd12c229da57e91ac47805d69cc7a.tar.gz |
Fix test_gettextutils on Python 3
On Python 3, str(obj) calls obj.__str__(), not obj.__unicode__().
The test does not pass with nosetests, LogLevelTranslationsTest fails with an
AttributeError (self.level is missing), because nosetests doesn't play well
with test scenarios.
Change-Id: I78ae106e336480d9995ac4394197ea45b8ef9b07
-rw-r--r-- | openstack/common/gettextutils.py | 15 | ||||
-rw-r--r-- | tests/unit/fakes.py | 5 | ||||
-rw-r--r-- | tests/unit/test_gettext.py | 12 |
3 files changed, 23 insertions, 9 deletions
diff --git a/openstack/common/gettextutils.py b/openstack/common/gettextutils.py index b77087d..7983fb1 100644 --- a/openstack/common/gettextutils.py +++ b/openstack/common/gettextutils.py @@ -274,13 +274,14 @@ class Message(six.text_type): def __radd__(self, other): return self.__add__(other) - def __str__(self): - # NOTE(luisg): Logging in python 2.6 tries to str() log records, - # and it expects specifically a UnicodeError in order to proceed. - msg = _('Message objects do not support str() because they may ' - 'contain non-ascii characters. ' - 'Please use unicode() or translate() instead.') - raise UnicodeError(msg) + if six.PY2: + def __str__(self): + # NOTE(luisg): Logging in python 2.6 tries to str() log records, + # and it expects specifically a UnicodeError in order to proceed. + msg = _('Message objects do not support str() because they may ' + 'contain non-ascii characters. ' + 'Please use unicode() or translate() instead.') + raise UnicodeError(msg) def get_available_languages(domain): diff --git a/tests/unit/fakes.py b/tests/unit/fakes.py index 9c2e663..eab0d88 100644 --- a/tests/unit/fakes.py +++ b/tests/unit/fakes.py @@ -43,6 +43,11 @@ class FakeTranslations(gettext.GNUTranslations): def __init__(self, translations): self.translations = translations + # used by Python 3 + def gettext(self, msgid): + return self.translations.get(msgid, msgid) + + # used by Python 2 def ugettext(self, msgid): return self.translations.get(msgid, msgid) diff --git a/tests/unit/test_gettext.py b/tests/unit/test_gettext.py index 5d285f5..61b6226 100644 --- a/tests/unit/test_gettext.py +++ b/tests/unit/test_gettext.py @@ -20,6 +20,7 @@ import logging from babel import localedata import mock import six +import testtools from openstack.common.fixture import moxstubout from openstack.common import gettextutils @@ -420,6 +421,7 @@ class MessageTestCase(test.BaseTestCase): test_me = lambda: SomeObject('test') + self.message(msgid) self.assertRaises(TypeError, test_me) + @testtools.skipIf(six.PY3, 'test specific to Python 2') def test_str_disabled(self): msgid = "A message" test_me = lambda: str(self.message(msgid)) @@ -796,6 +798,8 @@ class SomeObject(object): def __unicode__(self): return self.message + # alias for Python 3 + __str__ = __unicode__ class NoDeepCopyObject(object): @@ -803,8 +807,12 @@ class NoDeepCopyObject(object): def __init__(self, value): self.value = value - def __unicode__(self): - return unicode(self.value) + if six.PY3: + def __str__(self): + return str(self.value) + else: + def __unicode__(self): + return unicode(self.value) def __deepcopy__(self, memo): raise TypeError('Deep Copy not supported') |