call_py_cy.pyx 6.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# mode: run
# tag: cyfunction,call,python
# cython: binding=True

#######
# Test that Cython and Python functions can call each other in various signature combinations.
#######

py_call_noargs = eval("lambda: 'noargs'")
py_call_onearg = eval("lambda arg: arg")
py_call_twoargs = eval("lambda arg, arg2: (arg, arg2)")
py_call_starargs = eval("lambda *args: args")
py_call_pos_and_starargs = eval("lambda arg, *args: (arg, args)")
py_call_starstarargs = eval("lambda **kw: sorted(kw.items())")
Stefan Behnel's avatar
Stefan Behnel committed
15
py_call_args_and_starstarargs = eval("lambda *args, **kw: (args, sorted(kw.items()))")
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35


def cy_call_noargs():
    """
    >>> cy_call_noargs()
    'noargs'
    """
    return py_call_noargs()


def cy_call_onearg(f):
    """
    >>> cy_call_onearg(py_call_onearg)
    'onearg'
    >>> try: cy_call_onearg(py_call_noargs)
    ... except TypeError: pass
    ... else: print("FAILED!")
    >>> try: cy_call_onearg(py_call_twoargs)
    ... except TypeError: pass
    ... else: print("FAILED!")
Stefan Behnel's avatar
Stefan Behnel committed
36 37 38 39 40 41

    >>> class Class(object):
    ...     def method(self, arg): return arg

    >>> cy_call_onearg(Class().method)
    'onearg'
42 43 44 45 46 47 48 49
    """
    return f('onearg')


def cy_call_twoargs(f, arg):
    """
    >>> cy_call_twoargs(py_call_twoargs, 132)
    (132, 'twoargs')
Stefan Behnel's avatar
Stefan Behnel committed
50 51 52 53 54 55 56 57 58 59

    >>> class Class2(object):
    ...     def method(self, arg, arg2): return arg, arg2
    >>> cy_call_twoargs(Class2().method, 123)
    (123, 'twoargs')

    >>> class Class1(object):
    ...     def method(self, arg): return arg
    >>> cy_call_twoargs(Class1.method, Class1())
    'twoargs'
60 61 62 63 64 65
    """
    return f(arg, 'twoargs')


def cy_call_two_kwargs(f, arg):
    """
Stefan Behnel's avatar
Stefan Behnel committed
66 67 68 69 70 71 72 73 74 75 76 77
    >>> cy_call_two_kwargs(py_call_twoargs, arg=132)
    (132, 'two-kwargs')
    >>> cy_call_two_kwargs(f=py_call_twoargs, arg=132)
    (132, 'two-kwargs')
    >>> cy_call_two_kwargs(arg=132, f=py_call_twoargs)
    (132, 'two-kwargs')

    >>> class Class(object):
    ...     def method(self, arg, arg2): return arg, arg2

    >>> cy_call_two_kwargs(Class().method, 123)
    (123, 'two-kwargs')
78
    """
Stefan Behnel's avatar
Stefan Behnel committed
79
    return f(arg2='two-kwargs', arg=arg)
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117


def cy_call_starargs(*args):
    """
    >>> cy_call_starargs()
    ()
    >>> cy_call_starargs(1)
    (1,)
    >>> cy_call_starargs(1, 2)
    (1, 2)
    >>> cy_call_starargs(1, 2, 3)
    (1, 2, 3)
    """
    return py_call_starargs(*args)


def cy_call_pos_and_starargs(f, *args):
    """
    >>> cy_call_pos_and_starargs(py_call_onearg)
    'no-arg'
    >>> cy_call_pos_and_starargs(py_call_onearg, 123)
    123
    >>> cy_call_pos_and_starargs(py_call_twoargs, 123, 321)
    (123, 321)
    >>> cy_call_pos_and_starargs(py_call_starargs)
    ('no-arg',)
    >>> cy_call_pos_and_starargs(py_call_starargs, 123)
    (123,)
    >>> cy_call_pos_and_starargs(py_call_starargs, 123, 321)
    (123, 321)
    >>> cy_call_pos_and_starargs(py_call_pos_and_starargs)
    ('no-arg', ())
    >>> cy_call_pos_and_starargs(py_call_pos_and_starargs, 123)
    (123, ())
    >>> cy_call_pos_and_starargs(py_call_pos_and_starargs, 123, 321)
    (123, (321,))
    >>> cy_call_pos_and_starargs(py_call_pos_and_starargs, 123, 321, 234)
    (123, (321, 234))
Stefan Behnel's avatar
Stefan Behnel committed
118 119 120 121 122 123 124 125

    >>> class Class(object):
    ...     def method(self, arg, arg2): return arg, arg2

    >>> cy_call_pos_and_starargs(Class().method, 123, 321)
    (123, 321)
    >>> cy_call_pos_and_starargs(Class.method, Class(), 123, 321)
    (123, 321)
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    """
    return f(args[0] if args else 'no-arg', *args[1:])


def cy_call_starstarargs(**kw):
    """
    >>> kw = {}
    >>> cy_call_starstarargs(**kw)
    []
    >>> kw = {'a': 123}
    >>> cy_call_starstarargs(**kw)
    [('a', 123)]
    >>> kw = {'a': 123, 'b': 321}
    >>> cy_call_starstarargs(**kw)
    [('a', 123), ('b', 321)]
    """
    return py_call_starstarargs(**kw)


def cy_call_kw_and_starstarargs(f=None, arg1=None, **kw):
    """
    >>> kw = {}
    >>> cy_call_kw_and_starstarargs(**kw)
    [('arg', None)]
    >>> try: cy_call_kw_and_starstarargs(py_call_noargs, **kw)
    ... except TypeError: pass
    >>> try: cy_call_kw_and_starstarargs(py_call_twoargs, **kw)
    ... except TypeError: pass
    ... else: print("FAILED!")
    >>> cy_call_kw_and_starstarargs(py_call_onearg, **kw)
    >>> cy_call_kw_and_starstarargs(f=py_call_onearg, **kw)
    >>> cy_call_kw_and_starstarargs(py_call_pos_and_starargs, **kw)
    (None, ())

    >>> kw = {'arg1': 123}
    >>> cy_call_kw_and_starstarargs(**kw)
    [('arg', 123)]
    >>> cy_call_kw_and_starstarargs(py_call_onearg, **kw)
    123
    >>> cy_call_kw_and_starstarargs(f=py_call_onearg, **kw)
    123
    >>> cy_call_kw_and_starstarargs(py_call_twoargs, arg2=321, **kw)
    (123, 321)
    >>> cy_call_kw_and_starstarargs(f=py_call_twoargs, arg2=321, **kw)
    (123, 321)
    >>> try: cy_call_kw_and_starstarargs(py_call_twoargs, **kw)
    ... except TypeError: pass
    ... else: print("FAILED!")
    >>> try: cy_call_kw_and_starstarargs(py_call_twoargs, arg2=321, other=234, **kw)
    ... except TypeError: pass
    ... else: print("FAILED!")
    >>> cy_call_kw_and_starstarargs(py_call_pos_and_starargs, **kw)
    (123, ())

    >>> try: cy_call_kw_and_starstarargs(arg=321, **kw)   # duplicate kw in Python call
    ... except TypeError: pass
    ... else: print("FAILED!")

    >>> kw = {'a': 123}
    >>> cy_call_kw_and_starstarargs(**kw)
    [('a', 123), ('arg', None)]
    >>> cy_call_kw_and_starstarargs(arg1=321, **kw)
    [('a', 123), ('arg', 321)]

    >>> kw = {'a': 123, 'b': 321}
    >>> cy_call_kw_and_starstarargs(**kw)
    [('a', 123), ('arg', None), ('b', 321)]
    >>> cy_call_kw_and_starstarargs(arg1=234, **kw)
    [('a', 123), ('arg', 234), ('b', 321)]
Stefan Behnel's avatar
Stefan Behnel committed
195 196 197 198 199 200

    >>> class Class2(object):
    ...     def method(self, arg, arg2): return arg, arg2

    >>> cy_call_kw_and_starstarargs(Class2().method, arg1=123, arg2=321)
    (123, 321)
201 202
    """
    return (f or py_call_starstarargs)(arg=arg1, **kw)
Stefan Behnel's avatar
Stefan Behnel committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232


def cy_call_pos_and_starstarargs(f=None, arg1=None, **kw):
    """
    >>> cy_call_pos_and_starstarargs(arg=123)
    ((None,), [('arg', 123)])
    >>> cy_call_pos_and_starstarargs(arg1=123)
    ((123,), [])
    >>> cy_call_pos_and_starstarargs(arg=123, arg2=321)
    ((None,), [('arg', 123), ('arg2', 321)])
    >>> cy_call_pos_and_starstarargs(arg1=123, arg2=321)
    ((123,), [('arg2', 321)])

    >>> class Class2(object):
    ...     def method(self, arg, arg2=None): return arg, arg2

    >>> cy_call_pos_and_starstarargs(Class2().method, 123)
    (123, None)
    >>> cy_call_pos_and_starstarargs(Class2().method, 123, arg2=321)
    (123, 321)
    >>> cy_call_pos_and_starstarargs(Class2().method, arg1=123, arg2=321)
    (123, 321)
    >>> cy_call_pos_and_starstarargs(Class2.method, Class2(), arg=123)
    (123, None)
    >>> cy_call_pos_and_starstarargs(Class2.method, Class2(), arg=123, arg2=321)
    (123, 321)
    >>> cy_call_pos_and_starstarargs(Class2.method, arg1=Class2(), arg=123, arg2=321)
    (123, 321)
    """
    return (f or py_call_args_and_starstarargs)(arg1, **kw)