summaryrefslogtreecommitdiff
path: root/tests/testsupport/cythonarrayutil.pxi
blob: 50d764acd675561b9f5d44b3fb62afd8c519a644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from libc.stdlib cimport malloc, free
cimport cython
from cython.view cimport array

cdef void callback(void *data):
    print "callback called"
    free(data)

def create_array(shape, mode, use_callback=False):
    cdef array result = array(shape, itemsize=sizeof(int),
                              format='i', mode=mode)
    cdef int *data = <int *> result.data
    cdef int i, j, cidx, fidx

    for i in range(shape[0]):
        for j in range(shape[1]):
            cidx = i * shape[1] + j
            fidx = i + j * shape[0]

            if mode == 'fortran':
                data[fidx] = cidx
            else:
                data[cidx] = cidx

    if use_callback:
        result.callback_free_data = callback

    return result