diff options
Diffstat (limited to 'tests/run/extstarargs.pyx')
-rw-r--r-- | tests/run/extstarargs.pyx | 239 |
1 files changed, 142 insertions, 97 deletions
diff --git a/tests/run/extstarargs.pyx b/tests/run/extstarargs.pyx index 041603865..b1dcd4957 100644 --- a/tests/run/extstarargs.pyx +++ b/tests/run/extstarargs.pyx @@ -1,124 +1,169 @@ -__doc__ = u""" - >>> s = Silly(1,2,3, 'test') - >>> (spam,grail,swallow,creosote,onlyt,onlyk,tk) = ( - ... s.spam,s.grail,s.swallow,s.creosote,s.onlyt,s.onlyk,s.tk) - - >>> spam(1,2,3) - (1, 2, 3) - >>> spam(1,2) - Traceback (most recent call last): - TypeError: spam() takes exactly 3 positional arguments (2 given) - >>> spam(1,2,3,4) - Traceback (most recent call last): - TypeError: spam() takes exactly 3 positional arguments (4 given) - >>> spam(1,2,3, a=1) #doctest: +ELLIPSIS - Traceback (most recent call last): - TypeError: spam() got an unexpected keyword argument 'a' - - >>> grail(1,2,3) - (1, 2, 3, ()) - >>> grail(1,2,3,4) - (1, 2, 3, (4,)) - >>> grail(1,2,3,4,5,6,7,8,9) - (1, 2, 3, (4, 5, 6, 7, 8, 9)) - >>> grail(1,2) - Traceback (most recent call last): - TypeError: grail() takes at least 3 positional arguments (2 given) - >>> grail(1,2,3, a=1) #doctest: +ELLIPSIS - Traceback (most recent call last): - TypeError: grail() got an unexpected keyword argument 'a' - - >>> swallow(1,2,3) - (1, 2, 3, ()) - >>> swallow(1,2,3,4) - Traceback (most recent call last): - TypeError: swallow() takes exactly 3 positional arguments (4 given) - >>> swallow(1,2,3, a=1, b=2) - (1, 2, 3, (('a', 1), ('b', 2))) - >>> swallow(1,2,3, x=1) - Traceback (most recent call last): - TypeError: swallow() got multiple values for keyword argument 'x' - - >>> creosote(1,2,3) - (1, 2, 3, (), ()) - >>> creosote(1,2,3,4) - (1, 2, 3, (4,), ()) - >>> creosote(1,2,3, a=1) - (1, 2, 3, (), (('a', 1),)) - >>> creosote(1,2,3,4, a=1, b=2) - (1, 2, 3, (4,), (('a', 1), ('b', 2))) - >>> creosote(1,2,3,4, x=1) - Traceback (most recent call last): - TypeError: creosote() got multiple values for keyword argument 'x' - - >>> onlyt(1) - (1,) - >>> onlyt(1,2) - (1, 2) - >>> onlyt(a=1) - Traceback (most recent call last): - TypeError: onlyt() got an unexpected keyword argument 'a' - >>> onlyt(1, a=2) - Traceback (most recent call last): - TypeError: onlyt() got an unexpected keyword argument 'a' - - >>> onlyk(a=1) - (('a', 1),) - >>> onlyk(a=1, b=2) - (('a', 1), ('b', 2)) - >>> onlyk(1) - Traceback (most recent call last): - TypeError: onlyk() takes exactly 0 positional arguments (1 given) - >>> onlyk(1, 2) - Traceback (most recent call last): - TypeError: onlyk() takes exactly 0 positional arguments (2 given) - >>> onlyk(1, a=1, b=2) - Traceback (most recent call last): - TypeError: onlyk() takes exactly 0 positional arguments (1 given) - - >>> tk(a=1) - (('a', 1),) - >>> tk(a=1, b=2) - (('a', 1), ('b', 2)) - >>> tk(1) - (1,) - >>> tk(1, 2) - (1, 2) - >>> tk(1, a=1, b=2) - (1, ('a', 1), ('b', 2)) -""" - -import sys, re -if sys.version_info >= (2,6): - __doc__ = re.sub(u"(ELLIPSIS[^>]*Error: )[^\n]*\n", u"\\1...\n", __doc__) +cimport cython cdef sorteditems(d): - l = list(d.items()) - l.sort() - return tuple(l) + return tuple(sorted(d.items())) + cdef class Silly: def __init__(self, *a): - pass + """ + >>> s = Silly(1,2,3, 'test') + """ def spam(self, x, y, z): + """ + >>> s = Silly() + >>> s.spam(1,2,3) + (1, 2, 3) + >>> s.spam(1,2) + Traceback (most recent call last): + TypeError: spam() takes exactly 3 positional arguments (2 given) + >>> s.spam(1,2,3,4) + Traceback (most recent call last): + TypeError: spam() takes exactly 3 positional arguments (4 given) + >>> s.spam(1,2,3, a=1) + Traceback (most recent call last): + TypeError: spam() got an unexpected keyword argument 'a' + """ return (x, y, z) def grail(self, x, y, z, *a): + """ + >>> s = Silly() + >>> s.grail(1,2,3) + (1, 2, 3, ()) + >>> s.grail(1,2,3,4) + (1, 2, 3, (4,)) + >>> s.grail(1,2,3,4,5,6,7,8,9) + (1, 2, 3, (4, 5, 6, 7, 8, 9)) + >>> s.grail(1,2) + Traceback (most recent call last): + TypeError: grail() takes at least 3 positional arguments (2 given) + >>> s.grail(1,2,3, a=1) + Traceback (most recent call last): + TypeError: grail() got an unexpected keyword argument 'a' + """ return (x, y, z, a) def swallow(self, x, y, z, **k): + """ + >>> s = Silly() + >>> s.swallow(1,2,3) + (1, 2, 3, ()) + >>> s.swallow(1,2,3,4) + Traceback (most recent call last): + TypeError: swallow() takes exactly 3 positional arguments (4 given) + >>> s.swallow(1,2,3, a=1, b=2) + (1, 2, 3, (('a', 1), ('b', 2))) + >>> s.swallow(1,2,3, x=1) + Traceback (most recent call last): + TypeError: swallow() got multiple values for keyword argument 'x' + """ return (x, y, z, sorteditems(k)) def creosote(self, x, y, z, *a, **k): + """ + >>> s = Silly() + >>> s.creosote(1,2,3) + (1, 2, 3, (), ()) + >>> s.creosote(1,2,3,4) + (1, 2, 3, (4,), ()) + >>> s.creosote(1,2,3, a=1) + (1, 2, 3, (), (('a', 1),)) + >>> s.creosote(1,2,3,4, a=1, b=2) + (1, 2, 3, (4,), (('a', 1), ('b', 2))) + >>> s.creosote(1,2,3,4, x=1) + Traceback (most recent call last): + TypeError: creosote() got multiple values for keyword argument 'x' + """ return (x, y, z, a, sorteditems(k)) def onlyt(self, *a): + """ + >>> s = Silly() + >>> s.onlyt(1) + (1,) + >>> s.onlyt(1,2) + (1, 2) + >>> s.onlyt(a=1) + Traceback (most recent call last): + TypeError: onlyt() got an unexpected keyword argument 'a' + >>> s.onlyt(1, a=2) + Traceback (most recent call last): + TypeError: onlyt() got an unexpected keyword argument 'a' + """ + return a + + @cython.binding(False) # passthrough of exact same tuple can't work with binding + def onlyt_nobinding(self, *a): + """ + >>> s = Silly() + >>> s.onlyt_nobinding(1) + (1,) + >>> s.onlyt_nobinding(1,2) + (1, 2) + >>> s.onlyt_nobinding(a=1) + Traceback (most recent call last): + TypeError: onlyt_nobinding() got an unexpected keyword argument 'a' + >>> s.onlyt_nobinding(1, a=2) + Traceback (most recent call last): + TypeError: onlyt_nobinding() got an unexpected keyword argument 'a' + >>> test_no_copy_args(s.onlyt_nobinding) + True + """ return a def onlyk(self, **k): + """ + >>> s = Silly() + >>> s.onlyk(a=1) + (('a', 1),) + >>> s.onlyk(a=1, b=2) + (('a', 1), ('b', 2)) + >>> s.onlyk(1) + Traceback (most recent call last): + TypeError: onlyk() takes exactly 0 positional arguments (1 given) + >>> s.onlyk(1, 2) + Traceback (most recent call last): + TypeError: onlyk() takes exactly 0 positional arguments (2 given) + >>> s.onlyk(1, a=1, b=2) + Traceback (most recent call last): + TypeError: onlyk() takes exactly 0 positional arguments (1 given) + """ return sorteditems(k) def tk(self, *a, **k): + """ + >>> s = Silly() + >>> s.tk(a=1) + (('a', 1),) + >>> s.tk(a=1, b=2) + (('a', 1), ('b', 2)) + >>> s.tk(1) + (1,) + >>> s.tk(1, 2) + (1, 2) + >>> s.tk(1, a=1, b=2) + (1, ('a', 1), ('b', 2)) + """ return a + sorteditems(k) + + @cython.binding(False) # passthrough of exact same tuple can't work with binding + def t_kwonly(self, *a, k): + """ + >>> s = Silly() + >>> test_no_copy_args(s.t_kwonly, k=None) + True + """ + return a + + +def test_no_copy_args(func, **kw): + """ + func is a function such that func(*args, **kw) returns args. + We test that no copy is made of the args tuple. + This tests both the caller side and the callee side. + """ + args = (1, 2, 3) + return func(*args, **kw) is args |