diff --git a/nova/api/openstack/urlmap.py b/nova/api/openstack/urlmap.py index 9c35bd31fa..8482f9b8e5 100644 --- a/nova/api/openstack/urlmap.py +++ b/nova/api/openstack/urlmap.py @@ -19,10 +19,10 @@ from oslo_log import log as logging import paste.urlmap import six -if six.PY3: - from urllib import request as urllib2 -else: +if six.PY2: import urllib2 +else: + from urllib import request as urllib2 from nova.api.openstack import wsgi diff --git a/nova/cells/utils.py b/nova/cells/utils.py index 13f5055341..1c33d31a67 100644 --- a/nova/cells/utils.py +++ b/nova/cells/utils.py @@ -105,10 +105,10 @@ class _CellProxy(object): else: yield name, getattr(self._obj, name) - if six.PY3: - items = _iteritems - else: + if six.PY2: iteritems = _iteritems + else: + items = _iteritems def __getattr__(self, key): return getattr(self._obj, key) diff --git a/nova/openstack/common/cliutils.py b/nova/openstack/common/cliutils.py index ca617f6767..3d06069144 100644 --- a/nova/openstack/common/cliutils.py +++ b/nova/openstack/common/cliutils.py @@ -180,10 +180,10 @@ def print_list(objs, fields, formatters=None, sortby_index=0, row.append(data) pt.add_row(row) - if six.PY3: - print(encodeutils.safe_encode(pt.get_string(**kwargs)).decode()) - else: + if six.PY2: print(encodeutils.safe_encode(pt.get_string(**kwargs))) + else: + print(encodeutils.safe_encode(pt.get_string(**kwargs)).decode()) def print_dict(dct, dict_property="Property", wrap=0, dict_value='Value'): @@ -213,10 +213,10 @@ def print_dict(dct, dict_property="Property", wrap=0, dict_value='Value'): else: pt.add_row([k, v]) - if six.PY3: - print(encodeutils.safe_encode(pt.get_string()).decode()) - else: + if six.PY2: print(encodeutils.safe_encode(pt.get_string())) + else: + print(encodeutils.safe_encode(pt.get_string()).decode()) def get_password(max_password_prompts=3): diff --git a/nova/test.py b/nova/test.py index cffaed044e..ca0adec707 100644 --- a/nova/test.py +++ b/nova/test.py @@ -63,13 +63,13 @@ logging.setup(CONF, 'nova') _TRUE_VALUES = ('True', 'true', '1', 'yes') -if six.PY3: +if six.PY2: + nested = contextlib.nested +else: @contextlib.contextmanager def nested(*contexts): with contextlib.ExitStack() as stack: yield [stack.enter_context(c) for c in contexts] -else: - nested = contextlib.nested class SampleNetworks(fixtures.Fixture): diff --git a/nova/tests/unit/test_exception.py b/nova/tests/unit/test_exception.py index fab2c5a303..6a3b2b70a0 100644 --- a/nova/tests/unit/test_exception.py +++ b/nova/tests/unit/test_exception.py @@ -124,12 +124,12 @@ class NovaExceptionTestCase(test.NoDBTestCase): class FakeNovaException_Remote(exception.NovaException): msg_fmt = "some message" - if six.PY3: - def __str__(self): - return "print the whole trace" - else: + if six.PY2: def __unicode__(self): return u"print the whole trace" + else: + def __str__(self): + return "print the whole trace" exc = FakeNovaException_Remote() self.assertEqual(u"print the whole trace", six.text_type(exc)) diff --git a/nova/utils.py b/nova/utils.py index 0c013af5d7..0cc9fb201d 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -722,12 +722,12 @@ def monkey_patch(): # If CONF.monkey_patch is not True, this function do nothing. if not CONF.monkey_patch: return - if six.PY3: + if six.PY2: + is_method = inspect.ismethod + else: def is_method(obj): # Unbound methods became regular functions on Python 3 return inspect.ismethod(obj) or inspect.isfunction(obj) - else: - is_method = inspect.ismethod # Get list of modules and decorators for module_and_decorator in CONF.monkey_patch_modules: module, decorator_name = module_and_decorator.split(':') diff --git a/nova/virt/libvirt/host.py b/nova/virt/libvirt/host.py index e2b0a05e74..30c1a117ed 100644 --- a/nova/virt/libvirt/host.py +++ b/nova/virt/libvirt/host.py @@ -63,7 +63,7 @@ LOG = logging.getLogger(__name__) native_socket = patcher.original('socket') native_threading = patcher.original("threading") -native_Queue = patcher.original("queue" if six.PY3 else "Queue") +native_Queue = patcher.original("Queue" if six.PY2 else "queue") CONF = cfg.CONF CONF.import_opt('host', 'nova.netconf')