Commit b6b0f9a0 authored by Stefan Behnel's avatar Stefan Behnel

fixed handling of keyword-only arguments

changed more exception messages to what Py3 raises
some cleanup
parent 086f4098
This diff is collapsed.
__doc__ = u"""
>>> test()
>>> test_pos_args(h)
1 2 3 * 0 0
1 2 9 * 2 0
1 2 7 * 2 0
9 8 7 * 0 0
7 8 9 * 0 0
>>> test_kw_args(h)
1 2 3 * 0 0
1 2 9 * 2 1
1 2 7 * 2 1
1 2 9 * 2 2
1 2 9 * 2 2
1 2 9 * 2 3
>>> test_kw_args(e)
2 1
5 1
5 1
5 2
5 2
5 3
>>> test_kw(e)
0 1
0 2
0 2
0 1
>>> test_kw(g)
1
2
2
1
>>> test_pos_args(f)
3
5
5
3
3
>>> test_noargs(e)
0 0
>>> test_noargs(f)
0
>>> test_noargs(g)
0
>>> test_noargs(h)
Traceback (most recent call last):
TypeError: h() takes at least 3 positional arguments (0 given)
"""
def f(a, b, c, *args, **kwargs):
def e(*args, **kwargs):
print len(args), len(kwargs)
def f(*args):
print len(args)
def g(**kwargs):
print len(kwargs)
def h(a, b, c, *args, **kwargs):
print a, b, c, u'*', len(args), len(kwargs)
args = (9,8,7)
......@@ -21,11 +73,26 @@ else:
kwargs = {"test" : u"toast"}
def test():
f(1,2,3)
def test_kw_args(f):
f(1,2, c=3)
f(1,2, d=3, *args)
f(1,2, d=3, *(7,8,9))
f(1,2, d=3, *args, **kwargs)
f(1,2, d=3, *args, e=5)
f(1,2, d=3, *args, e=5, **kwargs)
def test_pos_args(f):
f(1,2,3)
f(1,2, *args)
f(1,2, *(7,8,9))
f(*args)
f(*(7,8,9))
def test_kw(f):
f(c=3)
f(d=3, e=5)
f(d=3, **kwargs)
f(**kwargs)
def test_noargs(f):
f()
......@@ -40,7 +40,7 @@ __doc__ = u"""
TypeError: f() takes at most 3 positional arguments (4 given)
>>> f(1,2)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
TypeError: f() needs keyword-only argument c
>>> f(1,2, c=1, e=2)
Traceback (most recent call last):
TypeError: 'e' is an invalid keyword argument for this function
......@@ -54,10 +54,10 @@ __doc__ = u"""
TypeError: g() takes at most 3 positional arguments (4 given)
>>> g(1,2)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
TypeError: g() needs keyword-only argument c
>>> g(1,2, c=1)
Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing
TypeError: g() needs keyword-only argument f
>>> h(1,2, c=1, f=2)
>>> h(1,2, c=1, f=2, e=3)
......@@ -66,10 +66,10 @@ __doc__ = u"""
>>> h(1,2,3)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
TypeError: h() needs keyword-only argument c
>>> h(1,2, d=1)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
TypeError: h() needs keyword-only argument c
>>> k(1,2, c=1, f=2)
>>> k(1,2, c=1, f=2, e=3)
......@@ -78,10 +78,10 @@ __doc__ = u"""
>>> k(1,2,3)
Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing
TypeError: k() needs keyword-only argument f
>>> k(1,2, d=1)
Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing
TypeError: k() needs keyword-only argument f
"""
import sys, re
......
......@@ -40,7 +40,7 @@ __doc__ = u"""
TypeError: f() takes at most 2 positional arguments (3 given)
>>> f(1,2)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
TypeError: f() needs keyword-only argument c
>>> f(1,2, c=1, e=2)
Traceback (most recent call last):
TypeError: 'e' is an invalid keyword argument for this function
......@@ -54,10 +54,10 @@ __doc__ = u"""
TypeError: g() takes at most 2 positional arguments (3 given)
>>> g(1,2)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
TypeError: g() needs keyword-only argument c
>>> g(1,2, c=1)
Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing
TypeError: g() needs keyword-only argument f
>>> h(1,2, c=1, f=2)
>>> h(1,2, c=1, f=2, e=3)
......@@ -66,10 +66,10 @@ __doc__ = u"""
>>> h(1,2,3)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
TypeError: h() needs keyword-only argument c
>>> h(1,2, d=1)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
TypeError: h() needs keyword-only argument c
>>> k(1,2, c=1, f=2)
>>> k(1,2, c=1, f=2, e=3)
......@@ -78,10 +78,10 @@ __doc__ = u"""
>>> k(1,2,3)
Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing
TypeError: k() needs keyword-only argument f
>>> k(1,2, d=1)
Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing
TypeError: k() needs keyword-only argument f
"""
cdef class Ext:
......
......@@ -37,7 +37,7 @@ __doc__ = u"""
TypeError: f() takes at most 2 positional arguments (3 given)
>>> f(1,2)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
TypeError: f() needs keyword-only argument c
>>> f(1,2, c=1, e=2)
Traceback (most recent call last):
TypeError: 'e' is an invalid keyword argument for this function
......@@ -51,10 +51,10 @@ __doc__ = u"""
TypeError: g() takes at most 2 positional arguments (3 given)
>>> g(1,2)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
TypeError: g() needs keyword-only argument c
>>> g(1,2, c=1)
Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing
TypeError: g() needs keyword-only argument f
>>> h(1,2, c=1, f=2)
>>> h(1,2, c=1, f=2, e=3)
......@@ -63,10 +63,10 @@ __doc__ = u"""
>>> h(1,2,3)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
TypeError: h() needs keyword-only argument c
>>> h(1,2, d=1)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
TypeError: h() needs keyword-only argument c
>>> k(1,2, c=1, f=2)
>>> k(1,2, c=1, f=2, e=3)
......@@ -75,10 +75,10 @@ __doc__ = u"""
>>> k(1,2,3)
Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing
TypeError: k() needs keyword-only argument f
>>> k(1,2, d=1)
Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing
TypeError: k() needs keyword-only argument f
"""
def b(a, b, c):
......
__doc__ = u"""
>>> call3(b)
1 2 3
>>> call4(b)
Traceback (most recent call last):
TypeError: b() takes at most 3 positional arguments (4 given)
>>> call2(c)
1 2 1
>>> call3(c)
1 2 3
>>> call4(c)
Traceback (most recent call last):
TypeError: c() takes at most 3 positional arguments (4 given)
>>> call2(d)
1 2 88
>>> call2c(d)
1 2 1
>>> call3(d)
Traceback (most recent call last):
......@@ -21,64 +26,82 @@ __doc__ = u"""
TypeError: 'd' is an invalid keyword argument for this function
>>> call2(e)
1 2 88 []
>>> call2c(e)
1 2 1 []
>>> call2d(e)
1 2 88 [('d', 1)]
>>> call2cde(e)
1 2 1 [('d', 2), ('e', 3)]
>>> call3(e)
1 2 3 []
>>> call4(e)
Traceback (most recent call last):
TypeError: e() takes at most 3 positional arguments (4 given)
>>> call2c(f)
1 2 1 42
>>> call2cd(f)
1 2 1 2
>>> call3(f)
Traceback (most recent call last):
TypeError: f() takes at most 2 positional arguments (3 given)
>>> call2(f)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
TypeError: f() needs keyword-only argument c
>>> call2ce(f)
Traceback (most recent call last):
TypeError: 'e' is an invalid keyword argument for this function
>>> call2cf(g)
1 2 1 42 17 2 []
>>> call2cefd(g)
1 2 1 11 0 2 []
>>> call2cfex(g)
1 2 1 42 0 2 [('x', 25)]
>>> call3(g)
Traceback (most recent call last):
TypeError: g() takes at most 2 positional arguments (3 given)
>>> call2(g)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
TypeError: g() needs keyword-only argument c
>>> call2c(g)
Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing
TypeError: g() needs keyword-only argument f
>>> call2cf(h)
1 2 1 42 17 2 () []
>>> call2cfe(h)
1 2 1 42 3 2 () []
>>> call6cf(h)
1 2 1 42 17 2 (3, 4, 5, 6) []
>>> call6cfexy(h)
1 2 1 42 3 2 (3, 4, 5, 6) [('x', 25), ('y', 11)]
>>> call3(h)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
TypeError: h() needs keyword-only argument c
>>> call3d(h)
Traceback (most recent call last):
TypeError: required keyword argument 'c' is missing
TypeError: h() needs keyword-only argument c
>>> call2cf(k)
1 2 1 42 17 2 () []
>>> call2cfe(k)
1 2 1 42 3 2 () []
>>> call6df(k)
1 2 3 1 17 2 (4, 5, 6) []
>>> call6dfexy(k)
1 2 3 1 3 2 (4, 5, 6) [('x', 25), ('y', 11)]
>>> call3(k)
Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing
TypeError: k() needs keyword-only argument f
>>> call2d(k)
Traceback (most recent call last):
TypeError: required keyword argument 'f' is missing
TypeError: k() needs keyword-only argument f
"""
import sys, re
......@@ -145,25 +168,33 @@ def call6dfexy(f):
# the called functions:
def b(a, b, c):
pass
print a,b,c
def c(a, b, c=1):
pass
print a,b,c
def d(a, b, *, c = 88):
pass
print a,b,c
def e(a, b, c = 88, **kwds):
pass
kwlist = list(kwds.items())
kwlist.sort()
print a,b,c, kwlist
def f(a, b, *, c, d = 42):
pass
print a,b,c,d
def g(a, b, *, c, d = 42, e = 17, f, **kwds):
pass
kwlist = list(kwds.items())
kwlist.sort()
print a,b,c,d,e,f, kwlist
def h(a, b, *args, c, d = 42, e = 17, f, **kwds):
pass
kwlist = list(kwds.items())
kwlist.sort()
print a,b,c,d,e,f, args, kwlist
def k(a, b, c=1, *args, d = 42, e = 17, f, **kwds):
pass
kwlist = list(kwds.items())
kwlist.sort()
print a,b,c,d,e,f, args, kwlist
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment