From be015dbb42d63ab2897a2e70cbe94e7900912326 Mon Sep 17 00:00:00 2001 From: Eric Brown Date: Wed, 13 Jan 2016 12:36:01 -0800 Subject: [PATCH] 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 --- nova/test.py | 6 +++--- nova/utils.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/nova/test.py b/nova/test.py index cffaed044..ca0adec70 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/utils.py b/nova/utils.py index 0c013af5d..0cc9fb201 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(':')