Replace unicode with six.text_type

The Unicode type is 'unicode' in Python 2 and 'str' on Python 3: replace
unicode with six.text_type to make Nova compatible with Python 2 and
Python 3.

This patch was generated by the following tool (revision e760664379c3) with the
operation "unicode":
https://bitbucket.org/haypo/misc/src/tip/python/sixer.py

Manual change:

* Replace "isinstance(value, str) or isinstance(value, unicode)"
  with "isinstance(value, six.string_types)" in nova/api/ec2/ec2utils.py

* Revert changes in strings in:

  - nova/compute/api.py
  - nova/hacking/checks.py
  - nova/tests/unit/api/openstack/test_wsgi.py
  - nova/utils.py

* Revert changes in nova/tests/unit/test_hacking.py: tests must use
  "unicode()". The nova.hacking module will probably need other changes
  to support Python 3.

* Reformat nova/tests/unit/objects/test_instance_action.py and
  nova/tests/unit/virt/hyperv/test_hypervapi.py to 80 columns

Blueprint nova-python3
Change-Id: I7ced236b6f8f8b6a5d2e7fee3c4f0ba4d72c21fb
This commit is contained in:
Victor Stinner 2015-05-04 19:02:51 +02:00
parent e8707aa3b5
commit 67eedf4fa0
25 changed files with 58 additions and 40 deletions

View File

@ -24,6 +24,7 @@ It is used via a single directive in the .rst file
import re
import six
from six.moves import configparser
from docutils import nodes
@ -111,7 +112,7 @@ class SupportMatrixTarget(object):
class SupportMatrixDirective(rst.Directive):
option_spec = {
'support-matrix': unicode,
'support-matrix': six.text_type,
}
def run(self):

View File

@ -572,7 +572,7 @@ def ec2_error_ex(ex, req, code=None, message=None, unexpected=False):
log_fun(log_msg, log_msg_args, context=context)
if ex.args and not message and (not unexpected or status < 500):
message = unicode(ex.args[0])
message = six.text_type(ex.args[0])
if unexpected:
# Log filtered environment for unexpected errors.
env = req.environ.copy()

View File

@ -26,6 +26,7 @@ import time
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import timeutils
import six
from nova.api.ec2 import ec2utils
from nova.api.ec2 import inst_state
@ -726,7 +727,7 @@ class CloudController(object):
return source_project_id
def create_security_group(self, context, group_name, group_description):
if isinstance(group_name, unicode):
if isinstance(group_name, six.text_type):
group_name = utils.utf8(group_name)
if CONF.ec2_strict_validation:
# EC2 specification gives constraints for name and description:

View File

@ -20,6 +20,7 @@ import re
from oslo_log import log as logging
from oslo_utils import timeutils
from oslo_utils import uuidutils
import six
from nova import context
from nova import exception
@ -413,7 +414,7 @@ def dict_from_dotted_str(items):
for key, value in items:
parts = key.split(".")
key = str(camelcase_to_underscore(parts[0]))
if isinstance(value, str) or isinstance(value, unicode):
if isinstance(value, six.string_types):
# NOTE(vish): Automatically convert strings back
# into their respective values
value = _try_convert(value)

View File

@ -21,6 +21,7 @@ WSGI middleware for OpenStack API controllers.
from oslo_config import cfg
from oslo_log import log as logging
import routes
import six
import stevedore
import webob.dec
import webob.exc
@ -90,7 +91,7 @@ class FaultWrapper(base_wsgi.Middleware):
status, webob.exc.HTTPInternalServerError)()
def _error(self, inner, req):
LOG.exception(_LE("Caught error: %s"), unicode(inner))
LOG.exception(_LE("Caught error: %s"), six.text_type(inner))
safe = getattr(inner, 'safe', False)
headers = getattr(inner, 'headers', None)

View File

@ -908,7 +908,7 @@ class ServersController(wsgi.Controller):
if not image_href and create_kwargs.get('block_device_mapping'):
return ''
elif image_href:
return self._image_uuid_from_href(unicode(image_href))
return self._image_uuid_from_href(six.text_type(image_href))
else:
msg = _("Missing imageRef attribute")
raise exc.HTTPBadRequest(explanation=msg)

View File

@ -842,7 +842,7 @@ class Controller(wsgi.Controller):
def _image_ref_from_req_data(self, data):
try:
return unicode(data['server']['imageRef'])
return six.text_type(data['server']['imageRef'])
except (TypeError, KeyError):
msg = _("Missing imageRef attribute")
raise exc.HTTPBadRequest(explanation=msg)

View File

@ -775,7 +775,7 @@ class Resource(wsgi.Application):
if body:
msg = _("Action: '%(action)s', calling method: %(meth)s, body: "
"%(body)s") % {'action': action,
'body': unicode(body, 'utf-8'),
'body': six.text_type(body, 'utf-8'),
'meth': str(meth)}
LOG.debug(strutils.mask_password(msg))
else:

View File

@ -119,7 +119,7 @@ def create(name, memory, vcpus, root_gb, ephemeral_gb=0, flavorid=None,
# NOTE(vish): Internally, flavorid is stored as a string but it comes
# in through json as an integer, so we convert it here.
flavorid = unicode(flavorid)
flavorid = six.text_type(flavorid)
# ensure leading/trailing whitespaces not present.
if flavorid.strip() != flavorid:

View File

@ -21,6 +21,7 @@ import traceback
import netifaces
from oslo_config import cfg
from oslo_log import log
import six
from nova import block_device
from nova.compute import power_state
@ -55,7 +56,7 @@ def exception_to_dict(fault):
# just because there is an unexpected error retrieving the message
except Exception:
try:
message = unicode(fault)
message = six.text_type(fault)
except Exception:
message = None
if not message:
@ -76,7 +77,7 @@ def _get_fault_details(exc_info, error_code):
tb = exc_info[2]
if tb:
details = ''.join(traceback.format_tb(tb))
return unicode(details)
return six.text_type(details)
def add_instance_fault_from_exc(context, instance, fault, exc_info=None):

View File

@ -368,7 +368,7 @@ class FloatingIP(object):
"""Performs db and driver calls to associate floating ip & fixed ip."""
interface = CONF.public_interface or interface
@utils.synchronized(unicode(floating_address))
@utils.synchronized(six.text_type(floating_address))
def do_associate():
# associate floating ip
floating = objects.FloatingIP.associate(context, floating_address,
@ -464,7 +464,7 @@ class FloatingIP(object):
"""Performs db and driver calls to disassociate floating ip."""
interface = CONF.public_interface or interface
@utils.synchronized(unicode(address))
@utils.synchronized(six.text_type(address))
def do_disassociate():
# NOTE(vish): Note that we are disassociating in the db before we
# actually remove the ip address on the host. We are

View File

@ -245,7 +245,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 in field %(attr)s, '
'not %(type)s') %

View File

@ -17,6 +17,7 @@ import datetime
import mock
from oslo_serialization import jsonutils
import six
import webob
from nova.api.openstack.compute.contrib import flavor_access as \
@ -114,7 +115,7 @@ class FlavorManageTestV21(test.NoDBTestCase):
"vcpus": 2,
"disk": 1,
"OS-FLV-EXT-DATA:ephemeral": 1,
"id": unicode('1234'),
"id": six.text_type('1234'),
"swap": 512,
"rxtx_factor": 1,
"os-flavor-access:is_public": True,
@ -156,7 +157,7 @@ class FlavorManageTestV21(test.NoDBTestCase):
"vcpus": 2,
"disk": 1,
"OS-FLV-EXT-DATA:ephemeral": 1,
"id": unicode('1234'),
"id": six.text_type('1234'),
"swap": 512,
"rxtx_factor": 1,
"os-flavor-access:is_public": True,

View File

@ -14,6 +14,7 @@
# under the License.
from oslo_utils import uuidutils
import six
import webob
from nova.api.openstack.compute.contrib import admin_actions as \
@ -214,7 +215,7 @@ class MigrateServerTestsV21(admin_only_action_common.CommonTests):
self.controller._migrate_live,
self.req, instance.uuid, body=body)
if check_response:
self.assertIn(unicode(fake_exc), ex.explanation)
self.assertIn(six.text_type(fake_exc), ex.explanation)
def test_migrate_live_compute_service_unavailable(self):
self._test_migrate_live_failed_with_exception(

View File

@ -13,6 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
import six.moves.urllib.parse as urlparse
import webob
@ -607,7 +608,7 @@ class DisabledFlavorsWithRealDBTestV21(test.TestCase):
del inst_type['id']
inst_type['name'] += '.disabled'
inst_type['flavorid'] = unicode(max(
inst_type['flavorid'] = six.text_type(max(
[int(flavor['flavorid']) for flavor in inst_types]) + 1)
inst_type['disabled'] = True

View File

@ -16,6 +16,7 @@
import mock
from oslo_serialization import jsonutils
import six
import webob
import webob.dec
import webob.exc
@ -56,7 +57,7 @@ class TestFaultWrapper(test.NoDBTestCase):
# translate().
mock_translate.assert_any_call(u'Should be translated.', None)
# The return value from translate() should appear in the response.
self.assertIn("I've been translated!", unicode(response.body))
self.assertIn("I've been translated!", six.text_type(response.body))
class TestFaults(test.NoDBTestCase):

View File

@ -16,6 +16,7 @@ import traceback
import mock
from oslo_utils import timeutils
import six
from nova import db
from nova.objects import instance_action
@ -292,13 +293,14 @@ class _TestInstanceActionEventObject(object):
timeutils.set_time_override(override_time=NOW)
test_class = instance_action.InstanceActionEvent
expected_packed_values = test_class.pack_action_event_finish(
self.context, 'fake-uuid', 'fake-event', 'val', unicode('fake-tb'))
self.context, 'fake-uuid', 'fake-event', 'val',
six.text_type('fake-tb'))
expected_packed_values['finish_time'] = timeutils.utcnow()
mock_finish.return_value = fake_event
event = test_class.event_finish_with_failure(
self.context, 'fake-uuid', 'fake-event', exc_val='val',
exc_tb=unicode('fake-tb'), want_result=True)
exc_tb=six.text_type('fake-tb'), want_result=True)
mock_finish.assert_called_once_with(self.context,
expected_packed_values)
self.compare_obj(event, fake_event)

View File

@ -18,6 +18,7 @@ Tests for availability zones
"""
from oslo_config import cfg
import six
from nova import availability_zones as az
from nova import context
@ -125,7 +126,7 @@ class AvailabilityZoneTestCases(test.TestCase):
service = self._create_service_with_topic('network', self.host)
services = db.service_get_all(self.context)
az.set_availability_zones(self.context, services)
self.assertIsInstance(services[0]['host'], unicode)
self.assertIsInstance(services[0]['host'], six.text_type)
cached_key = az._make_cache_key(services[0]['host'])
self.assertIsInstance(cached_key, str)
self._destroy_service(service)

View File

@ -25,6 +25,7 @@ import mock
from mox3 import mox
from oslo_config import cfg
from oslo_utils import units
import six
from nova.api.metadata import base as instance_metadata
from nova import context
@ -404,7 +405,7 @@ class HyperVAPITestCase(HyperVAPIBaseTestCase):
target_lun, target_portal, True)
vmutils.VMUtils.create_nic(mox.Func(self._check_vm_name),
mox.IsA(str), mox.IsA(unicode)).InAnyOrder()
mox.IsA(str), mox.IsA(six.text_type)).InAnyOrder()
if setup_vif_mocks_func:
setup_vif_mocks_func()
@ -464,7 +465,7 @@ class HyperVAPITestCase(HyperVAPIBaseTestCase):
fake.PathUtils.copyfile(mox.IsA(str), mox.IsA(str))
m = vhdutils.VHDUtils.get_internal_vhd_size_by_file_size(
mox.IsA(unicode), mox.IsA(object))
mox.IsA(six.text_type), mox.IsA(object))
m.AndReturn(1025)
vhdutils.VHDUtils.resize_vhd(mox.IsA(str), mox.IsA(object),
@ -511,7 +512,7 @@ class HyperVAPITestCase(HyperVAPIBaseTestCase):
if not (cow and vhd_format == constants.DISK_FORMAT_VHD):
m = vhdutils.VHDUtils.get_internal_vhd_size_by_file_size(
mox.IsA(unicode), mox.IsA(object))
mox.IsA(six.text_type), mox.IsA(object))
m.AndReturn(1025)
vhdutils.VHDUtils.resize_vhd(mox.IsA(str), mox.IsA(object),
is_file_max_size=False)
@ -950,7 +951,7 @@ class HyperVAPITestCase(HyperVAPIBaseTestCase):
m.AndReturn(self._test_instance_dir)
self._mox.StubOutWithMock(fake.PathUtils, 'exists')
m = fake.PathUtils.exists(mox.IsA(unicode))
m = fake.PathUtils.exists(mox.IsA(six.text_type))
m.AndReturn(True)
fake_parent_vhd_path = (os.path.join('FakeParentPath', '%s.vhd' %
@ -960,15 +961,16 @@ class HyperVAPITestCase(HyperVAPIBaseTestCase):
m.AndReturn({'ParentPath': fake_parent_vhd_path,
'MaxInternalSize': 1})
m = vhdutils.VHDUtils.get_internal_vhd_size_by_file_size(
mox.IsA(unicode), mox.IsA(object))
mox.IsA(six.text_type), mox.IsA(object))
m.AndReturn(1025)
vhdutils.VHDUtils.reconnect_parent_vhd(mox.IsA(str), mox.IsA(unicode))
vhdutils.VHDUtils.reconnect_parent_vhd(mox.IsA(str),
mox.IsA(six.text_type))
m = vhdutils.VHDUtils.get_vhd_info(mox.IsA(unicode))
m = vhdutils.VHDUtils.get_vhd_info(mox.IsA(six.text_type))
m.AndReturn({'MaxInternalSize': 1024})
m = fake.PathUtils.exists(mox.IsA(unicode))
m = fake.PathUtils.exists(mox.IsA(six.text_type))
m.AndReturn(True)
m = fake.PathUtils.get_instance_dir(mox.IsA(str))

View File

@ -20,6 +20,7 @@ import mock
from oslo_config import cfg
from oslo_serialization import jsonutils
from oslo_utils import uuidutils
import six
from nova.api.metadata import base as instance_metadata
from nova.compute import power_state as nova_states
@ -1116,7 +1117,7 @@ class IronicDriverTestCase(test.NoDBTestCase):
node=node_uuid)
network_info = utils.get_test_network_info()
port_id = unicode(network_info[0]['id'])
port_id = six.text_type(network_info[0]['id'])
expected_patch = [{'op': 'add',
'path': '/extra/vif_port_id',
'value': port_id}]

View File

@ -24,6 +24,7 @@ import fixtures
import mock
from oslo_concurrency import processutils
from oslo_config import cfg
import six
from nova.compute import arch
from nova import exception
@ -1374,7 +1375,7 @@ Setting up iSCSI targets: unused
self.stubs.Set(linuxscsi, 'find_multipath_device', lambda x: devices)
self.stubs.Set(linuxscsi, 'remove_device', lambda x: None)
# 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(self.vol,
@ -1464,7 +1465,7 @@ Setting up iSCSI targets: unused
self.stubs.Set(linuxscsi, 'find_multipath_device', lambda x: devices)
self.stubs.Set(linuxscsi, 'remove_device', lambda x: None)
# Should work for string, unicode, and list
wwns = ['1234567890123456', unicode('1234567890123456'),
wwns = ['1234567890123456', six.text_type('1234567890123456'),
['1234567890123456']]
expected_remove_calls = []
for wwn in wwns:

View File

@ -17,6 +17,7 @@ import io
import os
import mock
import six
from nova import test
from nova import utils
@ -120,7 +121,7 @@ class FakeMount(object):
class TestDiskImage(test.NoDBTestCase):
def mock_proc_mounts(self, mock_open):
response = io.StringIO(unicode(PROC_MOUNTS_CONTENTS))
response = io.StringIO(six.text_type(PROC_MOUNTS_CONTENTS))
mock_open.return_value = response
@mock.patch('__builtin__.open')

View File

@ -20,6 +20,7 @@ from oslo_utils import uuidutils
from oslo_vmware import exceptions as vexc
from oslo_vmware.objects import datastore as ds_obj
from oslo_vmware import vim_util as vutil
import six
from nova.compute import power_state
from nova import context
@ -1315,9 +1316,9 @@ class VMwareVMOpsTestCase(test.NoDBTestCase):
self._verify_spawn_method_calls(_call_method, extras)
dc_ref = 'fake_dc_ref'
source_file = unicode('[fake_ds] vmware_base/%s/%s.vmdk' %
source_file = six.text_type('[fake_ds] vmware_base/%s/%s.vmdk' %
(self._image_id, self._image_id))
dest_file = unicode('[fake_ds] vmware_base/%s/%s.%d.vmdk' %
dest_file = six.text_type('[fake_ds] vmware_base/%s/%s.%d.vmdk' %
(self._image_id, self._image_id,
self._instance['root_gb']))
# TODO(dims): add more tests for copy_virtual_disk after

View File

@ -424,7 +424,7 @@ def utf8(value):
http://github.com/facebook/tornado/blob/master/tornado/escape.py
"""
if isinstance(value, unicode):
if isinstance(value, six.text_type):
return value.encode('utf-8')
assert isinstance(value, str)
return value
@ -600,7 +600,7 @@ def make_dev_path(dev, partition=None, base='/dev'):
def sanitize_hostname(hostname):
"""Return a hostname which conforms to RFC-952 and RFC-1123 specs."""
if isinstance(hostname, unicode):
if isinstance(hostname, six.text_type):
hostname = hostname.encode('latin-1', 'ignore')
hostname = re.sub('[ _]', '-', hostname)
@ -1076,7 +1076,7 @@ def get_system_metadata_from_image(image_meta, flavor=None):
prefix_format = SM_IMAGE_PROP_PREFIX + '%s'
for key, value in image_meta.get('properties', {}).iteritems():
new_value = safe_truncate(unicode(value), 255)
new_value = safe_truncate(six.text_type(value), 255)
system_meta[prefix_format % key] = new_value
for key in SM_INHERITABLE_KEYS:

View File

@ -977,7 +977,7 @@ class IronicDriver(virt_driver.ComputeDriver):
# not needed if no vif are defined
for vif, pif in zip(network_info, ports):
# attach what neutron needs directly to the port
port_id = unicode(vif['id'])
port_id = six.text_type(vif['id'])
patch = [{'op': 'add',
'path': '/extra/vif_port_id',
'value': port_id}]