Merge "Fix test_gettextutils on Python 3"
This commit is contained in:
commit
5fb343faee
|
@ -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):
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue