我可以使用生成的 swig 代码来转换 C++对象到 PyObject?

发布于 2024-11-04 13:13:50 字数 221 浏览 3 评论 0原文

我正在使用 swig 将 python 嵌入到我的 C++ 程序中。目前我有一个用 C++ 编写的对象,我想将其传递给 python 函数。我创建了 swig 接口来包装该类。

我想做的就是获取我创建的这个 C++ 对象并将其传递给一个 python 函数,该函数能够像在 C++ 中一样使用它。 我可以使用 swig 生成的代码来执行此操作吗?如果不是,我该如何解决这个问题?

I'm working on embedding python into my C++ program using swig. At the moment I have a object written in C++ which I want to pass to a python function. I've created the swig interface to wrap the class.

What I'm trying to do is take this C++ object which I've created and pass it to a python function with the ability to use it like I would in C++. Is it possible for me to use code generate by swig to do this? If not how can I approach this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肤浅与狂妄 2024-11-11 13:13:50

您可以使用 PyObject_CallMethod 将新创建的对象传递回 python。假设 ModuleName.object 是一个带有名为 methodName 的方法的 python 对象,您想要将一个新创建的 C++ 对象粗略地传递给您想要的对象(从内存中,我不能立即测试)在 C++ 中执行此操作:

int callPython() {
   PyObject* module = PyImport_ImportModule("ModuleName");
   if (!module)
      return 0;

   // Get an object to call method on from ModuleName
   PyObject* python_object = PyObject_CallMethod(module, "object", "O", module);
   if (!python_object) {
      PyErr_Print();
      Py_DecRef(module);
      return 0;
   }

   // SWIGTYPE_p_Foo should be the SWIGTYPE for your wrapped class and
   // SWIG_POINTER_NEW is a flag indicating ownership of the new object
   PyObject *instance = SWIG_NewPointerObj(SWIG_as_voidptr(new Foo()), SWIGTYPE_p_Foo, SWIG_POINTER_NEW);

   PyObject *result = PyObject_CallMethod(python_object, "methodName", "O", instance);
   // Do something with result?

   Py_DecRef(instance);
   Py_DecRef(result);  
   Py_DecRef(module);

   return 1;
}

认为我已经获得了正确的引用计数,但我不完全确定。

You can use PyObject_CallMethod to pass a newly created object back to python. Assuming ModuleName.object is a python object with a method called methodName that you want to pass a newly created C++ object to you want to roughly (from memory, I can't test it right now) do this in C++:

int callPython() {
   PyObject* module = PyImport_ImportModule("ModuleName");
   if (!module)
      return 0;

   // Get an object to call method on from ModuleName
   PyObject* python_object = PyObject_CallMethod(module, "object", "O", module);
   if (!python_object) {
      PyErr_Print();
      Py_DecRef(module);
      return 0;
   }

   // SWIGTYPE_p_Foo should be the SWIGTYPE for your wrapped class and
   // SWIG_POINTER_NEW is a flag indicating ownership of the new object
   PyObject *instance = SWIG_NewPointerObj(SWIG_as_voidptr(new Foo()), SWIGTYPE_p_Foo, SWIG_POINTER_NEW);

   PyObject *result = PyObject_CallMethod(python_object, "methodName", "O", instance);
   // Do something with result?

   Py_DecRef(instance);
   Py_DecRef(result);  
   Py_DecRef(module);

   return 1;
}

I think I've got the reference counting right for this, but I'm not totally sure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文