From dbf7ff3a60ed49f548de4a3a21b625bf317edc66 Mon Sep 17 00:00:00 2001 From: Pierre Riteau Date: Thu, 16 Nov 2017 08:59:16 +0000 Subject: [PATCH] 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 --- blazar/api/context.py | 4 +++- .../alembic_migrations/versions/0_1_initial.py | 3 ++- blazar/db/sqlalchemy/models.py | 3 ++- blazar/tests/db/sqlalchemy/test_utils.py | 3 ++- .../tests/plugins/instances/test_instance_plugin.py | 3 ++- blazar/tests/test_exceptions.py | 12 +++++++----- 6 files changed, 18 insertions(+), 10 deletions(-) diff --git a/blazar/api/context.py b/blazar/api/context.py index e9ca921e..84571801 100644 --- a/blazar/api/context.py +++ b/blazar/api/context.py @@ -15,6 +15,8 @@ import json +import six + from blazar import context from blazar import exceptions @@ -34,5 +36,5 @@ def ctx_from_headers(headers): service_catalog=service_catalog, user_name=headers['X-User-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(',')), ) diff --git a/blazar/db/migration/alembic_migrations/versions/0_1_initial.py b/blazar/db/migration/alembic_migrations/versions/0_1_initial.py index 37d32e8b..bdbb2097 100644 --- a/blazar/db/migration/alembic_migrations/versions/0_1_initial.py +++ b/blazar/db/migration/alembic_migrations/versions/0_1_initial.py @@ -28,12 +28,13 @@ down_revision = None import uuid from alembic import op +import six import sqlalchemy as sa from sqlalchemy.dialects.mysql import MEDIUMTEXT def _generate_unicode_uuid(): - return unicode(str(uuid.uuid4())) + return six.text_type(str(uuid.uuid4())) def MediumText(): diff --git a/blazar/db/sqlalchemy/models.py b/blazar/db/sqlalchemy/models.py index 8b6d1899..47fa8ab9 100644 --- a/blazar/db/sqlalchemy/models.py +++ b/blazar/db/sqlalchemy/models.py @@ -17,6 +17,7 @@ # FIXME: https://bugs.launchpad.net/climate/+bug/1300132 # from oslo_log import log as logging from oslo_utils import uuidutils +import six import sqlalchemy as sa from sqlalchemy.dialects.mysql import MEDIUMTEXT from sqlalchemy.orm import relationship @@ -29,7 +30,7 @@ from blazar.db.sqlalchemy import model_base as mb def _generate_unicode_uuid(): - return unicode(uuidutils.generate_uuid()) + return six.text_type(uuidutils.generate_uuid()) def MediumText(): diff --git a/blazar/tests/db/sqlalchemy/test_utils.py b/blazar/tests/db/sqlalchemy/test_utils.py index 68720faf..c947cbad 100644 --- a/blazar/tests/db/sqlalchemy/test_utils.py +++ b/blazar/tests/db/sqlalchemy/test_utils.py @@ -18,6 +18,7 @@ import datetime from oslo_context import context from oslo_utils import uuidutils +import six from blazar.db.sqlalchemy import api as db_api from blazar.db.sqlalchemy import utils as db_utils @@ -25,7 +26,7 @@ from blazar import tests def _get_fake_random_uuid(): - return unicode(uuidutils.generate_uuid()) + return six.text_type(uuidutils.generate_uuid()) def _get_fake_lease_uuid(): diff --git a/blazar/tests/plugins/instances/test_instance_plugin.py b/blazar/tests/plugins/instances/test_instance_plugin.py index b931f05b..47b64375 100644 --- a/blazar/tests/plugins/instances/test_instance_plugin.py +++ b/blazar/tests/plugins/instances/test_instance_plugin.py @@ -17,6 +17,7 @@ import datetime import uuid import mock +import six from blazar import context from blazar.db import api as db_api @@ -54,7 +55,7 @@ class TestVirtualInstancePlugin(tests.TestCase): } 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): return [ diff --git a/blazar/tests/test_exceptions.py b/blazar/tests/test_exceptions.py index 87056dd9..81e79f2d 100644 --- a/blazar/tests/test_exceptions.py +++ b/blazar/tests/test_exceptions.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import six + from blazar import exceptions from blazar import tests @@ -23,10 +25,10 @@ class BlazarExceptionTestCase(tests.TestCase): msg_fmt = "default message" exc = FakeBlazarException() - self.assertEqual(unicode(exc), 'default message') + self.assertEqual(six.text_type(exc), 'default message') def test_error_msg(self): - self.assertEqual(unicode(exceptions.BlazarException('test')), + self.assertEqual(six.text_type(exceptions.BlazarException('test')), 'test') def test_default_error_msg_with_kwargs(self): @@ -34,7 +36,7 @@ class BlazarExceptionTestCase(tests.TestCase): msg_fmt = "default message: %(code)s" 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') def test_error_msg_exception_with_kwargs(self): @@ -42,7 +44,7 @@ class BlazarExceptionTestCase(tests.TestCase): msg_fmt = "default message: %(mispelled_code)s" 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') def test_default_error_code(self): @@ -61,6 +63,6 @@ class BlazarExceptionTestCase(tests.TestCase): def test_policynotauthorized_exception(self): 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") self.assertEqual(exc.kwargs['code'], 403)