summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2011-03-28 17:32:31 -0500
committerBenjamin Peterson <benjamin@python.org>2011-03-28 17:32:31 -0500
commitf3a1e627da95738857771f1419a4dce2c6bf44fc (patch)
treeb035e0249eba7014a56862ef18692b85a42bd5b2 /Lib
parent995487462bebed925b6de287565b377799e167e3 (diff)
downloadcpython-f3a1e627da95738857771f1419a4dce2c6bf44fc.tar.gz
Correct handling of functions with only kwarg args in getcallargs (closes #11256)
A patch from Daniel Urban.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/inspect.py10
-rw-r--r--Lib/test/test_inspect.py14
2 files changed, 22 insertions, 2 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 33065f562b..b0b2d3a8f5 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -943,8 +943,14 @@ def getcallargs(func, *positional, **named):
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))
+ if varkw:
+ if num_pos:
+ # XXX: We should use num_pos, but Python also uses num_total:
+ raise TypeError('%s() takes exactly 0 arguments '
+ '(%d given)' % (f_name, num_total))
+ else:
+ 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):
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index fcdfd94f16..d93ffec58b 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -632,6 +632,16 @@ class TestGetcallargsFunctions(unittest.TestCase):
self.assertEqualCallArgs(f, '2, c=4, **{u"b":3}')
self.assertEqualCallArgs(f, 'b=2, **{u"a":3, u"c":4}')
+ def test_varkw_only(self):
+ # issue11256:
+ f = self.makeCallable('**c')
+ self.assertEqualCallArgs(f, '')
+ self.assertEqualCallArgs(f, 'a=1')
+ self.assertEqualCallArgs(f, 'a=1, b=2')
+ self.assertEqualCallArgs(f, 'c=3, **{"a": 1, "b": 2}')
+ self.assertEqualCallArgs(f, '**UserDict(a=1, b=2)')
+ self.assertEqualCallArgs(f, 'c=3, **UserDict(a=1, b=2)')
+
def test_tupleargs(self):
f = self.makeCallable('(b,c), (d,(e,f))=(0,[1,2])')
self.assertEqualCallArgs(f, '(2,3)')
@@ -693,6 +703,10 @@ class TestGetcallargsFunctions(unittest.TestCase):
self.assertEqualException(f, '1')
self.assertEqualException(f, '[1]')
self.assertEqualException(f, '(1,2,3)')
+ # issue11256:
+ f3 = self.makeCallable('**c')
+ self.assertEqualException(f3, '1, 2')
+ self.assertEqualException(f3, '1, 2, a=1, b=2')
class TestGetcallargsMethods(TestGetcallargsFunctions):