[英]Get a c++ pointer to a Python instance using Boost::Python
I am working on embedding Python inside of a C++ application. 我正在努力将Python嵌入到C ++应用程序中。 When I create a new object in Python, I want to be able to store a reference to that Object in my C++ application, so that I can later call methods on that object.
当我在Python中创建一个新对象时,我希望能够在我的C ++应用程序中存储对该Object的引用,以便稍后可以调用该对象上的方法。 What's the recommended way of doing this?
建议的方法是什么?
For Example, I would like to be able to do something like this : 例如,我希望能够做到这样的事情:
Entity.py Entity.py
class Entity:
def getPointer(self)
return pointertoSelf;
Manager.cpp Manager.cpp
Py_Initialize();
PyRun_SimpleString("import Entity");
PyRun_SimpleString("entity = Entity.Entity()");
pointerToPythonObj* = somehowGetPointerToObj("entity");
The recommended way is to query into the namespace in which the entity
object was created, then store a handle to the entity
object as a boost::python::object
. 建议的方法是查询创建
entity
对象的命名空间,然后将entity
对象的句柄存储为boost::python::object
。 When interacting with Python objects from C++, it is best to use boost::python::object
whenever possible, as it provides a high-level notation that acts much like a Python variable. 当与C ++中的Python对象进行交互时,最好尽可能使用
boost::python::object
,因为它提供了一个与Python变量非常相似的高级表示法。 Additionally, it provides the appropriate reference counting to manage the lifetime of the Python object. 此外,它还提供适当的引用计数来管理Python对象的生命周期。 For example, storing a raw pointer (ie
pointerToPythonObj*
) would not extend the life of the Python object; 例如,存储原始指针(即
pointerToPythonObj*
)不会延长Python对象的生命周期; if the Python object was garbage collected from within the interpreter, then pointerToPythonObj
would be a dangling pointer. 如果Python对象是从解释器中收集的垃圾,那么
pointerToPythonObj
将是一个悬空指针。
Here is an example demonstrating this: 这是一个证明这一点的例子:
Entity.py: Entity.py:
class Entity:
def action(self):
print "in Entity::action"
main.cpp: main.cpp中:
#include <boost/python.hpp>
int main()
{
namespace python = boost::python;
try
{
Py_Initialize(); // Start interpreter.
// Create the __main__ module.
python::object main = python::import("__main__");
python::object main_namespace = main.attr("__dict__");
// Import Entity.py, and instantiate an Entity object in the
// global namespace. PyRun_SimpleString could also be used,
// as it will default to running within and creating
// __main__'s namespace.
exec(
"import Entity\n"
"entity = Entity.Entity()\n"
, main_namespace
);
// Obtain a handle to the entity object created from the previous
// exec.
python::object entity = main_namespace["entity"];
// Invoke the action method on the entity.
entity.attr("action")();
}
catch (const python::error_already_set&)
{
PyErr_Print();
}
}
Running the above program results in the following output: 运行以上程序会产生以下输出:
in Entity::action
If Entity.py
is failing to be imported, then it may require adding its containing directory to the PYTHONPATH
environment variable. 如果无法导入
Entity.py
,则可能需要将其包含的目录添加到PYTHONPATH
环境变量中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.