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 adeb0bfe4c
commit be015dbb42
2 changed files with 6 additions and 6 deletions

View File

@ -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):

View File

@ -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(':')