From ee14adec87331305cdc43ff8f0c1013fcf488862 Mon Sep 17 00:00:00 2001
From: Victor Stinner <vstinner@redhat.com>
Date: Thu, 5 Nov 2015 17:30:30 +0100
Subject: [PATCH] Port cinder.utils.monkey_patch() to Python 3

On Python 3, unbound methods are regular functions: use pass
inspect.isfunction to inspect.getmembers().

tox.ini: add test_utils to Python 3.4.

Partial-Implements: blueprint cinder-python3
Change-Id: I4fe9936792a43aff301010780e86244bdf2d05de
---
 cinder/utils.py | 6 ++++--
 tests-py3.txt   | 1 +
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/cinder/utils.py b/cinder/utils.py
index 58f23134b17..3eef2ac3c0b 100644
--- a/cinder/utils.py
+++ b/cinder/utils.py
@@ -389,12 +389,14 @@ def monkey_patch():
             # set the decorator for the class methods
             if isinstance(module_data[key], pyclbr.Class):
                 clz = importutils.import_class("%s.%s" % (module, key))
-                for method, func in inspect.getmembers(clz, inspect.ismethod):
+                # On Python 3, unbound methods are regular functions
+                predicate = inspect.isfunction if six.PY3 else inspect.ismethod
+                for method, func in inspect.getmembers(clz, predicate):
                     setattr(
                         clz, method,
                         decorator("%s.%s.%s" % (module, key, method), func))
             # set the decorator for the function
-            if isinstance(module_data[key], pyclbr.Function):
+            elif isinstance(module_data[key], pyclbr.Function):
                 func = importutils.import_class("%s.%s" % (module, key))
                 setattr(sys.modules[module], key,
                         decorator("%s.%s" % (module, key), func))
diff --git a/tests-py3.txt b/tests-py3.txt
index 8e07e9e3cb1..30fb906aedf 100644
--- a/tests-py3.txt
+++ b/tests-py3.txt
@@ -103,6 +103,7 @@ cinder.tests.unit.test_ssh_utils
 cinder.tests.unit.test_test
 cinder.tests.unit.test_test_utils
 cinder.tests.unit.test_tintri
+cinder.tests.unit.test_utils
 cinder.tests.unit.test_v6000_common
 cinder.tests.unit.test_v6000_fcp
 cinder.tests.unit.test_v6000_iscsi