extstarargs.pyx 4.25 KB
Newer Older
1
cdef sorteditems(d):
2
    return tuple(sorted(d.items()))
3

4

5 6 7
cdef class Silly:

    def __init__(self, *a):
8 9 10
        """
        >>> s = Silly(1,2,3, 'test')
        """
11 12

    def spam(self, x, y, z):
13 14 15 16 17 18 19 20 21 22 23 24 25 26
        """
        >>> 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'
        """
27 28
        return (x, y, z)

29
    def grail(self, x, y, z, *a):
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
        """
        >>> 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'
        """
45 46
        return (x, y, z, a)

47
    def swallow(self, x, y, z, **k):
48 49 50 51 52 53 54 55 56 57 58 59 60
        """
        >>> 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'
        """
61 62
        return (x, y, z, sorteditems(k))

63
    def creosote(self, x, y, z, *a, **k):
64 65 66 67 68 69 70 71 72 73 74 75 76 77
        """
        >>> 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'
        """
78 79 80
        return (x, y, z, a, sorteditems(k))

    def onlyt(self, *a):
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        """
        >>> 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'
        >>> test_no_copy_args(s.onlyt)
        True
        """
96 97 98
        return a

    def onlyk(self, **k):
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
        """
        >>> 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)
        """
115 116 117
        return sorteditems(k)

    def tk(self, *a, **k):
118 119 120 121 122 123 124 125 126 127 128 129 130
        """
        >>> 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))
        """
131
        return a + sorteditems(k)
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

    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