summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2010-03-30 18:42:32 (GMT)
committerBenjamin Peterson <benjamin@python.org>2010-03-30 18:42:32 (GMT)
commit25cd7eb9a14cb646e58e315ebfafe867862a705b (patch)
tree79a7b79ca21a2f2e8e3012163745f9d513edbeaf /Lib
parente6c9d24562adf4eddfcbeb6cc1873b3e6795e2eb (diff)
downloadcpython-25cd7eb9a14cb646e58e315ebfafe867862a705b.zip
cpython-25cd7eb9a14cb646e58e315ebfafe867862a705b.tar.gz
cpython-25cd7eb9a14cb646e58e315ebfafe867862a705b.tar.bz2
Merged revisions 79500 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r79500 | benjamin.peterson | 2010-03-30 12:58:13 -0500 (Tue, 30 Mar 2010) | 4 lines add inspect.getcallargs, which binds function arguments like a normal call #3135 Patch by George Sakkis ........
Diffstat (limited to 'Lib')
-rw-r--r--Lib/inspect.py68
-rw-r--r--Lib/test/test_inspect.py180
2 files changed, 244 insertions, 4 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index c489502..b9fcd74 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
getfullargspec() - same, with support for Python-3000 features
formatargspec(), formatargvalues() - format an argument spec
getouterframes(), getinnerframes() - get info about frames
@@ -33,6 +33,7 @@ __date__ = '1 Jan 2001'
import sys
import os
import types
+import itertools
import string
import re
import dis
@@ -926,6 +927,71 @@ def formatargvalues(args, varargs, varkw, locals,
specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
return '(' + ', '.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'."""
+ spec = getfullargspec(func)
+ args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
+ f_name = func.__name__
+ arg2value = {}
+
+ if ismethod(func) and func.__self__ is not None:
+ # implicit 'self' (or 'cls' for classmethods) argument
+ positional = (func.__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):
+ arg2value[arg] = value
+ if varargs:
+ if num_pos > num_args:
+ arg2value[varargs] = positional[-(num_pos-num_args):]
+ else:
+ arg2value[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 itertools.chain(args, kwonlyargs):
+ if arg in named:
+ if arg in arg2value:
+ raise TypeError("%s() got multiple values for keyword "
+ "argument '%s'" % (f_name, arg))
+ else:
+ arg2value[arg] = named.pop(arg)
+ for kwonlyarg in kwonlyargs:
+ if kwonlyarg not in arg2value:
+ try:
+ arg2value[kwonlyarg] = kwonlydefaults[kwonlyarg]
+ except KeyError:
+ raise TypeError("%s() needs keyword-only argument %s" %
+ (f_name, kwonlyarg))
+ if defaults: # fill in any missing values with the defaults
+ for arg, value in zip(args[-num_defaults:], defaults):
+ if arg not in arg2value:
+ arg2value[arg] = value
+ if varkw:
+ arg2value[varkw] = named
+ elif named:
+ unexpected = next(iter(named))
+ raise TypeError("%s() got an unexpected keyword argument '%s'" %
+ (f_name, unexpected))
+ unassigned = num_args - len([arg for arg in args if arg in arg2value])
+ 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')
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 35fd775..b89f8075 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -1,3 +1,4 @@
+import re
import sys
import types
import unittest
@@ -519,10 +520,183 @@ class TestClassesAndFunctions(unittest.TestCase):
self.assertIn(('m1', 'method', D), attrs, 'missing plain method')
self.assertIn(('datablob', 'data', A), attrs, 'missing data')
+class TestGetcallargsFunctions(unittest.TestCase):
+
+ def assertEqualCallArgs(self, func, call_params_string, locs=None):
+ locs = dict(locs or {}, func=func)
+ r1 = eval('func(%s)' % call_params_string, None, locs)
+ r2 = eval('inspect.getcallargs(func, %s)' % call_params_string, None,
+ locs)
+ self.assertEqual(r1, r2)
+
+ def assertEqualException(self, func, call_param_string, locs=None):
+ locs = dict(locs or {}, func=func)
+ try:
+ eval('func(%s)' % call_param_string, None, locs)
+ except Exception as e:
+ ex1 = e
+ else:
+ self.fail('Exception not raised')
+ try:
+ eval('inspect.getcallargs(func, %s)' % call_param_string, None,
+ locs)
+ except Exception as e:
+ ex2 = e
+ else:
+ self.fail('Exception not raised')
+ self.assertIs(type(ex1), type(ex2))
+ self.assertEqual(str(ex1), str(ex2))
+ del ex1, ex2
+
+ def makeCallable(self, signature):
+ """Create a function that returns its locals()"""
+ code = "lambda %s: locals()"
+ return eval(code % signature)
+
+ def test_plain(self):
+ f = self.makeCallable('a, b=1')
+ self.assertEqualCallArgs(f, '2')
+ self.assertEqualCallArgs(f, '2, 3')
+ self.assertEqualCallArgs(f, 'a=2')
+ self.assertEqualCallArgs(f, 'b=3, a=2')
+ self.assertEqualCallArgs(f, '2, b=3')
+ # expand *iterable / **mapping
+ self.assertEqualCallArgs(f, '*(2,)')
+ self.assertEqualCallArgs(f, '*[2]')
+ self.assertEqualCallArgs(f, '*(2, 3)')
+ self.assertEqualCallArgs(f, '*[2, 3]')
+ self.assertEqualCallArgs(f, '**{"a":2}')
+ self.assertEqualCallArgs(f, 'b=3, **{"a":2}')
+ self.assertEqualCallArgs(f, '2, **{"b":3}')
+ self.assertEqualCallArgs(f, '**{"b":3, "a":2}')
+ # expand UserList / UserDict
+ self.assertEqualCallArgs(f, '*collections.UserList([2])')
+ self.assertEqualCallArgs(f, '*collections.UserList([2, 3])')
+ self.assertEqualCallArgs(f, '**collections.UserDict(a=2)')
+ self.assertEqualCallArgs(f, '2, **collections.UserDict(b=3)')
+ self.assertEqualCallArgs(f, 'b=2, **collections.UserDict(a=3)')
+
+ def test_varargs(self):
+ f = self.makeCallable('a, b=1, *c')
+ self.assertEqualCallArgs(f, '2')
+ self.assertEqualCallArgs(f, '2, 3')
+ self.assertEqualCallArgs(f, '2, 3, 4')
+ self.assertEqualCallArgs(f, '*(2,3,4)')
+ self.assertEqualCallArgs(f, '2, *[3,4]')
+ self.assertEqualCallArgs(f, '2, 3, *collections.UserList([4])')
+
+ def test_varkw(self):
+ f = self.makeCallable('a, b=1, **c')
+ self.assertEqualCallArgs(f, 'a=2')
+ self.assertEqualCallArgs(f, '2, b=3, c=4')
+ self.assertEqualCallArgs(f, 'b=3, a=2, c=4')
+ self.assertEqualCallArgs(f, 'c=4, **{"a":2, "b":3}')
+ self.assertEqualCallArgs(f, '2, c=4, **{"b":3}')
+ self.assertEqualCallArgs(f, 'b=2, **{"a":3, "c":4}')
+ self.assertEqualCallArgs(f, '**collections.UserDict(a=2, b=3, c=4)')
+ self.assertEqualCallArgs(f, '2, c=4, **collections.UserDict(b=3)')
+ self.assertEqualCallArgs(f, 'b=2, **collections.UserDict(a=3, c=4)')
+
+ def test_keyword_only(self):
+ f = self.makeCallable('a=3, *, c, d=2')
+ self.assertEqualCallArgs(f, 'c=3')
+ self.assertEqualCallArgs(f, 'c=3, a=3')
+ self.assertEqualCallArgs(f, 'a=2, c=4')
+ self.assertEqualCallArgs(f, '4, c=4')
+ self.assertEqualException(f, '')
+ self.assertEqualException(f, '3')
+ self.assertEqualException(f, 'a=3')
+ self.assertEqualException(f, 'd=4')
+
+ def test_multiple_features(self):
+ f = self.makeCallable('a, b=2, *f, **g')
+ self.assertEqualCallArgs(f, '2, 3, 7')
+ self.assertEqualCallArgs(f, '2, 3, x=8')
+ self.assertEqualCallArgs(f, '2, 3, x=8, *[(4,[5,6]), 7]')
+ self.assertEqualCallArgs(f, '2, x=8, *[3, (4,[5,6]), 7], y=9')
+ self.assertEqualCallArgs(f, 'x=8, *[2, 3, (4,[5,6])], y=9')
+ self.assertEqualCallArgs(f, 'x=8, *collections.UserList('
+ '[2, 3, (4,[5,6])]), **{"y":9, "z":10}')
+ self.assertEqualCallArgs(f, '2, x=8, *collections.UserList([3, '
+ '(4,[5,6])]), **collections.UserDict('
+ 'y=9, z=10)')
+
+ def test_errors(self):
+ f0 = self.makeCallable('')
+ f1 = self.makeCallable('a, b')
+ f2 = self.makeCallable('a, b=1')
+ # f0 takes no arguments
+ self.assertEqualException(f0, '1')
+ self.assertEqualException(f0, 'x=1')
+ self.assertEqualException(f0, '1,x=1')
+ # f1 takes exactly 2 arguments
+ self.assertEqualException(f1, '')
+ self.assertEqualException(f1, '1')
+ self.assertEqualException(f1, 'a=2')
+ self.assertEqualException(f1, 'b=3')
+ # f2 takes at least 1 argument
+ self.assertEqualException(f2, '')
+ self.assertEqualException(f2, 'b=3')
+ for f in f1, f2:
+ # f1/f2 takes exactly/at most 2 arguments
+ self.assertEqualException(f, '2, 3, 4')
+ self.assertEqualException(f, '1, 2, 3, a=1')
+ self.assertEqualException(f, '2, 3, 4, c=5')
+ self.assertEqualException(f, '2, 3, 4, a=1, c=5')
+ # f got an unexpected keyword argument
+ self.assertEqualException(f, 'c=2')
+ self.assertEqualException(f, '2, c=3')
+ self.assertEqualException(f, '2, 3, c=4')
+ self.assertEqualException(f, '2, c=4, b=3')
+ self.assertEqualException(f, '**{u"\u03c0\u03b9": 4}')
+ # f got multiple values for keyword argument
+ self.assertEqualException(f, '1, a=2')
+ self.assertEqualException(f, '1, **{"a":2}')
+ self.assertEqualException(f, '1, 2, b=3')
+ # XXX: Python inconsistency
+ # - for functions and bound methods: unexpected keyword 'c'
+ # - for unbound methods: multiple values for keyword 'a'
+ #self.assertEqualException(f, '1, c=3, a=2')
+
+class TestGetcallargsMethods(TestGetcallargsFunctions):
+
+ def setUp(self):
+ class Foo(object):
+ pass
+ self.cls = Foo
+ self.inst = Foo()
+
+ def makeCallable(self, signature):
+ assert 'self' not in signature
+ mk = super(TestGetcallargsMethods, self).makeCallable
+ self.cls.method = mk('self, ' + signature)
+ return self.inst.method
+
+class TestGetcallargsUnboundMethods(TestGetcallargsMethods):
+
+ def makeCallable(self, signature):
+ super(TestGetcallargsUnboundMethods, self).makeCallable(signature)
+ return self.cls.method
+
+ def assertEqualCallArgs(self, func, call_params_string, locs=None):
+ return super(TestGetcallargsUnboundMethods, self).assertEqualCallArgs(
+ *self._getAssertEqualParams(func, call_params_string, locs))
+
+ def assertEqualException(self, func, call_params_string, locs=None):
+ return super(TestGetcallargsUnboundMethods, self).assertEqualException(
+ *self._getAssertEqualParams(func, call_params_string, locs))
+
+ def _getAssertEqualParams(self, func, call_params_string, locs=None):
+ assert 'inst' not in call_params_string
+ locs = dict(locs or {}, inst=self.inst)
+ return (func, 'inst,' + call_params_string, locs)
+
def test_main():
- run_unittest(TestDecorators, TestRetrievingSourceCode, TestOneliners,
- TestBuggyCases,
- TestInterpreterStack, TestClassesAndFunctions, TestPredicates)
+ run_unittest(
+ TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBuggyCases,
+ TestInterpreterStack, TestClassesAndFunctions, TestPredicates,
+ TestGetcallargsFunctions, TestGetcallargsMethods,
+ TestGetcallargsUnboundMethods)
if __name__ == "__main__":
test_main()