From d9099ee332b7dace9f0b73c8131ccd960cff5eaa Mon Sep 17 00:00:00 2001 From: Andrew Bogott Date: Thu, 3 May 2012 02:38:48 -0500 Subject: Find context arg by type rather than by name. This requires changes to all the tests that were passing in a string as a bogus context; that seems like an improvement. Change-Id: Ib8fc83cbda25a1cfa8794b72dc13f666549ef892 --- nova/exception.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'nova/exception.py') diff --git a/nova/exception.py b/nova/exception.py index df2f79e3d..87eaca243 100644 --- a/nova/exception.py +++ b/nova/exception.py @@ -25,7 +25,7 @@ SHOULD include dedicated exception logging. """ import functools -import inspect +import itertools import sys import webob.exc @@ -1073,19 +1073,17 @@ class CouldNotFetchImage(NovaException): def get_context_from_function_and_args(function, args, kwargs): - """Find an arg named 'context' and return the value. + """Find an arg of type RequestContext and return it. This is useful in a couple of decorators where we don't know much about the function we're wrapping. """ - context = None - if 'context' in kwargs.keys(): - context = kwargs['context'] - else: - (iargs, _iva, _ikw, _idf) = inspect.getargspec(function) - if 'context' in iargs: - index = iargs.index('context') - if len(args) > index: - context = args[index] - - return context + + # import here to avoid circularity: + from nova import context + + for arg in itertools.chain(kwargs.values(), args): + if isinstance(arg, context.RequestContext): + return arg + + return None -- cgit