8
>>> simple_convert(1, 2)
12
Traceback (most recent call last):
13
TypeError: Expected a sequence of size 2, got size 1
14
>>> simple_convert(1, 2, 3)
15
Traceback (most recent call last):
16
TypeError: Expected a sequence of size 2, got size 3
18
cdef (int, double) xy = o
22
def convert_from_list(*o):
24
>>> convert_from_list(1, 2)
27
>>> convert_from_list(1)
28
Traceback (most recent call last):
29
TypeError: Expected a sequence of size 2, got size 1
30
>>> convert_from_list(1, 2, 3)
31
Traceback (most recent call last):
32
TypeError: Expected a sequence of size 2, got size 3
34
cdef object values = list(o)
35
cdef (int, double) xy = values
39
def convert_from_deque(*o):
41
>>> convert_from_deque(1, 2)
44
>>> convert_from_deque(1)
45
Traceback (most recent call last):
46
TypeError: Expected a sequence of size 2, got size 1
47
>>> convert_from_deque(1, 2, 3)
48
Traceback (most recent call last):
49
TypeError: Expected a sequence of size 2, got size 3
51
from collections import deque
52
cdef object values = deque(o)
53
cdef (int, double) xy = values
57
def indexing((int, double) xy):
68
def unpacking((int, double) xy):
76
cdef (int, double) side_effect((int, double) xy):
77
print "called with", xy
80
def unpacking_with_side_effect((int, double) xy):
82
>>> unpacking_with_side_effect((1, 2))
86
x, y = side_effect(xy)
89
def packing_tuple(int x, double y):
91
>>> packing_tuple(1, 2)
94
cdef (int, double) xy = (x, y)
95
assert xy == (x, y), xy
97
assert xy == (x, y), xy
101
def packing_list(int x, double y):
103
>>> packing_list(1, 2)
106
cdef (int, double) xy = [x, y]
107
assert xy == (x, y), xy
109
assert xy == (x, y), xy
113
def coerce_packing_tuple(int x, int y):
114
cdef (int, double) xy = (x, y)
116
>>> coerce_packing_tuple(1, 2)
121
def c_types(int a, double b):
128
cdef (int*, double*) ab = (&a, &b)
130
return a_ptr[0], b_ptr[0]
138
def union_in_ctuple_literal():
140
>>> union_in_ctuple_literal()
143
cdef (Union,) a = ({"x": 1},)
144
cdef (Union,) b = ({"y": 2},)
145
return a[0].x, b[0].y
148
def union_in_ctuple_dynamic(*values):
150
>>> union_in_ctuple_dynamic(1, {'x': 1})
152
>>> union_in_ctuple_dynamic(2, {'y': 2})
154
>>> union_in_ctuple_dynamic(1, {'x': 1, 'y': 2})
155
Traceback (most recent call last):
156
ValueError: More than one union attribute passed: 'x' and 'y'
158
cdef (int, Union) a = values
159
return a[1].x if a[0] == 1 else a[1].y
162
cdef (int, int*) cdef_ctuple_return_type(int x, int* x_ptr):
165
def call_cdef_ctuple_return_type(int x):
167
>>> call_cdef_ctuple_return_type(2)
170
cdef (int, int*) res = cdef_ctuple_return_type(x, &x)
171
return res[0], res[1][0]
174
cpdef (int, double) cpdef_ctuple_return_type(int x, double y):
176
>>> cpdef_ctuple_return_type(1, 2)
182
def cast_to_ctuple(*o):
184
>>> cast_to_ctuple(1, 2.)
189
x, y = <(int, double)>o
193
@cython.infer_types(True)
194
def test_type_inference():
196
>>> test_type_inference()
202
assert cython.typeof(xy) == "(int, double)", cython.typeof(xy)
204
assert cython.typeof(xo) == "tuple object", cython.typeof(xo)
207
@cython.locals(a=(int,int), b=(cython.long,cython.float))
208
def test_pure_python_declaration(x, y):
210
>>> test_pure_python_declaration(1, 2)
214
>>> test_pure_python_declaration(1.0, 2.0)
218
>>> test_pure_python_declaration('x', 'y')
219
Traceback (most recent call last):
220
TypeError: an integer is required
224
print(cython.typeof(a))
225
print(cython.typeof(b))
229
def test_equality((int, int) ab, (int, int) cd, (int, int) ef):
231
>>> test_equality((1, 2), (3, 4), (5, 6))
233
>>> test_equality((1, 2), (3, 4), (3, 4))
235
>>> test_equality((3, 4), (3, 4), (3, 4))
240
def test_equality_different_types((double, int) ab, (int, int) cd, (long, int) ef):
242
>>> test_equality((1, 2), (3, 4), (5, 6))
244
>>> test_equality((1, 2), (3, 4), (3, 4))
246
>>> test_equality((3, 4), (3, 4), (3, 4))
251
def test_binop((int, int) ab, (double, double) cd):
253
>>> test_binop((1, 2), (3, 4))
258
def test_mul((int, int) ab, int c):
260
>>> test_mul((1, 2), 3)
265
def test_mul_to_ctuple((int, int) ab, int c):
267
>>> test_mul_to_ctuple((1, 2), 2)
269
>>> test_mul_to_ctuple((1, 2), 3)
270
Traceback (most recent call last):
271
TypeError: Expected a sequence of size 4, got size 6
273
result: tuple[cython.int, cython.int, cython.int, cython.int] = ab * c
276
def test_unop((int, int) ab):
278
>>> test_unop((1, 2))
283
# This is testing both that fused ctuples work, and that
284
# different fused ctuples end up as separate types
285
cdef (cython.floating, cython.floating) two_fused(cython.floating x):
288
cdef (cython.floating, cython.floating, cython.floating) three_fused(cython.floating x):
293
>>> a, b = test_fused()
299
return two_fused(1.0), three_fused(2.0)