]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Use six.text_type instead of unicode
authorIvan Kolodyazhny <e0ne@e0ne.info>
Thu, 2 Apr 2015 14:55:48 +0000 (17:55 +0300)
committerIvan Kolodyazhny <e0ne@e0ne.info>
Thu, 2 Apr 2015 16:07:58 +0000 (19:07 +0300)
Add hacking checks for unicode() calls

Change-Id: I27062986e69a90e3f4fc26a76080b146e825149b
Closes-Bug: #1380806

19 files changed:
HACKING.rst
cinder/api/contrib/volume_actions.py
cinder/api/contrib/volume_replication.py
cinder/api/middleware/fault.py
cinder/api/xmlutil.py
cinder/backup/chunkeddriver.py
cinder/backup/drivers/ceph.py
cinder/backup/manager.py
cinder/brick/exception.py
cinder/exception.py
cinder/hacking/checks.py
cinder/objects/fields.py
cinder/tests/brick/test_brick_connector.py
cinder/tests/brick/test_brick_exception.py
cinder/tests/test_exception.py
cinder/tests/test_hacking.py
cinder/tests/test_hplefthand.py
cinder/volume/drivers/ibm/flashsystem.py
cinder/volume/manager.py

index ef9bd58890430553d89271aecd6b27b400bf817b..507aea50b216d95a608e9b92cef2fa8483c6861e 100644 (file)
@@ -19,6 +19,7 @@ Cinder Specific Commandments
 - [N333] Ensure that oslo namespaces are used for namespaced libraries.
 - [N339] Prevent use of deprecated contextlib.nested.
 - [C301] timeutils.utcnow() from oslo_utils should be used instead of datetime.now().
+- [C302] six.text_type should be used instead of unicode
 
 
 General
index aca5c11844855a1a832670116cbb82e0070fe77f..423cf17a288de4ba43c0926011ddb5b9de3addf7 100644 (file)
@@ -16,6 +16,7 @@
 from oslo_log import log as logging
 import oslo_messaging as messaging
 from oslo_utils import strutils
+import six
 import webob
 
 from cinder.api import extensions
@@ -269,13 +270,13 @@ class VolumeActionsController(wsgi.Controller):
         except exception.InvalidVolume as error:
             raise webob.exc.HTTPBadRequest(explanation=error.msg)
         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:
             msg = "%(err_type)s: %(err_msg)s" % {'err_type': error.exc_type,
                                                  'err_msg': error.value}
             raise webob.exc.HTTPBadRequest(explanation=msg)
         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}
 
     @wsgi.action('os-extend')
index 338d46f4b2995aadfc91f5c9f8012b5b687c2c7e..e0846548f32a59725ef6f8177efaa101cf061d45 100644 (file)
@@ -13,6 +13,7 @@
 #    under the License.
 
 from oslo_log import log as logging
+import six
 import webob
 from webob import exc
 
@@ -76,7 +77,7 @@ class VolumeReplicationController(wsgi.Controller):
             msg = _("Volume could not be found")
             raise exc.HTTPNotFound(explanation=msg)
         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)
 
     @wsgi.response(202)
@@ -94,7 +95,7 @@ class VolumeReplicationController(wsgi.Controller):
             msg = _("Volume could not be found")
             raise exc.HTTPNotFound(explanation=msg)
         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)
 
 
index 480f8a89f63c3d5fa949e93b0b57eded2f38d365..0575b46f108f7bbf1727a70f7943260695f70e47 100644 (file)
@@ -15,6 +15,7 @@
 #    under the License.
 
 from oslo_log import log as logging
+import six
 import webob.dec
 import webob.exc
 
@@ -64,7 +65,7 @@ class FaultWrapper(base_wsgi.Middleware):
         # including those that are safe to expose, see bug 1021373
         if safe:
             msg = (inner.msg if isinstance(inner, exception.CinderException)
-                   else unicode(inner))
+                   else six.text_type(inner))
             params = {'exception': inner.__class__.__name__,
                       'explanation': msg}
             outer.explanation = _('%(exception)s: %(explanation)s') % params
index 76d102f5ee1d24656bcca6eb7daaa240e53ed6d4..a867555fa1dff70ccfc3b6ac892defc2c677ba9d 100644 (file)
@@ -17,6 +17,7 @@ import os.path
 import re
 
 from lxml import etree
+import six
 
 from cinder.i18n import _
 from cinder import utils
@@ -338,12 +339,12 @@ class TemplateElement(object):
 
         # Start with the text...
         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...
         for key, value in self.attrib.items():
             try:
-                elem.set(key, unicode(value(obj, True)))
+                elem.set(key, six.text_type(value(obj, True)))
             except KeyError:
                 # Attribute has no value, so don't include it
                 pass
index 1750fdd04744fd2f89f51ee655d522c9b1cf3fc3..cb7d7317ea7a15a8f6355821e90df0ac21711b4b 100644 (file)
@@ -80,7 +80,7 @@ class ChunkedBackupDriver(driver.BackupDriver):
             pass
 
         err = _('unsupported compression algorithm: %s') % algorithm
-        raise ValueError(unicode(err))
+        raise ValueError(err)
 
     def __init__(self, context, chunk_size_bytes, sha_block_size_bytes,
                  backup_default_container, enable_progress_timer,
index f0ee60d38c436c3ff824e96c8d1cce44d3a16f80..a975984540b582a017d67a1d51c7220fe6b54f61 100644 (file)
@@ -493,7 +493,7 @@ class CephBackupDriver(driver.BackupDriver):
             p1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
         except OSError as e:
-            LOG.error(_LE("Pipe1 failed - %s ") % unicode(e))
+            LOG.error(_LE("Pipe1 failed - %s "), e)
             raise
 
         # NOTE(dosaboy): ensure that the pipe is blocking. This is to work
@@ -507,7 +507,7 @@ class CephBackupDriver(driver.BackupDriver):
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
         except OSError as e:
-            LOG.error(_LE("Pipe2 failed - %s ") % unicode(e))
+            LOG.error(_LE("Pipe2 failed - %s "), e)
             raise
 
         p1.stdout.close()
index 0c6096bc05f7695fbdf82b9a818615b46954e1ed..8bdde385222dd33ce4077dedbdca9d097b74dc0e 100644 (file)
@@ -38,6 +38,7 @@ from oslo_log import log as logging
 import oslo_messaging as messaging
 from oslo_utils import excutils
 from oslo_utils import importutils
+import six
 
 from cinder.backup import driver
 from cinder.backup import rpcapi as backup_rpcapi
@@ -298,7 +299,7 @@ class BackupManager(manager.SchedulerDependentManager):
                                       {'status': 'available'})
                 self.db.backup_update(context, backup_id,
                                       {'status': 'error',
-                                       'fail_reason': unicode(err)})
+                                       'fail_reason': six.text_type(err)})
 
         self.db.volume_update(context, volume_id, {'status': 'available'})
         backup = self.db.backup_update(context, backup_id,
@@ -406,7 +407,7 @@ class BackupManager(manager.SchedulerDependentManager):
                     self.db.backup_update(context, backup_id,
                                           {'status': 'error',
                                            'fail_reason':
-                                           unicode(err)})
+                                           six.text_type(err)})
 
         LOG.info(_LI('Delete backup started, backup: %s.'), 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,
                                           {'status': 'error',
                                            'fail_reason':
-                                           unicode(err)})
+                                           six.text_type(err)})
 
         # Get reservations
         try:
@@ -528,7 +529,7 @@ class BackupManager(manager.SchedulerDependentManager):
             backup_url = backup_service.export_record(backup)
             backup_record['backup_url'] = backup_url
         except Exception as err:
-            msg = unicode(err)
+            msg = six.text_type(err)
             raise exception.InvalidBackup(reason=msg)
 
         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_options = backup_service.import_record(backup_url)
             except Exception as err:
-                msg = unicode(err)
+                msg = six.text_type(err)
                 self.db.backup_update(context,
                                       backup_id,
                                       {'status': 'error',
@@ -627,7 +628,7 @@ class BackupManager(manager.SchedulerDependentManager):
                     self.db.backup_update(context, backup_id,
                                           {'status': 'error',
                                            'fail_reason':
-                                           unicode(err)})
+                                           six.text_type(err)})
 
             LOG.info(_LI('Import record id %s metadata from driver '
                          'finished.') % backup_id)
index 0283c15671761d5570effc882ef0a4a282ec8c65..235793c17135270950ffd4325e10d24a15864281 100644 (file)
@@ -15,6 +15,7 @@
 """Exceptions for the Brick library."""
 
 from oslo_log import log as logging
+import six
 
 from cinder.i18n import _
 
@@ -65,7 +66,7 @@ class BrickException(Exception):
         super(BrickException, self).__init__(message)
 
     def __unicode__(self):
-        return unicode(self.msg)
+        return six.text_type(self.msg)
 
 
 class NotFound(BrickException):
index 84cfd236b16399edb2bcb36192fb73dcc750573f..9e53667354c4487563a38d5752dae2c991cdf909 100644 (file)
@@ -112,7 +112,7 @@ class CinderException(Exception):
         return self.kwargs['message'] is None or '%(message)' in self.message
 
     def __unicode__(self):
-        return unicode(self.msg)
+        return six.text_type(self.msg)
 
 
 class VolumeBackendAPIException(CinderException):
index 05c585d7d6e3c47f8eac8f2d2bfbdd168d4d5e75..999e1057a9ba6b0f0df6446419623a9c2ca4bb72 100644 (file)
@@ -198,6 +198,16 @@ def check_datetime_now(logical_line, noqa):
         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):
     register(no_vi_headers)
     register(no_translate_debug_logs)
@@ -209,3 +219,4 @@ def factory(register):
     register(check_no_contextlib_nested)
     register(check_datetime_now)
     register(validate_log_translations)
+    register(check_unicode_usage)
index 168b8677e855841dd86128d8c491843adbed6c3f..39986dbcf0a4d574bd4291607f278db7ed3cd790 100644 (file)
@@ -241,7 +241,7 @@ class String(FieldType):
         # FIXME(danms): We should really try to avoid the need to do this
         if isinstance(value, (six.string_types, int, long, float,
                               datetime.datetime)):
-            return unicode(value)
+            return six.text_type(value)
         else:
             raise ValueError(_('A string is required here, not %s') %
                              value.__class__.__name__)
index 4a55cb5e15e4b9c1455a0356dc24b83a71454e09..ff14e3865fd3402af49d552a87b8ee259285282c 100644 (file)
@@ -22,6 +22,7 @@ import mock
 from oslo_concurrency import processutils as putils
 from oslo_config import cfg
 from oslo_log import log as logging
+import six
 
 from cinder.brick import exception
 from cinder.brick.initiator import connector
@@ -715,7 +716,7 @@ class FibreChannelConnectorTestCase(ConnectorTestCase):
         name = 'volume-00000001'
         vol = {'id': 1, 'name': name}
         # Should work for string, unicode, and list
-        wwns = ['1234567890123456', unicode('1234567890123456'),
+        wwns = ['1234567890123456', six.text_type('1234567890123456'),
                 ['1234567890123456', '1234567890123457']]
         for wwn in wwns:
             connection_info = self.fibrechan_connection(vol, location, wwn)
index b6bc939a9e8b3d2b1f21f3fe21e48b6462ff2e6a..9799b80467b93aed0837e96e11065f79b9c2b562 100644 (file)
@@ -18,6 +18,8 @@
 from cinder.brick import exception
 from cinder import test
 
+import six
+
 
 class BrickExceptionTestCase(test.TestCase):
     def test_default_error_msg(self):
@@ -25,17 +27,18 @@ class BrickExceptionTestCase(test.TestCase):
             message = "default message"
 
         exc = FakeBrickException()
-        self.assertEqual(unicode(exc), 'default message')
+        self.assertEqual(six.text_type(exc), 'default message')
 
     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):
         class FakeBrickException(exception.BrickException):
             message = "default message: %(code)s"
 
         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):
         # NOTE(dprince): disable format errors for this test
@@ -45,7 +48,8 @@ class BrickExceptionTestCase(test.TestCase):
             message = "default message: %(mispelled_code)s"
 
         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):
         class FakeBrickException(exception.BrickException):
index d5712459fec35f21fbb42b54fea262002c5c18a2..00bbc198842a318666016da348e54fcd15674a43 100644 (file)
@@ -18,6 +18,8 @@
 from cinder import exception
 from cinder import test
 
+import six
+
 
 class FakeNotifier(object):
     """Acts like the cinder.openstack.common.notifier.api module."""
@@ -54,17 +56,18 @@ class CinderExceptionTestCase(test.TestCase):
             message = "default message"
 
         exc = FakeCinderException()
-        self.assertEqual(unicode(exc), 'default message')
+        self.assertEqual(six.text_type(exc), 'default message')
 
     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):
         class FakeCinderException(exception.CinderException):
             message = "default message: %(code)s"
 
         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):
         # NOTE(dprince): disable format errors for this test
@@ -74,7 +77,8 @@ class CinderExceptionTestCase(test.TestCase):
             message = "default message: %(misspelled_code)s"
 
         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):
         class FakeCinderException(exception.CinderException):
@@ -107,14 +111,14 @@ class CinderExceptionTestCase(test.TestCase):
             message = 'FakeCinderException: %(message)s'
 
         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):
         class FakeCinderException(exception.CinderException):
             message = 'Error %(code)d: %(message)s'
 
         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):
         class FakeCinderException(exception.CinderException):
@@ -123,4 +127,4 @@ class CinderExceptionTestCase(test.TestCase):
         msg = 'test message'
         exc1 = Exception(msg)
         exc2 = FakeCinderException(message=exc1)
-        self.assertEqual(unicode(exc2), 'Exception: test message')
+        self.assertEqual(six.text_type(exc2), 'Exception: test message')
index db69b8a1a148d5fc4dd5343378afd48050dacea8..338a6019ea03851eb41aa7f416d69fefae6858e4 100644 (file)
@@ -199,3 +199,9 @@ class HackingTestCase(test.TestCase):
             "LOG.error(_LE('foo')", "foo.py"))))
         self.assertEqual(0, len(list(checks.validate_log_translations(
             "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))))
index 3a7b8a99640072050f6bd2a0318cd379e3c4ef35..b239f8db08d5da70a020d39833654694b82ce6e5 100644 (file)
@@ -18,6 +18,7 @@
 import mock
 from oslo_log import log as logging
 from oslo_utils import units
+import six
 
 from cinder import context
 from cinder import exception
@@ -331,7 +332,7 @@ class TestHPLeftHandCLIQISCSIDriver(HPLeftHandBaseDriver, test.TestCase):
         def test_paramiko_1_13_0(cliq_args):
 
             # paramiko 1.13.0 now returns unicode
-            output = unicode(
+            output = six.text_type(
                 '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n'
                 '<gauche version="1.0">\n\n  <response description="Operation'
                 ' succeeded." name="CliqSuccess" processingTime="423" '
index 8c9be80f6104a32cfb18bd4f5cb06320e65a601b..f0cac7e4b2d70de6b50c3d00ee88ac8cd27434fd 100644 (file)
@@ -161,8 +161,8 @@ class FlashSystemDriver(san.SanDriver):
                 invalid_ch_in_host = invalid_ch_in_host + ch
 
         host_name = connector['host']
-        if isinstance(host_name, unicode):
-            unicode_host_name_filter = dict((ord(unicode(char)), u'-')
+        if isinstance(host_name, six.text_type):
+            unicode_host_name_filter = dict((ord(six.text_type(char)), u'-')
                                             for char in invalid_ch_in_host)
             host_name = host_name.translate(unicode_host_name_filter)
         elif isinstance(host_name, str):
index 63df538b84630c625f6967d09a046e730dfbb735..ad736efb418efff458707448288c92a580390108 100644 (file)
@@ -48,6 +48,7 @@ from oslo_utils import importutils
 from oslo_utils import timeutils
 from oslo_utils import uuidutils
 from osprofiler import profiler
+import six
 from taskflow import exceptions as tfe
 
 from cinder import compute
@@ -942,7 +943,7 @@ class VolumeManager(manager.SchedulerDependentManager):
                 self._delete_image(context, image_meta['id'], image_service)
 
             with excutils.save_and_reraise_exception():
-                payload['message'] = unicode(error)
+                payload['message'] = six.text_type(error)
         finally:
             if not volume['volume_attachment']:
                 self.db.volume_update(context, volume_id,