diff --git a/nova/safe_utils.py b/nova/safe_utils.py index 64885a32aa6c..0ee71620a4a0 100644 --- a/nova/safe_utils.py +++ b/nova/safe_utils.py @@ -36,4 +36,6 @@ def get_wrapped_function(function): elif hasattr(closure.cell_contents, '__call__'): return closure.cell_contents + return function + return _get_wrapped_function(function) diff --git a/nova/tests/unit/test_safeutils.py b/nova/tests/unit/test_safeutils.py index 1215c30b4386..cfac25560ffc 100644 --- a/nova/tests/unit/test_safeutils.py +++ b/nova/tests/unit/test_safeutils.py @@ -18,6 +18,15 @@ from nova import safe_utils from nova import test +def get_closure(): + x = 1 + + def wrapper(self, instance, red=None, blue=None): + return x + + return wrapper + + class WrappedCodeTestCase(test.NoDBTestCase): """Test the get_wrapped_function utility method.""" @@ -68,3 +77,13 @@ class WrappedCodeTestCase(test.NoDBTestCase): self.assertIn('instance', func_code.co_varnames) self.assertIn('red', func_code.co_varnames) self.assertIn('blue', func_code.co_varnames) + + def test_closure(self): + closure = get_closure() + func = safe_utils.get_wrapped_function(closure) + func_code = func.__code__ + self.assertEqual(4, len(func_code.co_varnames)) + self.assertIn('self', func_code.co_varnames) + self.assertIn('instance', func_code.co_varnames) + self.assertIn('red', func_code.co_varnames) + self.assertIn('blue', func_code.co_varnames)