我需要从我的c dll返回一个二维异构数据数组到python.
我为此目的从我的c dll返回一个元组元组.它以PyObject*的形式返回
这个元组元组需要作为tup [0] [0]访问第一行第一列tup [0] [1]第一行第二列......依此类推......在我的python代码中.
我使用ctypes来调用返回元组元组的c函数.但是,我无法访问python代码中返回的PyObject*.
extern "C" _declspec(dllexport) PyObject *FunctionThatReturnsTuple()
{
PyObject *data = GetTupleOfTuples();
return data; //(PyObject*)pFPy_BuildValue("O", data);
}
Run Code Online (Sandbox Code Playgroud)
在python脚本中我使用以下 -
libc = PyDLL("MyCDLL.dll")
x = libc.FunctionThatReturnsTuple()
if x != None :
print str( x[0][0] )
print str( x[0][1] )
Run Code Online (Sandbox Code Playgroud)
但是我得到一个错误 - 'int'对象不是可订阅的.我认为这是因为x被接收为指针.
实现这一目标的正确方法是什么?
您没有设置"FunctionThatReturnsTuple"的返回类型.
在C:
#include <Python.h>
extern "C" PyObject* FunctionThatReturnsTuple()
{
PyObject* tupleOne = Py_BuildValue("(ii)",1,2);
PyObject* tupleTwo = Py_BuildValue("(ii)",3,4);
PyObject* data = Py_BuildValue("(OO)", tupleOne, tupleTwo);
return data;
}
Run Code Online (Sandbox Code Playgroud)
蟒蛇:
>>> from ctypes import *
>>> libc = PyDLL("./test.dll")
>>> func = libc.FunctionThatReturnsTuple
>>> func()
-1215728020
>>> func.restype = py_object
>>> func()
((1, 2), (3, 4))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1366 次 |
| 最近记录: |