summaryrefslogtreecommitdiff
path: root/tests/testsupport/cythonarrayutil.pxi
blob: 683dc4b71402a94b0877c904767bc1b3f5dc3b8c (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) noexcept:
    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