如何在Python中处理C++中的PyObject*
how to deal with the PyObject* from C++ in Python
我创建了用C++编写的DLL,导出函数返回PyObject*。然后我用ctypes在Python中导入DLL。现在,我怎样才能得到真正的PyObject??
这是c++代码的一部分:
PyObject* _stdcall getList(){
PyObject * PList = NULL;
PyObject * PItem = NULL;
PList = PyList_New(10);
vector <int> intVector;
int i;
for(int i=0;i<10;i++){
intVector.push_back(i);
}
for(vector<int>::const_iterator it=intVector.begin();it<intVector.end();it++){
PItem = Py_BuildValue("i", &it);
PyList_Append(PList, PItem);
}
return PList;
}
以及一些python代码:
dll = ctypes.windll.LoadLibrary(DllPath)
PList = dll.getList()
*我想得到包含1,2,3,4…10的真实python列表吗*我明白了吗??感谢提前
您的代码有很多问题,有些修改:
#include <Python.h>
#include <vector>
extern "C" PyObject* _stdcall getList(){
PyObject *PList = PyList_New(0);
std::vector <int> intVector;
std::vector<int>::const_iterator it;
for(int i = 0 ; i < 10 ; i++){
intVector.push_back(i);
}
for(it = intVector.begin(); it != intVector.end() ; it++ ){
PyList_Append(PList, Py_BuildValue("i", *it));
}
return PList;
}
编译:
> g++ -Wall -shared lib.cpp -I Python27include -L Python27libs -lpython27 -o lib.dll -Wl,--add-stdcall-alias
现在您可以将其加载为任何函数,并将getList
返回类型设置为py_object
为:
import ctypes
lib = ctypes.WinDLL('lib.dll')
getList = lib.getList
getList.argtypes = None
getList.restype = ctypes.py_object
getList()
测试它:
>>> import ctypes
>>>
>>> lib = ctypes.WinDLL('lib.dll')
>>>
>>> getList = lib.getList
>>> getList.argtypes = None
>>> getList.restype = ctypes.py_object
>>> getList()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
>>>
使用Visual Studio和Python 64位:
1-创建一个空的Win32项目(DLL类型)
2-右键单击解决方案项目->Configuration Manager
3-主动解决方案配置(发布)
4-活动解决方案平台->新建,然后在底部下拉列表中选择x64->确定
5-在源文件文件夹中,添加一个空的C++文件
6-放入您的C++代码(一次修改以识别getList)
#include <Python.h>
#include <vector>
extern "C" __declspec(dllexport) PyObject* _stdcall getList();
PyObject* _stdcall getList(){
PyObject *PList = PyList_New(0);
std::vector <int> intVector;
std::vector<int>::const_iterator it;
for (int i = 0; i < 10; i++){
intVector.push_back(i);
}
for (it = intVector.begin(); it != intVector.end(); it++){
PyList_Append(PList, Py_BuildValue("i", *it));
}
return PList;
}
我不太清楚你在问什么。但我想你的意思是问你现在可以用你的DLL做什么。
-
好吧,为了适当地使用它,你必须构建一个特殊的DLL,它可以直接作为Python中的模块导入。为了确定该做什么来使用它,你最好看看其他模块,它们是如何做到的。例如,
MySQLdb
可能是一个候选者。简而言之,你有这个";包装器";DLL调用您的函数。
-
但如果我现在再看一眼你的问题,我会发现你正试图通过
ctypes
加载你的DLL。这也是可行的,甚至可能更好,并且您必须使用ctypes.py_object
数据类型。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- .cpp和.h文件中的模板专用化声明
- 反向给定链表中的K节点
- 正在查找文档以获得PS4平台的C++中的设备信息
- enum是C++中的宏变量还是整数变量
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 将字符串存储在c++中的稳定内存中
- 文本文件中的单词链表
- 递归函数计算序列中的平方和(并输出过程)
- 如何从C++中的依赖类型中获得它所依赖的类型
- C++中的"inline"关键字
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 用C++中的一个变量定义一个常量
- vector.resize()中的分配错误
- 使用指针从C++中的数组中获取最大值
- arr[-1]在c++中的奇怪行为
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 将值指定给向量(2D)的向量中的某个位置
- 内置函数可查看CPP中的成员变量