From fd85b3ead32cd988e93f1d33d219ffd52cd77a51 Mon Sep 17 00:00:00 2001 From: Cyril Roelandt Date: Wed, 10 Jun 2015 10:20:58 +0000 Subject: [PATCH] Python3: replace 'unicode' with 'six.text_type' In Python 3, 'unicode' does not exist; 'six.text_type' should be used instead. Change-Id: I71011b4beee9817a61278eb473804cfb798de74a Blueprint: neutron-python3 --- neutron/agent/metadata/agent.py | 7 +++++-- neutron/agent/metadata/namespace_proxy.py | 7 +++++-- neutron/api/extensions.py | 3 ++- neutron/common/exceptions.py | 6 ++++-- neutron/policy.py | 2 +- neutron/tests/unit/api/v2/test_base.py | 4 +++- neutron/wsgi.py | 2 +- 7 files changed, 21 insertions(+), 10 deletions(-) diff --git a/neutron/agent/metadata/agent.py b/neutron/agent/metadata/agent.py index e2cad9c9ace..769d8039bc0 100644 --- a/neutron/agent/metadata/agent.py +++ b/neutron/agent/metadata/agent.py @@ -20,6 +20,7 @@ from neutronclient.v2_0 import client from oslo_config import cfg from oslo_log import log as logging import oslo_messaging +import six import six.moves.urllib.parse as urlparse import webob @@ -116,7 +117,8 @@ class MetadataProxyHandler(object): LOG.exception(_LE("Unexpected error.")) msg = _('An unknown error has occurred. ' 'Please try your request again.') - return webob.exc.HTTPInternalServerError(explanation=unicode(msg)) + explanation = six.text_type(msg) + return webob.exc.HTTPInternalServerError(explanation=explanation) def _get_ports_from_server(self, router_id=None, ip_address=None, networks=None): @@ -257,7 +259,8 @@ class MetadataProxyHandler(object): 'Remote metadata server experienced an internal server error.' ) LOG.warn(msg) - return webob.exc.HTTPInternalServerError(explanation=unicode(msg)) + explanation = six.text_type(msg) + return webob.exc.HTTPInternalServerError(explanation=explanation) else: raise Exception(_('Unexpected response code: %s') % resp.status) diff --git a/neutron/agent/metadata/namespace_proxy.py b/neutron/agent/metadata/namespace_proxy.py index e84a256de69..d68cb2493a5 100644 --- a/neutron/agent/metadata/namespace_proxy.py +++ b/neutron/agent/metadata/namespace_proxy.py @@ -15,6 +15,7 @@ import httplib2 from oslo_config import cfg from oslo_log import log as logging +import six import six.moves.urllib.parse as urlparse import webob @@ -56,7 +57,8 @@ class NetworkMetadataProxyHandler(object): LOG.exception(_LE("Unexpected error.")) msg = _('An unknown error has occurred. ' 'Please try your request again.') - return webob.exc.HTTPInternalServerError(explanation=unicode(msg)) + explanation = six.text_type(msg) + return webob.exc.HTTPInternalServerError(explanation=explanation) def _proxy_request(self, remote_address, method, path_info, query_string, body): @@ -103,7 +105,8 @@ class NetworkMetadataProxyHandler(object): 'Remote metadata server experienced an internal server error.' ) LOG.debug(msg) - return webob.exc.HTTPInternalServerError(explanation=unicode(msg)) + explanation = six.text_type(msg) + return webob.exc.HTTPInternalServerError(explanation=explanation) else: raise Exception(_('Unexpected response code: %s') % resp.status) diff --git a/neutron/api/extensions.py b/neutron/api/extensions.py index fa275bfe02f..f6b4601ba21 100644 --- a/neutron/api/extensions.py +++ b/neutron/api/extensions.py @@ -506,7 +506,8 @@ class ExtensionManager(object): LOG.debug('Ext namespace: %s', extension.get_namespace()) LOG.debug('Ext updated: %s', extension.get_updated()) except AttributeError as ex: - LOG.exception(_LE("Exception loading extension: %s"), unicode(ex)) + LOG.exception(_LE("Exception loading extension: %s"), + six.text_type(ex)) return False return True diff --git a/neutron/common/exceptions.py b/neutron/common/exceptions.py index 5d29d2afe04..c6ec6ccca54 100644 --- a/neutron/common/exceptions.py +++ b/neutron/common/exceptions.py @@ -18,6 +18,7 @@ Neutron base exception handling. """ from oslo_utils import excutils +import six class NeutronException(Exception): @@ -40,8 +41,9 @@ class NeutronException(Exception): # at least get the core message out if something happened super(NeutronException, self).__init__(self.message) - def __unicode__(self): - return unicode(self.msg) + if six.PY2: + def __unicode__(self): + return unicode(self.msg) def use_fatal_exceptions(self): return False diff --git a/neutron/policy.py b/neutron/policy.py index a2d099f6761..63a0820b3f1 100644 --- a/neutron/policy.py +++ b/neutron/policy.py @@ -313,7 +313,7 @@ class OwnerCheck(policy.Check): f) match = self.match % target if self.kind in creds: - return match == unicode(creds[self.kind]) + return match == six.text_type(creds[self.kind]) return False diff --git a/neutron/tests/unit/api/v2/test_base.py b/neutron/tests/unit/api/v2/test_base.py index ab05215e3f9..ccb4f44b14f 100644 --- a/neutron/tests/unit/api/v2/test_base.py +++ b/neutron/tests/unit/api/v2/test_base.py @@ -929,7 +929,9 @@ class JSONV2TestCase(APIv2TestBase, testlib_api.WebTestCase): return_value.update(initial_input['port']) instance = self.plugin.return_value - instance.get_network.return_value = {'tenant_id': unicode(tenant_id)} + instance.get_network.return_value = { + 'tenant_id': six.text_type(tenant_id) + } instance.get_ports_count.return_value = 1 instance.create_port.return_value = return_value res = self.api.post(_get_path('ports', fmt=self.fmt), diff --git a/neutron/wsgi.py b/neutron/wsgi.py index 437e57b0984..7adba758e4b 100644 --- a/neutron/wsgi.py +++ b/neutron/wsgi.py @@ -412,7 +412,7 @@ class JSONDictSerializer(DictSerializer): def default(self, data): def sanitizer(obj): - return unicode(obj) + return six.text_type(obj) return jsonutils.dumps(data, default=sanitizer)