From 19aaaf5ee166ae0276a4bc00d8bfba1fb3c7fc57 Mon Sep 17 00:00:00 2001 From: Roman Podolyaka Date: Sat, 18 May 2013 23:06:55 +0300 Subject: Fix require_context() decorators. require_context() and require_admin_context() decorators don't copy attributes of a decorated function (func_name, __doc__, __module__) to a wrapper function. This makes introspection and debugging harder and should be fixed. Fixes bug 1181606. Change-Id: I0926df92a40d3fc09b94a0c4b1ded63039abf18b --- nova/db/sqlalchemy/api.py | 2 ++ nova/tests/test_db_api.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index a35e8154d..53131331d 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -95,6 +95,7 @@ def require_admin_context(f): """ + @functools.wraps(f) def wrapper(*args, **kwargs): nova.context.require_admin_context(args[0]) return f(*args, **kwargs) @@ -112,6 +113,7 @@ def require_context(f): """ + @functools.wraps(f) def wrapper(*args, **kwargs): nova.context.require_context(args[0]) return f(*args, **kwargs) diff --git a/nova/tests/test_db_api.py b/nova/tests/test_db_api.py index 4afefdfc8..efe63c738 100644 --- a/nova/tests/test_db_api.py +++ b/nova/tests/test_db_api.py @@ -1176,6 +1176,22 @@ class DbApiTestCase(DbTestCase): _compare(bw_usages[2], expected_bw_usages[2]) timeutils.clear_time_override() + def _test_decorator_wraps_helper(self, decorator): + def test_func(): + """Test docstring.""" + + decorated_func = decorator(test_func) + + self.assertEquals(test_func.func_name, decorated_func.func_name) + self.assertEquals(test_func.__doc__, decorated_func.__doc__) + self.assertEquals(test_func.__module__, decorated_func.__module__) + + def test_require_context_decorator_wraps_functions_properly(self): + self._test_decorator_wraps_helper(sqlalchemy_api.require_context) + + def test_require_admin_context_decorator_wraps_functions_properly(self): + self._test_decorator_wraps_helper(sqlalchemy_api.require_admin_context) + def _get_fake_aggr_values(): return {'name': 'fake_aggregate'} -- cgit