我第一次在Python中使用ctypes
进行实验。我写了一个C文件,其中包含进行计算的方法,以便旋转点(和曲线)。我的代码是:
#include "math.h"
double * rotatePoint(double P[2], double angle) {
static double Q[2];
Q[0] = P[0] * cos(angle * M_PI/180);
Q[1] = P[1] * sin(angle * M_PI/180);
return Q;
}
我用GCC编译了它
gcc -lm -shared lib.c -o lib.so
在我的Python代码中:
import ctypes
lib = ctypes.CDLL('lib.so')
def cRotatePoint(P):
#how I parse P in order to be understood by ctypes?
#.....
lib.rotatePoint(P)
#how can I return the array back?
#....
return P
你能帮我解决这些问题:
使用ctypes
在Python中将结果转换为double []并将其返回
由于
答案 0 :(得分:3)
所以诀窍在于确保ctypes知道你的函数的参数类型是什么。
事情的C方面很好,但在Python方面,你需要为你的函数指定restype
和argtypes
:
import ctypes
lib = ctypes.CDLL('lib.so')
# let ctypes know the argument types
lib.rotatePoint.argtypes = [ctypes.POINTER(ctypes.c_double),ctypes.c_double]
# let ctypes know the return type
lib.rotatePoint.restype = ctypes.POINTER(ctypes.c_double)
def c_rotate_point(p,angle):
# convert arguments to appropriate ctypes type
p = (ctypes.c_double*2)(*p)
angle = ctypes.c_double(angle)
# call the function
retval = lib.rotatePoint(p,angle)
# here, retval has type LP_c_double object
# indexing this will dereference the underlying pointer giving the results
print retval[0],retval[1]
if __name__ == "__main__":
c_rotate_point((0.2,0.3),0.4)
对于这些值,我得到输出:
0.199995126141 0.00209437808939
答案 1 :(得分:1)
@ebarr provided a complete code example。这是一个小小的变化。
要允许从多个线程调用该函数,请不要使用static
变量:
#include "math.h"
void rotatePoint(double P[2], double angle, double Q[2]) {
Q[0] = P[0] * cos(angle * M_PI/180);
Q[1] = P[1] * sin(angle * M_PI/180);
}
您可以将double P[2]
作为数组类型c_double * 2
传递:
import ctypes
from collections import namedtuple
ArrayType = ctypes.c_double * 2
Point = namedtuple('Point', 'x y')
lib = ctypes.CDLL('lib.so')
lib.rotatePoint.argtypes = [ArrayType, ctypes.c_double, ArrayType]
lib.rotatePoint.restype = None # void
def rotate_point(p, angle):
retval = ArrayType()
lib.rotatePoint(ArrayType(*p), angle, retval)
return Point(*retval)
if __name__ == "__main__":
print(rotate_point((0.2,0.3), 0.4))
请注意,您不需要明确地转换angle
(标量类型)。