summaryrefslogtreecommitdiff
path: root/Lib/inspect.py
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2010-03-30 17:58:13 +0000
committerBenjamin Peterson <benjamin@python.org>2010-03-30 17:58:13 +0000
commit7e213255cee76f692dded963ec726b39319646c0 (patch)
tree73fb707550d69a479a8007a4bddb11faca6303ea /Lib/inspect.py
parentec71794cb825ff391c02c4ae942f03a31a68c061 (diff)
downloadcpython-git-7e213255cee76f692dded963ec726b39319646c0.tar.gz
add inspect.getcallargs, which binds function arguments like a normal call #3135
Patch by George Sakkis
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r--Lib/inspect.py85
1 files changed, 84 insertions, 1 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index e5098d798e..5344893a43 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -17,7 +17,7 @@ Here are some of the useful functions provided by this module:
getmodule() - determine the module that an object came from
getclasstree() - arrange classes so as to represent their hierarchy
- getargspec(), getargvalues() - get info about function arguments
+ getargspec(), getargvalues(), getcallargs() - get info about function arguments
formatargspec(), formatargvalues() - format an argument spec
getouterframes(), getinnerframes() - get info about frames
currentframe() - get the current stack frame
@@ -884,6 +884,89 @@ def formatargvalues(args, varargs, varkw, locals,
specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
return '(' + string.join(specs, ', ') + ')'
+def getcallargs(func, *positional, **named):
+ """Get the mapping of arguments to values.
+
+ A dict is returned, with keys the function argument names (including the
+ names of the * and ** arguments, if any), and values the respective bound
+ values from 'positional' and 'named'."""
+ args, varargs, varkw, defaults = getargspec(func)
+ f_name = func.__name__
+ arg2value = {}
+
+ # The following closures are basically because of tuple parameter unpacking.
+ assigned_tuple_params = []
+ def assign(arg, value):
+ if isinstance(arg, str):
+ arg2value[arg] = value
+ else:
+ assigned_tuple_params.append(arg)
+ value = iter(value)
+ for i, subarg in enumerate(arg):
+ try:
+ subvalue = next(value)
+ except StopIteration:
+ raise ValueError('need more than %d %s to unpack' %
+ (i, 'values' if i > 1 else 'value'))
+ assign(subarg,subvalue)
+ try:
+ next(value)
+ except StopIteration:
+ pass
+ else:
+ raise ValueError('too many values to unpack')
+ def is_assigned(arg):
+ if isinstance(arg,str):
+ return arg in arg2value
+ return arg in assigned_tuple_params
+ if ismethod(func) and func.im_self is not None:
+ # implicit 'self' (or 'cls' for classmethods) argument
+ positional = (func.im_self,) + positional
+ num_pos = len(positional)
+ num_total = num_pos + len(named)
+ num_args = len(args)
+ num_defaults = len(defaults) if defaults else 0
+ for arg, value in zip(args, positional):
+ assign(arg, value)
+ if varargs:
+ if num_pos > num_args:
+ assign(varargs, positional[-(num_pos-num_args):])
+ else:
+ assign(varargs, ())
+ elif 0 < num_args < num_pos:
+ raise TypeError('%s() takes %s %d %s (%d given)' % (
+ f_name, 'at most' if defaults else 'exactly', num_args,
+ 'arguments' if num_args > 1 else 'argument', num_total))
+ elif num_args == 0 and num_total:
+ raise TypeError('%s() takes no arguments (%d given)' %
+ (f_name, num_total))
+ for arg in args:
+ if isinstance(arg, str) and arg in named:
+ if is_assigned(arg):
+ raise TypeError("%s() got multiple values for keyword "
+ "argument '%s'" % (f_name, arg))
+ else:
+ assign(arg, named.pop(arg))
+ if defaults: # fill in any missing values with the defaults
+ for arg, value in zip(args[-num_defaults:], defaults):
+ if not is_assigned(arg):
+ assign(arg, value)
+ if varkw:
+ assign(varkw, named)
+ elif named:
+ unexpected = next(iter(named))
+ if isinstance(unexpected, unicode):
+ unexpected = unexpected.encode(sys.getdefaultencoding(), 'replace')
+ raise TypeError("%s() got an unexpected keyword argument '%s'" %
+ (f_name, unexpected))
+ unassigned = num_args - len([arg for arg in args if is_assigned(arg)])
+ if unassigned:
+ num_required = num_args - num_defaults
+ raise TypeError('%s() takes %s %d %s (%d given)' % (
+ f_name, 'at least' if defaults else 'exactly', num_required,
+ 'arguments' if num_required > 1 else 'argument', num_total))
+ return arg2value
+
# -------------------------------------------------- stack frame extraction
Traceback = namedtuple('Traceback', 'filename lineno function code_context index')