Fix unicode issues with Python 3

In Python 3, all strings are unicode and the unicode() function has been
removed. For compatibility with both Python 2 and 3, use six.text_type()
instead.

Partially implements: blueprint python-3
Change-Id: I062e3b1146f2e8f416d78258a947b3bffa482222
This commit is contained in:
Pierre Riteau 2017-11-16 08:59:16 +00:00
parent 9a08d37e5c
commit dbf7ff3a60
6 changed files with 18 additions and 10 deletions

View File

@ -15,6 +15,8 @@
import json import json
import six
from blazar import context from blazar import context
from blazar import exceptions from blazar import exceptions
@ -34,5 +36,5 @@ def ctx_from_headers(headers):
service_catalog=service_catalog, service_catalog=service_catalog,
user_name=headers['X-User-Name'], user_name=headers['X-User-Name'],
project_name=headers['X-Project-Name'], project_name=headers['X-Project-Name'],
roles=map(unicode.strip, headers['X-Roles'].split(',')), roles=map(six.text_type.strip, headers['X-Roles'].split(',')),
) )

View File

@ -28,12 +28,13 @@ down_revision = None
import uuid import uuid
from alembic import op from alembic import op
import six
import sqlalchemy as sa import sqlalchemy as sa
from sqlalchemy.dialects.mysql import MEDIUMTEXT from sqlalchemy.dialects.mysql import MEDIUMTEXT
def _generate_unicode_uuid(): def _generate_unicode_uuid():
return unicode(str(uuid.uuid4())) return six.text_type(str(uuid.uuid4()))
def MediumText(): def MediumText():

View File

@ -17,6 +17,7 @@
# FIXME: https://bugs.launchpad.net/climate/+bug/1300132 # FIXME: https://bugs.launchpad.net/climate/+bug/1300132
# from oslo_log import log as logging # from oslo_log import log as logging
from oslo_utils import uuidutils from oslo_utils import uuidutils
import six
import sqlalchemy as sa import sqlalchemy as sa
from sqlalchemy.dialects.mysql import MEDIUMTEXT from sqlalchemy.dialects.mysql import MEDIUMTEXT
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
@ -29,7 +30,7 @@ from blazar.db.sqlalchemy import model_base as mb
def _generate_unicode_uuid(): def _generate_unicode_uuid():
return unicode(uuidutils.generate_uuid()) return six.text_type(uuidutils.generate_uuid())
def MediumText(): def MediumText():

View File

@ -18,6 +18,7 @@ import datetime
from oslo_context import context from oslo_context import context
from oslo_utils import uuidutils from oslo_utils import uuidutils
import six
from blazar.db.sqlalchemy import api as db_api from blazar.db.sqlalchemy import api as db_api
from blazar.db.sqlalchemy import utils as db_utils from blazar.db.sqlalchemy import utils as db_utils
@ -25,7 +26,7 @@ from blazar import tests
def _get_fake_random_uuid(): def _get_fake_random_uuid():
return unicode(uuidutils.generate_uuid()) return six.text_type(uuidutils.generate_uuid())
def _get_fake_lease_uuid(): def _get_fake_lease_uuid():

View File

@ -17,6 +17,7 @@ import datetime
import uuid import uuid
import mock import mock
import six
from blazar import context from blazar import context
from blazar.db import api as db_api from blazar.db import api as db_api
@ -54,7 +55,7 @@ class TestVirtualInstancePlugin(tests.TestCase):
} }
def get_uuid(self): def get_uuid(self):
return unicode(str(uuid.uuid4())) return six.text_type(str(uuid.uuid4()))
def generate_basic_events(self, lease_id, start, before_end, end): def generate_basic_events(self, lease_id, start, before_end, end):
return [ return [

View File

@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import six
from blazar import exceptions from blazar import exceptions
from blazar import tests from blazar import tests
@ -23,10 +25,10 @@ class BlazarExceptionTestCase(tests.TestCase):
msg_fmt = "default message" msg_fmt = "default message"
exc = FakeBlazarException() exc = FakeBlazarException()
self.assertEqual(unicode(exc), 'default message') self.assertEqual(six.text_type(exc), 'default message')
def test_error_msg(self): def test_error_msg(self):
self.assertEqual(unicode(exceptions.BlazarException('test')), self.assertEqual(six.text_type(exceptions.BlazarException('test')),
'test') 'test')
def test_default_error_msg_with_kwargs(self): def test_default_error_msg_with_kwargs(self):
@ -34,7 +36,7 @@ class BlazarExceptionTestCase(tests.TestCase):
msg_fmt = "default message: %(code)s" msg_fmt = "default message: %(code)s"
exc = FakeBlazarException(code=500) exc = FakeBlazarException(code=500)
self.assertEqual(unicode(exc), 'default message: 500') self.assertEqual(six.text_type(exc), 'default message: 500')
self.assertEqual(exc.message, 'default message: 500') self.assertEqual(exc.message, 'default message: 500')
def test_error_msg_exception_with_kwargs(self): def test_error_msg_exception_with_kwargs(self):
@ -42,7 +44,7 @@ class BlazarExceptionTestCase(tests.TestCase):
msg_fmt = "default message: %(mispelled_code)s" msg_fmt = "default message: %(mispelled_code)s"
exc = FakeBlazarException(code=500, mispelled_code='blah') exc = FakeBlazarException(code=500, mispelled_code='blah')
self.assertEqual(unicode(exc), 'default message: blah') self.assertEqual(six.text_type(exc), 'default message: blah')
self.assertEqual(exc.message, 'default message: blah') self.assertEqual(exc.message, 'default message: blah')
def test_default_error_code(self): def test_default_error_code(self):
@ -61,6 +63,6 @@ class BlazarExceptionTestCase(tests.TestCase):
def test_policynotauthorized_exception(self): def test_policynotauthorized_exception(self):
exc = exceptions.PolicyNotAuthorized(action='foo') exc = exceptions.PolicyNotAuthorized(action='foo')
self.assertEqual(unicode(exc.message), self.assertEqual(six.text_type(exc.message),
"Policy doesn't allow foo to be performed") "Policy doesn't allow foo to be performed")
self.assertEqual(exc.kwargs['code'], 403) self.assertEqual(exc.kwargs['code'], 403)