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:
Eric Brown 2016-01-13 12:36:01 -08:00
parent deb1ee4409
commit dd19bc143b
7 changed files with 23 additions and 23 deletions

View File

@ -19,10 +19,10 @@ from oslo_log import log as logging
import paste.urlmap import paste.urlmap
import six import six
if six.PY3: if six.PY2:
from urllib import request as urllib2
else:
import urllib2 import urllib2
else:
from urllib import request as urllib2
from nova.api.openstack import wsgi from nova.api.openstack import wsgi

View File

@ -105,10 +105,10 @@ class _CellProxy(object):
else: else:
yield name, getattr(self._obj, name) yield name, getattr(self._obj, name)
if six.PY3: if six.PY2:
items = _iteritems
else:
iteritems = _iteritems iteritems = _iteritems
else:
items = _iteritems
def __getattr__(self, key): def __getattr__(self, key):
return getattr(self._obj, key) return getattr(self._obj, key)

View File

@ -180,10 +180,10 @@ def print_list(objs, fields, formatters=None, sortby_index=0,
row.append(data) row.append(data)
pt.add_row(row) pt.add_row(row)
if six.PY3: if six.PY2:
print(encodeutils.safe_encode(pt.get_string(**kwargs)).decode())
else:
print(encodeutils.safe_encode(pt.get_string(**kwargs))) 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'): 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: else:
pt.add_row([k, v]) pt.add_row([k, v])
if six.PY3: if six.PY2:
print(encodeutils.safe_encode(pt.get_string()).decode())
else:
print(encodeutils.safe_encode(pt.get_string())) print(encodeutils.safe_encode(pt.get_string()))
else:
print(encodeutils.safe_encode(pt.get_string()).decode())
def get_password(max_password_prompts=3): def get_password(max_password_prompts=3):

View File

@ -63,13 +63,13 @@ logging.setup(CONF, 'nova')
_TRUE_VALUES = ('True', 'true', '1', 'yes') _TRUE_VALUES = ('True', 'true', '1', 'yes')
if six.PY3: if six.PY2:
nested = contextlib.nested
else:
@contextlib.contextmanager @contextlib.contextmanager
def nested(*contexts): def nested(*contexts):
with contextlib.ExitStack() as stack: with contextlib.ExitStack() as stack:
yield [stack.enter_context(c) for c in contexts] yield [stack.enter_context(c) for c in contexts]
else:
nested = contextlib.nested
class SampleNetworks(fixtures.Fixture): class SampleNetworks(fixtures.Fixture):

View File

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

View File

@ -722,12 +722,12 @@ def monkey_patch():
# If CONF.monkey_patch is not True, this function do nothing. # If CONF.monkey_patch is not True, this function do nothing.
if not CONF.monkey_patch: if not CONF.monkey_patch:
return return
if six.PY3: if six.PY2:
is_method = inspect.ismethod
else:
def is_method(obj): def is_method(obj):
# Unbound methods became regular functions on Python 3 # Unbound methods became regular functions on Python 3
return inspect.ismethod(obj) or inspect.isfunction(obj) return inspect.ismethod(obj) or inspect.isfunction(obj)
else:
is_method = inspect.ismethod
# Get list of modules and decorators # Get list of modules and decorators
for module_and_decorator in CONF.monkey_patch_modules: for module_and_decorator in CONF.monkey_patch_modules:
module, decorator_name = module_and_decorator.split(':') module, decorator_name = module_and_decorator.split(':')

View File

@ -63,7 +63,7 @@ LOG = logging.getLogger(__name__)
native_socket = patcher.original('socket') native_socket = patcher.original('socket')
native_threading = patcher.original("threading") 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 = cfg.CONF
CONF.import_opt('host', 'nova.netconf') CONF.import_opt('host', 'nova.netconf')