Boost::python和swig的集成
boost::python and swig integration
我有两个类,第一个叫做"Radish",第二个叫做"RadishCont"。所有代码都是用c++编写的,需要在python中使用。
萝卜已暴露于python使用SWIG;相反,RadishCont已经使用boost::python.
公开了。我需要在RadishCont类中添加一个方法,它具有以下语法:
Radish* get_radish()
{
return &radish;
}
其中"radish"是RadishCont中包含的一个萝卜实例。
当我执行python代码时,我收到这个异常:
TypeError: No Python class registered for C++ class Radish
所以,我的问题是:我怎么能使这个方法在python工作不重写RadishCont使用SWIG ?
我非常确定您最好重写SWIG接口以包含新函数。SWIG为类/函数使用了一个内部注册系统,这个系统与boost::python系统不兼容(或者至少不是开箱使用的)。
最后,我完成了。
我在boost python模块中创建了一个类实例,如下所示:
class_<Radish>("Radish");
我创建了一个像这样的转换器函数:
static void* radishConvert(PyObject* obj)
{
char thisStr[] = "this";
//first we need to get the this attribute from the Python Object
if (!PyObject_HasAttrString(obj, thisStr))
return NULL;
PyObject* thisAttr = PyObject_GetAttrString(obj, thisStr);
if (thisAttr == NULL)
return NULL;
//This Python Object is a SWIG Wrapper and contains our pointer
void* pointer = ((PySwigObject*)thisAttr)->ptr;
Py_DECREF(thisAttr);
return pointer;
}
其中PySwigObject是一个像这样的结构体:
struct PySwigObject
{
PyObject_HEAD
void * ptr;
const char * desc;
};
最后,我注册了转换器:
boost::python::converter::registry::insert(&radishConvert, type_id<Radish>());
这非常有效,我可以从SWIG获取对象并将其传递给boost::python。相反的过程仍然没有解决,但对我来说已经足够了。
这是我用来找到解决方案的指南:http://wiki.python.org/moin/boost.python/HowTo SWIG_exposed_C.2B -.2B -_object_from_Python
这个错误来自Boost.Python。BP的类型表中没有关于萝卜的条目。没有入口,BP就不知道该怎么做。获得参赛资格的唯一方法是用BP包裹萝卜。
恐怕BP和SWIG根本就不是一起工作的,而且我非常怀疑你会让他们按照你想要的方式工作,没有大规模的黑客攻击。
我建议您将整个系统移动到其中一个库,或者另一个
- 将公共但非静态的成员函数与ALGLIB集成
- Python 3.7 和 excess_args 的 SWIG 问题
- 将IBM Rhapsody模型集成到VS 2019中
- 从R调用C++函数并对其进行集成时出错
- 从具有默认值的部分指定模板类继承时发生SWIG错误,具有不带默认值的正向声明
- 为重写std::exception的库生成swig接口时出错
- 元组由 Swig 生成的 Python 包装器返回,用于C++向量
- 无法使用 SWIG 在 Python 中实例化C++类(获取属性错误)
- SWIG Java 在使用 -DSWIGWORDSIZE64 时将int64_t转换为 jlong
- 使用 SWIG 更改生成的 CS 函数中的返回类型
- 如何集成 HID USB 控制器?
- 在模板化函数中重新定义(忽略)的 SWIG 标识符
- 使用 swig 追加到字节数组
- 如何使用 swig 修改类构造函数以保留对其中一个构造函数参数的引用?
- 在VS2019项目中集成ImageMagick:x64-windows-static library
- 将Qt集成到现有的VS项目中以取代WinAPI
- Java和SWIG:类集成到一个包中
- 使用SWIG将c++类集成到PHP中
- Boost::python和swig的集成
- 我如何将c++和Python与SWIG集成