3
from libc cimport stdint
4
from libc.stdint cimport int16_t as my_int16_t
19
cpdef tuple tuple_from_int_array():
21
>>> tuple_from_int_array()
28
assert isinstance(<tuple>v, tuple)
32
cdef extern from "stdint.h":
33
ctypedef unsigned long uint32_t
36
def from_typedef_int_array():
38
>>> from_typedef_int_array()
48
cpdef tuple tuple_from_typedef_int_array():
50
>>> tuple_from_typedef_int_array()
60
def from_cimported_int_array():
62
>>> from_cimported_int_array()
65
cdef stdint.int32_t[3] v
72
def from_cimported_as_int_array():
74
>>> from_cimported_as_int_array()
84
def from_int_array_array():
86
>>> from_int_array_array()
87
[[11, 12, 13], [21, 22, 23]]
99
def assign_int_array_array():
101
>>> assign_int_array_array()
102
[[11, 12, 13], [21, 22, 23]]
104
cdef int[2][3] v = [[11, 12, 13], [21, 22, 23]]
108
def assign_int_array_array_from_tuples():
110
>>> assign_int_array_array_from_tuples()
111
[[11, 12, 13], [21, 22, 23]]
113
cdef int[2][3] v = ([11, 12, 13], [21, 22, 23])
117
''' FIXME: this currently crashes:
118
def assign_int_array_array_from_tuples():
120
>>> assign_int_array_array_from_tuples()
121
[[11, 12, 13], [21, 22, 23]]
123
cdef int[2][3] v = ((11, 12, 13), (21, 22, 23))
128
def build_from_list_of_arrays():
130
>>> build_from_list_of_arrays()
131
[[11, 12, 13], [21, 22, 23]]
133
cdef int[3] x = [11, 12, 13]
134
cdef int[3] y = [21, 22, 23]
135
cdef int[2][3] v = [x, y]
139
def build_from_tuple_of_arrays():
141
>>> build_from_tuple_of_arrays()
142
[[11, 12, 13], [21, 22, 23]]
144
cdef int[3] x = [11, 12, 13]
145
cdef int[3] y = [21, 22, 23]
146
cdef int[2][3] v = (x, y)
150
ctypedef struct MyStructType:
160
def from_struct_array():
162
>>> a, b = from_struct_array()
168
cdef MyStructType[2] v
170
v[0] = MyStructType(1, 2)
171
v[1] = MyStructType(3, 4)
172
assert isinstance(<tuple>v, tuple)
173
assert isinstance(v, list)
175
w[0] = MyStruct(1, 2)
176
w[1] = MyStruct(3, 4)
177
assert (<object>w) == v
178
assert w == (<object>v)
185
>>> to_int_array([1, 2, 3])
187
>>> to_int_array([1, 2])
188
Traceback (most recent call last):
189
IndexError: not enough values found during array assignment, expected 3, got 2
190
>>> to_int_array([1, 2, 3, 4])
191
Traceback (most recent call last):
192
IndexError: too many values found during array assignment, expected 3
195
return v[0], v[1], v[2]
198
def to_int_array_array(x):
200
>>> to_int_array_array([[1, 2, 3], [4, 5, 6]])
202
>>> to_int_array_array(iter([[1, 2, 3], [4, 5, 6]]))
205
>>> to_int_array_array([[1, 2, 3]])
206
Traceback (most recent call last):
207
IndexError: not enough values found during array assignment, expected 2, got 1
208
>>> to_int_array_array(iter([[1, 2, 3]]))
209
Traceback (most recent call last):
210
IndexError: not enough values found during array assignment, expected 2, got 1
212
>>> to_int_array_array([[1, 2, 3], [4, 5]])
213
Traceback (most recent call last):
214
IndexError: not enough values found during array assignment, expected 3, got 2
215
>>> to_int_array_array(iter([[1, 2, 3], [4, 5]]))
216
Traceback (most recent call last):
217
IndexError: not enough values found during array assignment, expected 3, got 2
219
>>> to_int_array_array([[1, 2, 3, 4], [5, 6, 7]])
220
Traceback (most recent call last):
221
IndexError: too many values found during array assignment, expected 3
222
>>> to_int_array_array(iter([[1, 2, 3, 4], [5, 6, 7]]))
223
Traceback (most recent call last):
224
IndexError: too many values found during array assignment, expected 3
227
return v[0][0], v[0][1], v[0][2], v[1][0], v[1][1], v[1][2]
231
# FIXME: this isn't currently allowed
236
def to_int_array_array_enumsize(x):
238
>>> to_int_array_array([[1, 2, 3], [4, 5, 6]])
240
>>> to_int_array_array(iter([[1, 2, 3], [4, 5, 6]]))
242
>>> to_int_array([1, 2])
243
Traceback (most recent call last):
244
IndexError: not enough values found during array assignment, expected 3, got 2
245
>>> to_int_array([1, 2, 3, 4])
246
Traceback (most recent call last):
247
IndexError: too many values found during array assignment, expected 3
249
cdef int[SIZE_A][SIZE_B] v = x
250
return v[0][0], v[0][1], v[0][2], v[1][0], v[1][1], v[1][2]
255
# FIXME: this isn't currently supported
256
def array_as_argument(int[2] x):
258
>>> array_as_argument([1, 2])
265
def to_int_array_slice(x):
267
>>> to_int_array_slice([1, 2, 3])
269
>>> to_int_array_slice([1, 2])
270
Traceback (most recent call last):
271
IndexError: not enough values found during array assignment, expected 3, got 2
272
>>> to_int_array_slice([1, 2, 3, 4])
273
Traceback (most recent call last):
274
IndexError: too many values found during array assignment, expected 3
286
return v[0], v[1], v[2]
289
def iterable_to_int_array(x):
291
>>> iterable_to_int_array(iter([1, 2, 3]))
293
>>> iterable_to_int_array(iter([1, 2]))
294
Traceback (most recent call last):
295
IndexError: not enough values found during array assignment, expected 3, got 2
296
>>> iterable_to_int_array(iter([1, 2, 3, 4]))
297
Traceback (most recent call last):
298
IndexError: too many values found during array assignment, expected 3
302
return v[0], v[1], v[2]
305
def to_struct_array(x):
307
>>> a, b = to_struct_array(({'x': 1, 'y': 2}, {'x': 3, 'y': 4}))
313
cdef MyStructType[2] v
319
assert w[0].x == v[0].x
320
assert w[0].y == v[0].y
321
assert w[1].x == v[1].x
322
assert w[1].y == v[1].y
327
def to_struct_array_array(x):
329
>>> (a1, a2, a3), (b1, b2, b3) = to_struct_array_array([
330
... ({'x': 11, 'y': 12}, {'x': 13, 'y': 14}, {'x': 15, 'y': 16}),
331
... ({'x': 21, 'y': 22}, {'x': 23, 'y': 24}, {'x': 25, 'y': 26}),
338
cdef MyStructType[2][3] v = x
342
cdef struct StructWithArray:
347
def to_struct_with_array(x):
349
>>> x, y = to_struct_with_array([
350
... {'a': 11, 'b': [{'x': 12, 'y': 13}, {'x': 14, 'y': 15}]},
351
... {'a': 21, 'b': [{'x': 22, 'y': 23}, {'x': 24, 'y': 25}]},
355
>>> sorted(sorted(v.items()) for v in x['b'])
356
[[('x', 12), ('y', 13.0)], [('x', 14), ('y', 15.0)]]
357
>>> sorted(sorted(v.items()) for v in y['b'])
358
[[('x', 22), ('y', 23.0)], [('x', 24), ('y', 25.0)]]
360
>>> x, y = to_struct_with_array(iter([
361
... {'a': 11, 'b': iter([{'x': 12, 'y': 13}, {'x': 14, 'y': 15}])},
362
... {'a': 21, 'b': iter([{'x': 22, 'y': 23}, {'x': 24, 'y': 25}])},
366
>>> sorted(sorted(v.items()) for v in x['b'])
367
[[('x', 12), ('y', 13.0)], [('x', 14), ('y', 15.0)]]
368
>>> sorted(sorted(v.items()) for v in y['b'])
369
[[('x', 22), ('y', 23.0)], [('x', 24), ('y', 25.0)]]
371
cdef StructWithArray[2] v
376
def to_struct_with_array_slice(x):
378
>>> x, y = to_struct_with_array_slice([
379
... {'a': 11, 'b': [{'x': 12, 'y': 13}, {'x': 14, 'y': 15}]},
380
... {'a': 21, 'b': [{'x': 22, 'y': 23}, {'x': 24, 'y': 25}]},
384
>>> sorted(sorted(v.items()) for v in x['b'])
385
[[('x', 12), ('y', 13.0)], [('x', 14), ('y', 15.0)]]
386
>>> sorted(sorted(v.items()) for v in y['b'])
387
[[('x', 22), ('y', 23.0)], [('x', 24), ('y', 25.0)]]
389
>>> x, y = to_struct_with_array_slice(iter([
390
... {'a': 11, 'b': iter([{'x': 12, 'y': 13}, {'x': 14, 'y': 15}])},
391
... {'a': 21, 'b': iter([{'x': 22, 'y': 23}, {'x': 24, 'y': 25}])},
395
>>> sorted(sorted(v.items()) for v in x['b'])
396
[[('x', 12), ('y', 13.0)], [('x', 14), ('y', 15.0)]]
397
>>> sorted(sorted(v.items()) for v in y['b'])
398
[[('x', 22), ('y', 23.0)], [('x', 24), ('y', 25.0)]]
400
cdef StructWithArray[2] v
406
# FIXME: this isn't currently allowed
407
def to_struct_with_array_slice_end(x):
409
>>> to_struct_with_array_slice_end([
410
... {'a': 11, 'b': [{'x': 12, 'y': 13}, {'x': 14, 'y': 15}]},
412
[{'a': 11, 'b': [{'y': 13.0, 'x': 12}, {'y': 15.0, 'x': 14}]}]
413
>>> to_struct_with_array_slice_end(iter([
414
... {'a': 11, 'b': iter([{'x': 12, 'y': 13}, {'x': 14, 'y': 15}])},
416
[{'a': 11, 'b': [{'y': 13.0, 'x': 12}, {'y': 15.0, 'x': 14}]}]
417
>>> to_struct_with_array_slice_end(iter([
418
... {'a': 11, 'b': iter([{'x': 12, 'y': 13}, {'x': 14, 'y': 15}])},
419
... {'a': 21, 'b': iter([{'x': 22, 'y': 23}, {'x': 24, 'y': 25}])},
421
Traceback (most recent call last):
422
IndexError: too many values found during array assignment, expected 1
424
cdef StructWithArray[2] v
429
def to_int_array_slice_start_end(x):
431
>>> to_int_array_slice_start_end([1, 2, 3])
437
return v[0], v[1], v[2], v[3], v[4]