python一个函数调用另一个函数的返回值_发出从Python调用的C函数返回值的问题...

1586010002-jmsa.png

I'm have difficulty with a seeming simple problem. There are various C functions that I need to call from Python code. Currently, I'm trying to do this through ctypes. I'm having issues with a simple example implementation that I'm using to ensure that everything works as intended before changing large chunks of existing code. The C functions take a few parameters, do various mathematical calculations, and then return a double value. This is the C file that I'm using as a test.

#include "Python.h"

double eval(double* args, int length, double* rands, int randLength, int debug)

{

double val1 = args[0];

double val2 = rands[0];

double ret = val1 + val2;

return ret;

}

In my actual test function I'm printing val1 and val2 to ensure they're receiving values. Everything is fine on that end. The Python that calls this function is the following.

test = ctypes.CDLL("/home/mjjoyce/Dev/C2M2L/AquaticWavesModel.so")

rand = Random();

args = [2]

argsArr = (ctypes.c_double * len(args))()

argsArr[:] = args

argsRand = [rand.random()]

argsRandArr = (ctypes.c_double * len(argsRand))()

rgsRandArr[:] = argsRand

result = test.eval(argsArr, len(argsArr), argsRandArr, len(argsRandArr), 0)

print "result", result

When this prints the returned result, it usually dumps a large negative number. It's my assumption that I need to do some converting before Python can handle the value, but I can't find an answer for that for the life of me. I've searched forever, but I'm not certain if I'm missing the obvious, or searching for the wrong information.

Also, if I change the return type of the function to int, the calculations turn out correct and an int is returned and printed as expected. If I set it to double, the calculations are correct in the C code (I print them to check), but Python doesn't like the returned result.

Any ideas what I'm missing?

解决方案

You should set the function's argument types and return type in Python using the argtypes and restype members:

test.eval.argtypes = [ctypes.POINTER(ctypes.c_double),

ctypes.c_int,

ctypes.POINTER(ctypes.c_double),

ctypes.c_int,

ctypes.c_int]

test.eval.restype = ctypes.c_double

Then you can call it like you're doing: construct arrays of type (ctypes.c_double * length) initialized with your values, and the result will be a double.

See also this ctypes tutorial, it has lots of useful info.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值