Use six.text_type instead of unicode

Add hacking checks for unicode() calls

Change-Id: I27062986e69a90e3f4fc26a76080b146e825149b
Closes-Bug: #1380806
This commit is contained in:
Ivan Kolodyazhny 2015-04-02 17:55:48 +03:00
parent ebe3234d9e
commit 319bddcc03
19 changed files with 70 additions and 35 deletions

View File

@ -19,6 +19,7 @@ Cinder Specific Commandments
- [N333] Ensure that oslo namespaces are used for namespaced libraries. - [N333] Ensure that oslo namespaces are used for namespaced libraries.
- [N339] Prevent use of deprecated contextlib.nested. - [N339] Prevent use of deprecated contextlib.nested.
- [C301] timeutils.utcnow() from oslo_utils should be used instead of datetime.now(). - [C301] timeutils.utcnow() from oslo_utils should be used instead of datetime.now().
- [C302] six.text_type should be used instead of unicode
General General

View File

@ -16,6 +16,7 @@
from oslo_log import log as logging from oslo_log import log as logging
import oslo_messaging as messaging import oslo_messaging as messaging
from oslo_utils import strutils from oslo_utils import strutils
import six
import webob import webob
from cinder.api import extensions from cinder.api import extensions
@ -269,13 +270,13 @@ class VolumeActionsController(wsgi.Controller):
except exception.InvalidVolume as error: except exception.InvalidVolume as error:
raise webob.exc.HTTPBadRequest(explanation=error.msg) raise webob.exc.HTTPBadRequest(explanation=error.msg)
except ValueError as error: except ValueError as error:
raise webob.exc.HTTPBadRequest(explanation=unicode(error)) raise webob.exc.HTTPBadRequest(explanation=six.text_type(error))
except messaging.RemoteError as error: except messaging.RemoteError as error:
msg = "%(err_type)s: %(err_msg)s" % {'err_type': error.exc_type, msg = "%(err_type)s: %(err_msg)s" % {'err_type': error.exc_type,
'err_msg': error.value} 'err_msg': error.value}
raise webob.exc.HTTPBadRequest(explanation=msg) raise webob.exc.HTTPBadRequest(explanation=msg)
except Exception as error: except Exception as error:
raise webob.exc.HTTPBadRequest(explanation=unicode(error)) raise webob.exc.HTTPBadRequest(explanation=six.text_type(error))
return {'os-volume_upload_image': response} return {'os-volume_upload_image': response}
@wsgi.action('os-extend') @wsgi.action('os-extend')

View File

@ -13,6 +13,7 @@
# under the License. # under the License.
from oslo_log import log as logging from oslo_log import log as logging
import six
import webob import webob
from webob import exc from webob import exc
@ -76,7 +77,7 @@ class VolumeReplicationController(wsgi.Controller):
msg = _("Volume could not be found") msg = _("Volume could not be found")
raise exc.HTTPNotFound(explanation=msg) raise exc.HTTPNotFound(explanation=msg)
except exception.ReplicationError as error: except exception.ReplicationError as error:
raise exc.HTTPBadRequest(explanation=unicode(error)) raise exc.HTTPBadRequest(explanation=six.text_type(error))
return webob.Response(status_int=202) return webob.Response(status_int=202)
@wsgi.response(202) @wsgi.response(202)
@ -94,7 +95,7 @@ class VolumeReplicationController(wsgi.Controller):
msg = _("Volume could not be found") msg = _("Volume could not be found")
raise exc.HTTPNotFound(explanation=msg) raise exc.HTTPNotFound(explanation=msg)
except exception.ReplicationError as error: except exception.ReplicationError as error:
raise exc.HTTPBadRequest(explanation=unicode(error)) raise exc.HTTPBadRequest(explanation=six.text_type(error))
return webob.Response(status_int=202) return webob.Response(status_int=202)

View File

@ -15,6 +15,7 @@
# under the License. # under the License.
from oslo_log import log as logging from oslo_log import log as logging
import six
import webob.dec import webob.dec
import webob.exc import webob.exc
@ -64,7 +65,7 @@ class FaultWrapper(base_wsgi.Middleware):
# including those that are safe to expose, see bug 1021373 # including those that are safe to expose, see bug 1021373
if safe: if safe:
msg = (inner.msg if isinstance(inner, exception.CinderException) msg = (inner.msg if isinstance(inner, exception.CinderException)
else unicode(inner)) else six.text_type(inner))
params = {'exception': inner.__class__.__name__, params = {'exception': inner.__class__.__name__,
'explanation': msg} 'explanation': msg}
outer.explanation = _('%(exception)s: %(explanation)s') % params outer.explanation = _('%(exception)s: %(explanation)s') % params

View File

@ -17,6 +17,7 @@ import os.path
import re import re
from lxml import etree from lxml import etree
import six
from cinder.i18n import _ from cinder.i18n import _
from cinder import utils from cinder import utils
@ -338,12 +339,12 @@ class TemplateElement(object):
# Start with the text... # Start with the text...
if self.text is not None: if self.text is not None:
elem.text = unicode(self.text(obj)) elem.text = six.text_type(self.text(obj))
# Now set up all the attributes... # Now set up all the attributes...
for key, value in self.attrib.items(): for key, value in self.attrib.items():
try: try:
elem.set(key, unicode(value(obj, True))) elem.set(key, six.text_type(value(obj, True)))
except KeyError: except KeyError:
# Attribute has no value, so don't include it # Attribute has no value, so don't include it
pass pass

View File

@ -80,7 +80,7 @@ class ChunkedBackupDriver(driver.BackupDriver):
pass pass
err = _('unsupported compression algorithm: %s') % algorithm err = _('unsupported compression algorithm: %s') % algorithm
raise ValueError(unicode(err)) raise ValueError(err)
def __init__(self, context, chunk_size_bytes, sha_block_size_bytes, def __init__(self, context, chunk_size_bytes, sha_block_size_bytes,
backup_default_container, enable_progress_timer, backup_default_container, enable_progress_timer,

View File

@ -493,7 +493,7 @@ class CephBackupDriver(driver.BackupDriver):
p1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE, p1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
except OSError as e: except OSError as e:
LOG.error(_LE("Pipe1 failed - %s ") % unicode(e)) LOG.error(_LE("Pipe1 failed - %s "), e)
raise raise
# NOTE(dosaboy): ensure that the pipe is blocking. This is to work # NOTE(dosaboy): ensure that the pipe is blocking. This is to work
@ -507,7 +507,7 @@ class CephBackupDriver(driver.BackupDriver):
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
except OSError as e: except OSError as e:
LOG.error(_LE("Pipe2 failed - %s ") % unicode(e)) LOG.error(_LE("Pipe2 failed - %s "), e)
raise raise
p1.stdout.close() p1.stdout.close()

View File

@ -38,6 +38,7 @@ from oslo_log import log as logging
import oslo_messaging as messaging import oslo_messaging as messaging
from oslo_utils import excutils from oslo_utils import excutils
from oslo_utils import importutils from oslo_utils import importutils
import six
from cinder.backup import driver from cinder.backup import driver
from cinder.backup import rpcapi as backup_rpcapi from cinder.backup import rpcapi as backup_rpcapi
@ -298,7 +299,7 @@ class BackupManager(manager.SchedulerDependentManager):
{'status': 'available'}) {'status': 'available'})
self.db.backup_update(context, backup_id, self.db.backup_update(context, backup_id,
{'status': 'error', {'status': 'error',
'fail_reason': unicode(err)}) 'fail_reason': six.text_type(err)})
self.db.volume_update(context, volume_id, {'status': 'available'}) self.db.volume_update(context, volume_id, {'status': 'available'})
backup = self.db.backup_update(context, backup_id, backup = self.db.backup_update(context, backup_id,
@ -406,7 +407,7 @@ class BackupManager(manager.SchedulerDependentManager):
self.db.backup_update(context, backup_id, self.db.backup_update(context, backup_id,
{'status': 'error', {'status': 'error',
'fail_reason': 'fail_reason':
unicode(err)}) six.text_type(err)})
LOG.info(_LI('Delete backup started, backup: %s.'), backup_id) LOG.info(_LI('Delete backup started, backup: %s.'), backup_id)
backup = self.db.backup_get(context, backup_id) backup = self.db.backup_get(context, backup_id)
@ -446,7 +447,7 @@ class BackupManager(manager.SchedulerDependentManager):
self.db.backup_update(context, backup_id, self.db.backup_update(context, backup_id,
{'status': 'error', {'status': 'error',
'fail_reason': 'fail_reason':
unicode(err)}) six.text_type(err)})
# Get reservations # Get reservations
try: try:
@ -528,7 +529,7 @@ class BackupManager(manager.SchedulerDependentManager):
backup_url = backup_service.export_record(backup) backup_url = backup_service.export_record(backup)
backup_record['backup_url'] = backup_url backup_record['backup_url'] = backup_url
except Exception as err: except Exception as err:
msg = unicode(err) msg = six.text_type(err)
raise exception.InvalidBackup(reason=msg) raise exception.InvalidBackup(reason=msg)
LOG.info(_LI('Export record finished, backup %s exported.'), backup_id) LOG.info(_LI('Export record finished, backup %s exported.'), backup_id)
@ -579,7 +580,7 @@ class BackupManager(manager.SchedulerDependentManager):
backup_service = self.service.get_backup_driver(context) backup_service = self.service.get_backup_driver(context)
backup_options = backup_service.import_record(backup_url) backup_options = backup_service.import_record(backup_url)
except Exception as err: except Exception as err:
msg = unicode(err) msg = six.text_type(err)
self.db.backup_update(context, self.db.backup_update(context,
backup_id, backup_id,
{'status': 'error', {'status': 'error',
@ -627,7 +628,7 @@ class BackupManager(manager.SchedulerDependentManager):
self.db.backup_update(context, backup_id, self.db.backup_update(context, backup_id,
{'status': 'error', {'status': 'error',
'fail_reason': 'fail_reason':
unicode(err)}) six.text_type(err)})
LOG.info(_LI('Import record id %s metadata from driver ' LOG.info(_LI('Import record id %s metadata from driver '
'finished.') % backup_id) 'finished.') % backup_id)

View File

@ -15,6 +15,7 @@
"""Exceptions for the Brick library.""" """Exceptions for the Brick library."""
from oslo_log import log as logging from oslo_log import log as logging
import six
from cinder.i18n import _ from cinder.i18n import _
@ -65,7 +66,7 @@ class BrickException(Exception):
super(BrickException, self).__init__(message) super(BrickException, self).__init__(message)
def __unicode__(self): def __unicode__(self):
return unicode(self.msg) return six.text_type(self.msg)
class NotFound(BrickException): class NotFound(BrickException):

View File

@ -112,7 +112,7 @@ class CinderException(Exception):
return self.kwargs['message'] is None or '%(message)' in self.message return self.kwargs['message'] is None or '%(message)' in self.message
def __unicode__(self): def __unicode__(self):
return unicode(self.msg) return six.text_type(self.msg)
class VolumeBackendAPIException(CinderException): class VolumeBackendAPIException(CinderException):

View File

@ -198,6 +198,16 @@ def check_datetime_now(logical_line, noqa):
yield(0, msg) yield(0, msg)
def check_unicode_usage(logical_line, noqa):
if noqa:
return
msg = "C302: Found unicode() call. Please use six.text_type()."
if 'unicode(' in logical_line:
yield(0, msg)
def factory(register): def factory(register):
register(no_vi_headers) register(no_vi_headers)
register(no_translate_debug_logs) register(no_translate_debug_logs)
@ -209,3 +219,4 @@ def factory(register):
register(check_no_contextlib_nested) register(check_no_contextlib_nested)
register(check_datetime_now) register(check_datetime_now)
register(validate_log_translations) register(validate_log_translations)
register(check_unicode_usage)

View File

@ -241,7 +241,7 @@ class String(FieldType):
# FIXME(danms): We should really try to avoid the need to do this # FIXME(danms): We should really try to avoid the need to do this
if isinstance(value, (six.string_types, int, long, float, if isinstance(value, (six.string_types, int, long, float,
datetime.datetime)): datetime.datetime)):
return unicode(value) return six.text_type(value)
else: else:
raise ValueError(_('A string is required here, not %s') % raise ValueError(_('A string is required here, not %s') %
value.__class__.__name__) value.__class__.__name__)

View File

@ -22,6 +22,7 @@ import mock
from oslo_concurrency import processutils as putils from oslo_concurrency import processutils as putils
from oslo_config import cfg from oslo_config import cfg
from oslo_log import log as logging from oslo_log import log as logging
import six
from cinder.brick import exception from cinder.brick import exception
from cinder.brick.initiator import connector from cinder.brick.initiator import connector
@ -715,7 +716,7 @@ class FibreChannelConnectorTestCase(ConnectorTestCase):
name = 'volume-00000001' name = 'volume-00000001'
vol = {'id': 1, 'name': name} vol = {'id': 1, 'name': name}
# Should work for string, unicode, and list # Should work for string, unicode, and list
wwns = ['1234567890123456', unicode('1234567890123456'), wwns = ['1234567890123456', six.text_type('1234567890123456'),
['1234567890123456', '1234567890123457']] ['1234567890123456', '1234567890123457']]
for wwn in wwns: for wwn in wwns:
connection_info = self.fibrechan_connection(vol, location, wwn) connection_info = self.fibrechan_connection(vol, location, wwn)

View File

@ -18,6 +18,8 @@
from cinder.brick import exception from cinder.brick import exception
from cinder import test from cinder import test
import six
class BrickExceptionTestCase(test.TestCase): class BrickExceptionTestCase(test.TestCase):
def test_default_error_msg(self): def test_default_error_msg(self):
@ -25,17 +27,18 @@ class BrickExceptionTestCase(test.TestCase):
message = "default message" message = "default message"
exc = FakeBrickException() exc = FakeBrickException()
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(exception.BrickException('test')), 'test') self.assertEqual(six.text_type(exception.BrickException('test')),
'test')
def test_default_error_msg_with_kwargs(self): def test_default_error_msg_with_kwargs(self):
class FakeBrickException(exception.BrickException): class FakeBrickException(exception.BrickException):
message = "default message: %(code)s" message = "default message: %(code)s"
exc = FakeBrickException(code=500) exc = FakeBrickException(code=500)
self.assertEqual(unicode(exc), 'default message: 500') self.assertEqual(six.text_type(exc), 'default message: 500')
def test_error_msg_exception_with_kwargs(self): def test_error_msg_exception_with_kwargs(self):
# NOTE(dprince): disable format errors for this test # NOTE(dprince): disable format errors for this test
@ -45,7 +48,8 @@ class BrickExceptionTestCase(test.TestCase):
message = "default message: %(mispelled_code)s" message = "default message: %(mispelled_code)s"
exc = FakeBrickException(code=500) exc = FakeBrickException(code=500)
self.assertEqual(unicode(exc), 'default message: %(mispelled_code)s') self.assertEqual(six.text_type(exc),
'default message: %(mispelled_code)s')
def test_default_error_code(self): def test_default_error_code(self):
class FakeBrickException(exception.BrickException): class FakeBrickException(exception.BrickException):

View File

@ -18,6 +18,8 @@
from cinder import exception from cinder import exception
from cinder import test from cinder import test
import six
class FakeNotifier(object): class FakeNotifier(object):
"""Acts like the cinder.openstack.common.notifier.api module.""" """Acts like the cinder.openstack.common.notifier.api module."""
@ -54,17 +56,18 @@ class CinderExceptionTestCase(test.TestCase):
message = "default message" message = "default message"
exc = FakeCinderException() exc = FakeCinderException()
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(exception.CinderException('test')), 'test') self.assertEqual(six.text_type(exception.CinderException('test')),
'test')
def test_default_error_msg_with_kwargs(self): def test_default_error_msg_with_kwargs(self):
class FakeCinderException(exception.CinderException): class FakeCinderException(exception.CinderException):
message = "default message: %(code)s" message = "default message: %(code)s"
exc = FakeCinderException(code=500) exc = FakeCinderException(code=500)
self.assertEqual(unicode(exc), 'default message: 500') self.assertEqual(six.text_type(exc), 'default message: 500')
def test_error_msg_exception_with_kwargs(self): def test_error_msg_exception_with_kwargs(self):
# NOTE(dprince): disable format errors for this test # NOTE(dprince): disable format errors for this test
@ -74,7 +77,8 @@ class CinderExceptionTestCase(test.TestCase):
message = "default message: %(misspelled_code)s" message = "default message: %(misspelled_code)s"
exc = FakeCinderException(code=500) exc = FakeCinderException(code=500)
self.assertEqual(unicode(exc), 'default message: %(misspelled_code)s') self.assertEqual(six.text_type(exc),
'default message: %(misspelled_code)s')
def test_default_error_code(self): def test_default_error_code(self):
class FakeCinderException(exception.CinderException): class FakeCinderException(exception.CinderException):
@ -107,14 +111,14 @@ class CinderExceptionTestCase(test.TestCase):
message = 'FakeCinderException: %(message)s' message = 'FakeCinderException: %(message)s'
exc = FakeCinderException(message='message') exc = FakeCinderException(message='message')
self.assertEqual(unicode(exc), 'FakeCinderException: message') self.assertEqual(six.text_type(exc), 'FakeCinderException: message')
def test_message_and_kwarg_in_format_string(self): def test_message_and_kwarg_in_format_string(self):
class FakeCinderException(exception.CinderException): class FakeCinderException(exception.CinderException):
message = 'Error %(code)d: %(message)s' message = 'Error %(code)d: %(message)s'
exc = FakeCinderException(message='message', code=404) exc = FakeCinderException(message='message', code=404)
self.assertEqual(unicode(exc), 'Error 404: message') self.assertEqual(six.text_type(exc), 'Error 404: message')
def test_message_is_exception_in_format_string(self): def test_message_is_exception_in_format_string(self):
class FakeCinderException(exception.CinderException): class FakeCinderException(exception.CinderException):
@ -123,4 +127,4 @@ class CinderExceptionTestCase(test.TestCase):
msg = 'test message' msg = 'test message'
exc1 = Exception(msg) exc1 = Exception(msg)
exc2 = FakeCinderException(message=exc1) exc2 = FakeCinderException(message=exc1)
self.assertEqual(unicode(exc2), 'Exception: test message') self.assertEqual(six.text_type(exc2), 'Exception: test message')

View File

@ -199,3 +199,9 @@ class HackingTestCase(test.TestCase):
"LOG.error(_LE('foo')", "foo.py")))) "LOG.error(_LE('foo')", "foo.py"))))
self.assertEqual(0, len(list(checks.validate_log_translations( self.assertEqual(0, len(list(checks.validate_log_translations(
"LOG.exception(_LE('foo')", "foo.py")))) "LOG.exception(_LE('foo')", "foo.py"))))
def test_check_unicode_usage(self):
self.assertEqual(1, len(list(checks.check_unicode_usage(
"unicode(msg)", False))))
self.assertEqual(0, len(list(checks.check_unicode_usage(
"unicode(msg) # noqa", True))))

View File

@ -18,6 +18,7 @@
import mock import mock
from oslo_log import log as logging from oslo_log import log as logging
from oslo_utils import units from oslo_utils import units
import six
from cinder import context from cinder import context
from cinder import exception from cinder import exception
@ -331,7 +332,7 @@ class TestHPLeftHandCLIQISCSIDriver(HPLeftHandBaseDriver, test.TestCase):
def test_paramiko_1_13_0(cliq_args): def test_paramiko_1_13_0(cliq_args):
# paramiko 1.13.0 now returns unicode # paramiko 1.13.0 now returns unicode
output = unicode( output = six.text_type(
'<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n' '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n'
'<gauche version="1.0">\n\n <response description="Operation' '<gauche version="1.0">\n\n <response description="Operation'
' succeeded." name="CliqSuccess" processingTime="423" ' ' succeeded." name="CliqSuccess" processingTime="423" '

View File

@ -161,8 +161,8 @@ class FlashSystemDriver(san.SanDriver):
invalid_ch_in_host = invalid_ch_in_host + ch invalid_ch_in_host = invalid_ch_in_host + ch
host_name = connector['host'] host_name = connector['host']
if isinstance(host_name, unicode): if isinstance(host_name, six.text_type):
unicode_host_name_filter = dict((ord(unicode(char)), u'-') unicode_host_name_filter = dict((ord(six.text_type(char)), u'-')
for char in invalid_ch_in_host) for char in invalid_ch_in_host)
host_name = host_name.translate(unicode_host_name_filter) host_name = host_name.translate(unicode_host_name_filter)
elif isinstance(host_name, str): elif isinstance(host_name, str):

View File

@ -48,6 +48,7 @@ from oslo_utils import importutils
from oslo_utils import timeutils from oslo_utils import timeutils
from oslo_utils import uuidutils from oslo_utils import uuidutils
from osprofiler import profiler from osprofiler import profiler
import six
from taskflow import exceptions as tfe from taskflow import exceptions as tfe
from cinder import compute from cinder import compute
@ -942,7 +943,7 @@ class VolumeManager(manager.SchedulerDependentManager):
self._delete_image(context, image_meta['id'], image_service) self._delete_image(context, image_meta['id'], image_service)
with excutils.save_and_reraise_exception(): with excutils.save_and_reraise_exception():
payload['message'] = unicode(error) payload['message'] = six.text_type(error)
finally: finally:
if not volume['volume_attachment']: if not volume['volume_attachment']:
self.db.volume_update(context, volume_id, self.db.volume_update(context, volume_id,