Use of six.PY3 should be forward compatible
Care should be taken in using a condition like so: if six.PY3: foo() else: bar() This assumes PY2 and PY4 would behave the same. Rather the conditional should check for PY2 and use the else for PY3 and future versions of Python. http://astrofrog.github.io/blog/2016/01/12/stop-writing-python-4-incompatible-code/ Change-Id: I9bc00e2f01fe8fe970d6c5327207c08a90885cda
This commit is contained in:
parent
deb1ee4409
commit
dd19bc143b
@ -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
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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):
|
||||
|
@ -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):
|
||||
|
@ -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))
|
||||
|
@ -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(':')
|
||||
|
@ -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')
|
||||
|
Loading…
Reference in New Issue
Block a user