Remove six.PY2 and six.PY3

Remove six.PY2 and six.PY3.
Subsequent patches will replace other six usages.

Change-Id: Iccce0ab50eee515e533ab36c8e7adc10cb3f7019
Implements: blueprint six-removal
Signed-off-by: Takashi Natsume <takanattie@gmail.com>
This commit is contained in:
Takashi Natsume 2020-05-09 09:34:25 +00:00
parent 1c2cccab71
commit 28ed0c5c9a
26 changed files with 52 additions and 194 deletions

View File

@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
from six.moves import range
from webob import exc
@ -47,7 +46,7 @@ def convert_password(context, password):
Password is stored with the keys 'password_0' -> 'password_3'.
"""
password = password or ''
if six.PY3 and isinstance(password, bytes):
if isinstance(password, bytes):
password = password.decode('utf-8')
meta = {}

View File

@ -14,18 +14,13 @@
# under the License.
import re
from urllib import request as urllib2
from oslo_log import log as logging
import paste.urlmap
import six
from nova.api.openstack import wsgi
if six.PY2:
import urllib2
else:
from urllib import request as urllib2
LOG = logging.getLogger(__name__)

View File

@ -287,26 +287,17 @@ class ResponseObject(object):
response = webob.Response(body=body)
response.status_int = self.code
for hdr, val in self._headers.items():
if six.PY2:
# In Py2.X Headers must be a UTF-8 encode str.
response.headers[hdr] = encodeutils.safe_encode(val)
else:
# In Py3.X Headers must be a str that was first safely
# encoded to UTF-8 (to catch any bad encodings) and then
# decoded back to a native str.
response.headers[hdr] = encodeutils.safe_decode(
encodeutils.safe_encode(val))
# In Py3.X Headers must be a str that was first safely
# encoded to UTF-8 (to catch any bad encodings) and then
# decoded back to a native str.
response.headers[hdr] = encodeutils.safe_decode(
encodeutils.safe_encode(val))
# Deal with content_type
if not isinstance(content_type, six.text_type):
content_type = six.text_type(content_type)
if six.PY2:
# In Py2.X Headers must be a UTF-8 encode str.
response.headers['Content-Type'] = encodeutils.safe_encode(
content_type)
else:
# In Py3.X Headers must be a str.
response.headers['Content-Type'] = encodeutils.safe_decode(
encodeutils.safe_encode(content_type))
# In Py3.X Headers must be a str.
response.headers['Content-Type'] = encodeutils.safe_decode(
encodeutils.safe_encode(content_type))
return response
@property
@ -573,13 +564,9 @@ class Resource(wsgi.Application):
for hdr, val in list(response.headers.items()):
if not isinstance(val, six.text_type):
val = six.text_type(val)
if six.PY2:
# In Py2.X Headers must be UTF-8 encoded string
response.headers[hdr] = encodeutils.safe_encode(val)
else:
# In Py3.X Headers must be a string
response.headers[hdr] = encodeutils.safe_decode(
encodeutils.safe_encode(val))
# In Py3.X Headers must be a string
response.headers[hdr] = encodeutils.safe_decode(
encodeutils.safe_encode(val))
if not request.api_version_request.is_null():
response.headers[API_VERSION_REQUEST_HEADER] = \

View File

@ -17,8 +17,6 @@
import collections
import six
from nova import cache_utils
import nova.conf
from nova import objects
@ -51,8 +49,6 @@ def reset_cache():
def _make_cache_key(host):
if six.PY2:
host = host.encode('utf-8')
return "azcache-%s" % host

View File

@ -169,10 +169,7 @@ class DbCommands(object):
else:
pt.add_row([k, v])
if six.PY2:
print(encodeutils.safe_encode(pt.get_string()))
else:
print(encodeutils.safe_encode(pt.get_string()).decode())
print(encodeutils.safe_encode(pt.get_string()).decode())
@args('--local_cell', action='store_true',
help='Only sync db in the local cell: do not attempt to fan-out '

View File

@ -7988,12 +7988,6 @@ class ComputeManager(manager.Manager):
if events:
LOG.debug('Not waiting for events after pre_live_migration: '
'%s. ', events, instance=instance)
# This is a bit weird, but we need to clear sys.exc_info() so that
# oslo.log formatting does not inadvertently use it later if an
# error message is logged without an explicit exc_info. This is
# only a problem with python 2.
if six.PY2:
sys.exc_clear()
except exception.VirtualInterfacePlugException:
with excutils.save_and_reraise_exception():
LOG.exception('Failed waiting for network virtual interfaces '

View File

@ -65,15 +65,8 @@ class TenantSock(object):
new_frames, closed = self.reqhandler.recv_frames()
# flatten frames onto queue
for frame in new_frames:
# The socket returns (byte) strings in Python 2...
if six.PY2:
self.queue.extend(frame)
# ...and integers in Python 3. For the Python 3 case, we need
# to convert these to characters using 'chr' and then, as this
# returns unicode, convert the result to byte strings.
else:
self.queue.extend(
[six.binary_type(chr(c), 'ascii') for c in frame])
self.queue.extend(
[six.binary_type(chr(c), 'ascii') for c in frame])
if closed:
break

View File

@ -18,7 +18,6 @@ import copy
from oslo_config import cfg
from oslo_log import log as logging
import six
from nova import exception
from nova.objects import fields
@ -410,10 +409,6 @@ class PciDeviceStats(object):
def __eq__(self, other):
return self.pools == other.pools
if six.PY2:
def __ne__(self, other):
return not (self == other)
def to_device_pools_obj(self):
"""Return the contents of the pools as a PciDevicePoolList object."""
stats = [x for x in self]

View File

@ -25,6 +25,7 @@ import nova.monkey_patch # noqa
import abc
import collections
import contextlib
import copy
import datetime
import inspect
@ -65,11 +66,6 @@ from nova.tests.unit import policy_fixture
from nova import utils
from nova.virt import images
if six.PY2:
import contextlib2 as contextlib
else:
import contextlib
CONF = cfg.CONF
logging.register_options(CONF)

View File

@ -10,8 +10,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
from nova.db.sqlalchemy import models
from nova.tests.functional.api_sample_tests import test_servers
@ -39,8 +37,7 @@ class ServerTagsJsonTest(test_servers.ServersSampleBase):
subs['instance_name'] = r'instance-\d{8}'
subs['hypervisor_hostname'] = r'[\w\.\-]+'
subs['cdrive'] = '.*'
subs['user_data'] = (self.user_data if six.PY2
else self.user_data.decode('utf-8'))
subs['user_data'] = self.user_data.decode('utf-8')
return subs
def _put_server_tags(self):

View File

@ -68,8 +68,7 @@ class ServersSampleBase(api_sample_base.ApiSampleTestBaseV21):
'glance_host': self._get_glance_host(),
'access_ip_v4': '1.2.3.4',
'access_ip_v6': '80fe::',
'user_data': (self.user_data if six.PY2
else self.user_data.decode('utf-8')),
'user_data': self.user_data.decode('utf-8'),
'uuid': '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}'
'-[0-9a-f]{4}-[0-9a-f]{12}',
'name': 'new-server-test' if name is None else name,
@ -129,8 +128,7 @@ class ServersSampleJsonTest(ServersSampleBase):
subs['mac_addr'] = '(?:[a-f0-9]{2}:){5}[a-f0-9]{2}'
subs['access_ip_v4'] = '1.2.3.4'
subs['access_ip_v6'] = '80fe::'
subs['user_data'] = (self.user_data if six.PY2
else self.user_data.decode('utf-8'))
subs['user_data'] = self.user_data.decode('utf-8')
# config drive can be a string for True or empty value for False
subs['cdrive'] = '.*'
self._verify_response('server-get-resp', subs, response, 200)
@ -156,8 +154,7 @@ class ServersSampleJsonTest(ServersSampleBase):
subs['mac_addr'] = '(?:[a-f0-9]{2}:){5}[a-f0-9]{2}'
subs['access_ip_v4'] = '1.2.3.4'
subs['access_ip_v6'] = '80fe::'
subs['user_data'] = (self.user_data if six.PY2
else self.user_data.decode('utf-8'))
subs['user_data'] = self.user_data.decode('utf-8')
# config drive can be a string for True or empty value for False
subs['cdrive'] = '.*'
self._verify_response('servers-details-resp', subs, response, 200)
@ -271,8 +268,7 @@ class ServersSampleJson263Test(ServersSampleBase):
'hostname': r'[\w\.\-]+',
'access_ip_v4': '1.2.3.4',
'access_ip_v6': '80fe::',
'user_data': (self.user_data if six.PY2
else self.user_data.decode('utf-8')),
'user_data': self.user_data.decode('utf-8'),
'cdrive': '.*',
}
@ -340,8 +336,7 @@ class ServersSampleJson266Test(ServersSampleBase):
'hostname': r'[\w\.\-]+',
'access_ip_v4': '1.2.3.4',
'access_ip_v6': '80fe::',
'user_data': (self.user_data if six.PY2
else self.user_data.decode('utf-8')),
'user_data': self.user_data.decode('utf-8'),
'cdrive': '.*',
}
@ -440,8 +435,7 @@ class ServersSampleJson271Test(ServersSampleBase):
'hostname': r'[\w\.\-]+',
'access_ip_v4': '1.2.3.4',
'access_ip_v6': '80fe::',
'user_data': (self.user_data if six.PY2
else self.user_data.decode('utf-8')),
'user_data': self.user_data.decode('utf-8'),
'cdrive': '.*',
}

View File

@ -4704,14 +4704,8 @@ class ConsumerGenerationConflictTest(
self._wait_for_server_parameter(server,
{'OS-EXT-STS:task_state': None})
# The instance action should have failed with details.
# save_and_reraise_exception gets different results between py2 and py3
# for the traceback but we want to use the more specific
# "claim_resources" for py3. We can remove this when we drop support
# for py2.
error_in_tb = 'claim_resources' if six.PY3 else 'select_destinations'
self._assert_resize_migrate_action_fail(
server, instance_actions.MIGRATE, error_in_tb)
server, instance_actions.MIGRATE, 'claim_resources')
# The migration is aborted so the instance is ACTIVE on the source
# host instead of being in VERIFY_RESIZE state.

View File

@ -17,7 +17,6 @@ import datetime
import mock
from oslo_serialization import jsonutils
import six
from nova.api.openstack import compute
from nova.compute import api as compute_api
@ -96,14 +95,9 @@ class DiskConfigTestCaseV21(test.TestCase):
self.app = compute.APIRouterV21()
def _get_expected_msg_for_invalid_disk_config(self):
if six.PY3:
return ('{{"badRequest": {{"message": "Invalid input for'
' field/attribute {0}. Value: {1}. \'{1}\' is'
' not one of [\'AUTO\', \'MANUAL\']", "code": 400}}}}')
else:
return ('{{"badRequest": {{"message": "Invalid input for'
' field/attribute {0}. Value: {1}. u\'{1}\' is'
' not one of [\'AUTO\', \'MANUAL\']", "code": 400}}}}')
return ('{{"badRequest": {{"message": "Invalid input for'
' field/attribute {0}. Value: {1}. \'{1}\' is'
' not one of [\'AUTO\', \'MANUAL\']", "code": 400}}}}')
def _setup_fake_image_service(self):
self.image_service = nova.tests.unit.image.fake.stub_out_image_service(

View File

@ -19,7 +19,6 @@ import iso8601
import mock
from oslo_serialization import jsonutils
from oslo_versionedobjects import exception as ovo_exc
import six
from nova import exception
from nova.network import model as network_model
@ -120,8 +119,6 @@ class TestString(TestField):
super(TestString, self).setUp()
self.field = fields.StringField()
self.coerce_good_values = [('foo', 'foo'), (1, '1'), (True, 'True')]
if six.PY2:
self.coerce_good_values.append((int(1), '1'))
self.coerce_bad_values = [None]
self.to_primitive_values = self.coerce_good_values[0:1]
self.from_primitive_values = self.coerce_good_values[0:1]
@ -161,8 +158,6 @@ class TestEnum(TestField):
self.field = fields.EnumField(
valid_values=['foo', 'bar', 1, 1, True])
self.coerce_good_values = [('foo', 'foo'), (1, '1'), (True, 'True')]
if six.PY2:
self.coerce_good_values.append((int(1), '1'))
self.coerce_bad_values = ['boo', 2, False]
self.to_primitive_values = self.coerce_good_values[0:1]
self.from_primitive_values = self.coerce_good_values[0:1]

View File

@ -263,12 +263,8 @@ class QueryParamsSchemaTestCase(test.NoDBTestCase):
req.api_version_request = api_version.APIVersionRequest("2.3")
ex = self.assertRaises(exception.ValidationError, self.controller.get,
req)
if six.PY3:
self.assertEqual("Invalid input for query parameters foo. Value: "
"abc. 'abc' is not a 'uuid'", six.text_type(ex))
else:
self.assertEqual("Invalid input for query parameters foo. Value: "
"abc. u'abc' is not a 'uuid'", six.text_type(ex))
self.assertEqual("Invalid input for query parameters foo. Value: "
"abc. 'abc' is not a 'uuid'", str(ex))
def test_validate_request_with_multiple_values(self):
req = fakes.HTTPRequest.blank("/tests?foos=abc")
@ -443,12 +439,9 @@ class PatternPropertiesTestCase(APIValidationTestCase):
# Note(jrosenboom): This is referencing an internal python error
# string, which is no stable interface. We need a patch in the
# jsonschema library in order to fix this properly.
if six.PY3:
detail = "expected string or bytes-like object"
else:
detail = "expected string or buffer"
self.check_validation_error(self.post, body={None: 'bar'},
expected_detail=detail)
self.check_validation_error(
self.post, body={None: 'bar'},
expected_detail="expected string or bytes-like object")
class StringTestCase(APIValidationTestCase):

View File

@ -99,12 +99,9 @@ e6fCXWECgYEAqgpGvva5kJ1ISgNwnJbwiNw0sOT9BMOsdNZBElf0kJIIy6FMPvap
self.assertIsInstance(enc, bytes)
# Comparison between bytes and str raises a TypeError
# when using python3 -bb
if six.PY2:
self.assertNotEqual(enc, self.text)
result = self._ssh_decrypt_text(self.prikey, enc)
self.assertIsInstance(result, bytes)
if six.PY3:
result = result.decode('utf-8')
result = result.decode('utf-8')
self.assertEqual(result, self.text)
def test_ssh_encrypt_failure(self):

View File

@ -204,12 +204,8 @@ class NovaExceptionTestCase(test.NoDBTestCase):
class FakeNovaException_Remote(exception.NovaException):
msg_fmt = "some message"
if six.PY2:
def __unicode__(self):
return u"print the whole trace"
else:
def __str__(self):
return "print the whole trace"
def __str__(self):
return "print the whole trace"
exc = FakeNovaException_Remote()
self.assertEqual(u"print the whole trace", six.text_type(exc))

View File

@ -25,7 +25,6 @@ import eventlet.wsgi
import mock
from oslo_config import cfg
import requests
import six
import testtools
import webob
@ -226,7 +225,7 @@ class TestWSGIServer(test.NoDBTestCase):
server.stop()
@testtools.skipIf(six.PY3, "bug/1482633: test hangs on Python 3")
@testtools.skip("bug/1482633: test hangs on Python 3")
class TestWSGIServerWithSSL(test.NoDBTestCase):
"""WSGI server with SSL tests."""

View File

@ -20,7 +20,6 @@ import mock
from oslo_service import fixture as service_fixture
from oslo_utils import encodeutils
import six
import testtools
from nova import context
from nova import exception
@ -692,23 +691,6 @@ class GuestTestCase(test.NoDBTestCase):
'destination_xml': '</xml>',
'bandwidth': 2})
@testtools.skipIf(not six.PY2, 'libvirt python3 bindings accept unicode')
def test_migrate_v3_unicode(self):
dest_xml_template = "<domain type='qemu'><name>%s</name></domain>"
name = u'\u00CD\u00F1st\u00E1\u00F1c\u00E9'
dest_xml = dest_xml_template % name
expect_dest_xml = dest_xml_template % encodeutils.to_utf8(name)
self.guest.migrate('an-uri', flags=1, migrate_uri='dest-uri',
migrate_disks=[u"disk1", u"disk2"],
destination_xml=dest_xml,
bandwidth=2)
self.domain.migrateToURI3.assert_called_once_with(
'an-uri', flags=1, params={'migrate_uri': 'dest-uri',
'migrate_disks': ['disk1',
'disk2'],
'destination_xml': expect_dest_xml,
'bandwidth': 2})
def test_abort_job(self):
self.guest.abort_job()
self.domain.abortJob.assert_called_once_with()

View File

@ -1105,9 +1105,6 @@ Active: 8381604 kB
def emulate_defineXML(xml):
conn = self.host.get_connection()
# Emulate the decoding behavior of defineXML in Python2
if six.PY2:
xml = xml.decode("utf-8")
dom = fakelibvirt.Domain(conn, xml, False)
return dom
with mock.patch.object(fakelibvirt.virConnect, "defineXML"

View File

@ -368,11 +368,9 @@ def sanitize_hostname(hostname, default_name=None):
{'hostname': name, 'truncated_name': name[:63]})
return name[:63]
if isinstance(hostname, six.text_type):
if isinstance(hostname, str):
# Remove characters outside the Unicode range U+0000-U+00FF
hostname = hostname.encode('latin-1', 'ignore')
if six.PY3:
hostname = hostname.decode('latin-1')
hostname = hostname.encode('latin-1', 'ignore').decode('latin-1')
hostname = truncate_hostname(hostname)
hostname = re.sub('[ _]', '-', hostname)
@ -809,8 +807,6 @@ def get_obj_repr_unicode(obj):
else it converts the repr() to unicode.
"""
obj_repr = repr(obj)
if not six.PY3:
obj_repr = six.text_type(obj_repr, 'utf-8')
return obj_repr
@ -1039,13 +1035,10 @@ def generate_hostid(host, project_id):
return ""
if six.PY2:
nested_contexts = contextlib.nested
else:
@contextlib.contextmanager
def nested_contexts(*contexts):
with contextlib.ExitStack() as stack:
yield [stack.enter_context(c) for c in contexts]
@contextlib.contextmanager
def nested_contexts(*contexts):
with contextlib.ExitStack() as stack:
yield [stack.enter_context(c) for c in contexts]
def normalize_rc_name(rc_name):

View File

@ -13,7 +13,6 @@
# under the License.
import collections
import fractions
import itertools
import math
import re
@ -789,12 +788,8 @@ def _pack_instance_onto_cores(host_cell, instance_cell,
threads) and 2 (number of 'orphan' CPUs) and get 2 as the number of
threads.
"""
# fractions.gcd is deprecated in favor of math.gcd starting in py35
if six.PY2:
gcd = fractions.gcd
else:
gcd = math.gcd
return gcd(threads_per_core, _orphans(instance_cell, threads_per_core))
return math.gcd(threads_per_core, _orphans(instance_cell,
threads_per_core))
def _get_pinning(threads_no, sibling_set, instance_cores):
"""Determines pCPUs/vCPUs mapping

View File

@ -35,7 +35,6 @@ from oslo_service import loopingcall
from oslo_utils import encodeutils
from oslo_utils import excutils
from oslo_utils import importutils
import six
from nova.compute import power_state
from nova import exception
@ -137,7 +136,7 @@ class Guest(object):
:returns guest.Guest: Guest ready to be launched
"""
try:
if six.PY3 and isinstance(xml, six.binary_type):
if isinstance(xml, bytes):
xml = xml.decode('utf-8')
guest = host.write_instance_config(xml)
except Exception:
@ -305,7 +304,7 @@ class Guest(object):
flags |= live and libvirt.VIR_DOMAIN_AFFECT_LIVE or 0
device_xml = conf.to_xml()
if six.PY3 and isinstance(device_xml, six.binary_type):
if isinstance(device_xml, bytes):
device_xml = device_xml.decode('utf-8')
LOG.debug("attach device xml: %s", device_xml)
@ -495,7 +494,7 @@ class Guest(object):
flags |= live and libvirt.VIR_DOMAIN_AFFECT_LIVE or 0
device_xml = conf.to_xml()
if six.PY3 and isinstance(device_xml, six.binary_type):
if isinstance(device_xml, bytes):
device_xml = device_xml.decode('utf-8')
LOG.debug("detach device xml: %s", device_xml)
@ -599,7 +598,7 @@ class Guest(object):
flags |= quiesce and libvirt.VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE or 0
device_xml = conf.to_xml()
if six.PY3 and isinstance(device_xml, six.binary_type):
if isinstance(device_xml, bytes):
device_xml = device_xml.decode('utf-8')
self._domain.snapshotCreateXML(device_xml, flags=flags)
@ -674,13 +673,6 @@ class Guest(object):
not params.get('migrate_disks')):
flags &= ~libvirt.VIR_MIGRATE_NON_SHARED_INC
# In the Python2 libvirt bindings, strings passed to
# migrateToURI3 via params must not be unicode.
if six.PY2:
params = {key: encodeutils.to_utf8(value)
if isinstance(value, six.text_type) else value
for key, value in params.items()}
self._domain.migrateToURI3(
destination, params=params, flags=flags)

View File

@ -41,12 +41,10 @@ from eventlet import greenthread
from eventlet import patcher
from eventlet import tpool
from oslo_log import log as logging
from oslo_utils import encodeutils
from oslo_utils import excutils
from oslo_utils import importutils
from oslo_utils import units
from oslo_utils import versionutils
import six
from nova.compute import utils as compute_utils
import nova.conf
@ -70,7 +68,7 @@ LOG = logging.getLogger(__name__)
native_socket = patcher.original('socket')
native_threading = patcher.original("threading")
native_Queue = patcher.original("Queue" if six.PY2 else "queue")
native_Queue = patcher.original("queue")
CONF = nova.conf.CONF
@ -720,7 +718,7 @@ class Host(object):
self._caps.host.cpu.model is not None):
try:
xml_str = self._caps.host.cpu.to_xml()
if six.PY3 and isinstance(xml_str, six.binary_type):
if isinstance(xml_str, bytes):
xml_str = xml_str.decode('utf-8')
features = self.get_connection().baselineCPU(
[xml_str],
@ -1143,8 +1141,6 @@ class Host(object):
:returns: an instance of Guest
"""
if six.PY2:
xml = encodeutils.safe_encode(xml)
domain = self.get_connection().defineXML(xml)
return libvirt_guest.Guest(domain)

View File

@ -29,7 +29,6 @@ from oslo_concurrency import lockutils
from oslo_concurrency import processutils
from oslo_log import log as logging
from oslo_utils import encodeutils
import six
import nova.conf
import nova.privsep.path
@ -95,10 +94,7 @@ class ImageCacheManager(imagecache.ImageCacheManager):
self.unexplained_images, self.originals, and self.back_swap_images.
"""
if six.PY2:
digest_size = hashlib.sha1().digestsize * 2
else:
digest_size = hashlib.sha1().digest_size * 2
digest_size = hashlib.sha1().digest_size * 2
for ent in os.listdir(base_dir):
if len(ent) == digest_size:
self._store_image(base_dir, ent, original=True)

View File

@ -26,7 +26,6 @@ from oslo_log import log as logging
from oslo_utils import excutils
from oslo_utils import strutils
from oslo_utils import versionutils
import six
import nova.conf
from nova import exception
@ -48,9 +47,6 @@ def parse_sr_info(connection_data, description=''):
params = _parse_volume_info(connection_data)
sr_identity = "%s/%s/%s" % (params['target'], params['port'],
params['targetIQN'])
# PY2 can only support taking an ascii string to uuid5
if six.PY2 and isinstance(sr_identity, six.text_type):
sr_identity = sr_identity.encode('utf-8')
sr_uuid = str(uuid.uuid5(SR_NAMESPACE, sr_identity))
else:
sr_uuid = connection_data['sr_uuid']