如何在不构建模块的情况下将C ++类暴露给Python

编程入门 行业动态 更新时间:2024-10-28 18:34:14
本文介绍了如何在不构建模块的情况下将C ++类暴露给Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道是否有任何方法将C ++类暴露给Python,但没有构建中间共享库。

I want to know if there is any way to expose a C++ class to Python but without building an intermediate shared library.

这是我的理想情况。例如我有以下C ++类:

Here is my desirable scenario. For example I have following C++ class:

class toto { public: toto(int iValue1_, int iValue2_): iValue1(iValue1_), iValue2(iValue2_) {} int Addition(void) const {if (!this) return 0; return iValue1 + iValue2;} private: int iValue1; int iValue2; };

我想将这个类(或其intance)转换为PyObject *它作为paremter(args)到例如PyObject_CallObject:

I would like to convert somehow this class (or its intance) to a PyObject* in order to send it as paremter (args) to for example PyObject_CallObject:

PyObject* PyObject_CallObject(PyObject* wrapperFunction, PyObject* args)

另一方面,在我的python方面,我将有一个wrapperFunction获取我的C ++类的指针或其实例)作为参数,并调用其方法或使用其属性:

In the other hand in my python side, I'll have a wrapperFunction which gets the pointer on my C++ class (or its instance) as parameter and it calls its methods or uses its properties:

def wrapper_function(cPlusPlusClass): instance = cPlusPlusClass(4, 5) result = instance.Addition()

可以看到,我真的不需要/想有一个单独的共享库或通过boost python构建一个模块。我需要的是找到一种方法来将一个C ++代码转换为PyObject并将其发送到python。我找不到一个办法做到这一点的C python库,boost或SWIG。

As you can see, I don't really need/want to have a separate shared library or build a module by boost python. All that I need is to find a way to convert a C++ code to PyObject and send it to python. I cannot find a way to do that by C python libraries, boost or SWIG.

你有什么想法吗? 感谢您的帮助。

Do you have any idea? Thanks for your help.

推荐答案

我找到了我的答案。实际上我正在搜索是非常类似于这个答案(感谢moooeeeep他的评论):

I found my answer. Actually what I was searching was pretty similar to this answer (thanks moooeeeep for his comment):

将一个C ++类实例暴露给一个python嵌入式解释器

C ++类(注意!默认构造函数是强制性的):

Following C++ class (Attention! default constructor is mandatory):

class TwoValues { public: TwoValues(void): iValue1(0), iValue2(0) {} TwoValues(int iValue1, int iValue2): iValue1(iValue1_), iValue2(iValue2_) {} int Addition(void) const {if (!this) return 0; return iValue1 + iValue2;} public: int iValue1; int iValue2; };

可以通过以下宏显示:

BOOST_PYTHON_MODULE(ModuleTestBoost) { class_<TwoValues>("TwoValues") .def("Addition", &TWOVALUES::Addition) .add_property("Value1", &TWOVALUES::iValue1) .add_property("Value2", &TWOVALUES::iValue2); };

另一方面,我有一个python函数定义在 python_script.py 它接受这个类的一个实例并做某事。例如:

In the other hand I have a python function defined in python_script.py which takes an instance of this class and do something. For example:

def wrapper_function(instance): result = instance.Addition() myfile = open(r"C:\...\testboostexample.txt", "w") output = 'First variable is {0}, second variable is {1} and finally the addition is {2}'.format(instance.Value1, instance.Value2, result) myfile .write(output) myfile .close()

然后在C ++方面,我可以通过同时发送我的类的实例来调用此函数,如下所示:

Then in C++ side, I can call this function by sending at the same time the instance of my class, like this:

Py_Initialize(); try { TwoValues instance(5, 10); initModuleTestBoost(); object python_script = import("python_script"); object wrapper_function = python_script.attr("wrapper_function"); wrapper_function(&instance); } catch (error_already_set) { PyErr_Print(); } Py_Finalize();

优点:

  • 我不需要构建任何共享库或二进制文件
  • 由于我使用Boost,我不需要担心内存管理&引用计数
  • 我不使用共享boost指针(boost :: shared_ptr)指向我的类的实例

更多推荐

如何在不构建模块的情况下将C ++类暴露给Python

本文发布于:2023-11-23 09:20:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   情况下   如何在   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!