1
2
3
4
5
6
7
8
9 """Helpers for wrapping C libraries with ctypes."""
10
11 import numpy as N
12 import ctypes as C
13
14
15 c_darray = N.ctypeslib.ndpointer(dtype=N.float64, flags='aligned,contiguous')
16 c_larray = N.ctypeslib.ndpointer(dtype=N.int64, flags='aligned,contiguous')
17 c_farray = N.ctypeslib.ndpointer(dtype=N.float32, flags='aligned,contiguous')
18 c_iarray = N.ctypeslib.ndpointer(dtype=N.int32, flags='aligned,contiguous')
19
21 """Turn ndarray arguments into dims and arrays."""
22 arglist = []
23 for arg in args:
24 if isinstance(arg, N.ndarray):
25
26 arglist.extend(arg.shape)
27
28
29 arglist.append(arg)
30
31 return arglist
32
33
34
35
36
37
38 typemap = {
39 N.float64: C.c_double,
40 N.float32: C.c_float,
41 N.int64: C.c_int64,
42 N.int32: C.c_int32}
43
45 """Turn ndarray arguments into dims and array pointers for calling
46 a ctypes-wrapped function."""
47 arglist = []
48 for arg in args:
49 if isinstance(arg, N.ndarray):
50
51 arglist.extend(arg.shape)
52
53
54 arglist.append(arg.ctypes.data_as(
55 C.POINTER(typemap[arg.dtype.type])))
56 else:
57
58 arglist.append(arg)
59
60 return arglist
61
63 argtypes = []
64 for arg in args:
65 if isinstance(arg, N.ndarray):
66
67 argtypes.extend([C.c_int]*len(arg.shape))
68
69
70 argtypes.append(N.ctypeslib.ndpointer(dtype=arg.dtype))
71 else:
72
73 argtypes.append(arg)
74 return argtypes
75