发出从Python调用的C函数返回值的问题 [英] Issue returning values from C function called from Python

查看:451
本文介绍了发出从Python调用的C函数返回值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难解决一个看似简单的问题。我需要从Python代码中调用各种C函数。目前,我正在尝试通过ctypes完成此操作。我在使用一个简单的示例实现时遇到了问题,该示例实现用于确保在更改大量现有代码之前一切都能按预期工作。 C函数采用一些参数,进行各种数学计算,然后返回一个双精度值。这是我正在用作测试的C文件。

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;
}

在我的实际测试函数中,我正在打印 val1 val2 以确保它们正在接收值。为此,一切都很好。以下是调用此函数的Python。

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

当打印返回的结果时,通常会转储一个很大的负数。我以为我需要先进行一些转换,然后Python才能处理该值,但我终生无法找到答案。我一直在搜索,但是不确定是否遗漏了明显的信息或搜索错误的信息。

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.

此外,如果我将函数的返回类型更改为int,则计算结果正确,并且按预期方式返回并打印了int。如果将其设置为double,则C代码中的计算是正确的(我将其打印以进行检查),但是Python不喜欢返回的结果。

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.

任何想法我缺少什么?

推荐答案

您应该使用 argtypes 和 restype 成员:

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

然后您可以称之为就像您正在做的那样:构造类型为(ctypes.c_double * length)的数组,并使用您的值进行初始化,并生成 result 将是的两倍

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.

另请参见以下ct是的教程,其中包含许多有用的信息。

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

这篇关于发出从Python调用的C函数返回值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆