mar*_*ipf 5 c++ python boost boost-python c++11
在python中调用C++函数时遇到一个奇怪的问题.
我公开了一个我想要调用函数的类:
class_<MyClass, std::shared_ptr<MyClass>>("MyClass", init<>())
// ...
.def("someFunc", &MyClass::someFunc)
;
Run Code Online (Sandbox Code Playgroud)
我std::shared_ptr<MyClass>从另一个通过公开的类中获取一个成员变量.def_readonly(...)
当我尝试调用该函数时,我收到以下错误:
File "pytest.py", line 27, in test_func
cu.someFunc("string")
Boost.Python.ArgumentError: Python argument types in
MyClass.someFunc(MyClass, str)
did not match C++ signature:
result(MyClass{lvalue}, std::string)
Run Code Online (Sandbox Code Playgroud)
据我所知,签名确实匹配.有人看到了这个问题吗?
正如在这张票中所追踪的那样,Boost.Python并不完全支持std::shared_ptr.
简而言之,两个简单的解决方案是:
boost::shared_ptr而不是std::shared_ptr.std::shared_ptr用via 公开成员变量add_property(),提供boost::python::return_value_policy一个类型的boost::python::return_by_value.虽然异常中的签名看起来相同,但细微的细节是Python MyClass对象嵌入了std::shared_ptr<MyClass>.因此,Boost.Python必须执行std::shared_ptr<MyClass>从左值到左值的转换MyClass.但是,Boost.Python目前不支持自定义左值转换.因此,ArgumentError抛出异常.
使用公开成员变量时def_readonly("spam", &Factory::spam),它相当于通过以下方式公开它:
add_property("spam", make_getter(&Factory::spam, return_internal_reference()))
Run Code Online (Sandbox Code Playgroud)
当以这种方式公开的类型是a时,Boost.Python有特殊代码boost::shared_ptr.由于它是一个只读属性并且std::shared_ptr是要复制的,因此可以安全地公开std::shared_ptr带有类型为的返回值策略的副本return_by_value.
这是一个完整的示例,其中Factory公开由以下Spam对象持有std::shared_ptr的Egg对象boost::shared_ptr:
#include <iostream>
#include <memory> // std::shared_ptr, std::make_shared
#include <string>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/python.hpp>
/// @brief Mockup Spam type.
struct Spam
{
~Spam() { std::cout << "~Spam()" << std::endl; }
void someFunc(std::string str)
{
std::cout << "Spam::someFunc() " << this << " : " << str << std::endl;
}
};
/// @brief Mockup Egg type.
struct Egg
{
~Egg() { std::cout << "~Egg()" << std::endl; }
void someFunc(std::string str)
{
std::cout << "Egg::someFunc() " << this << " : " << str << std::endl;
}
};
/// @brief Mockup Factory type.
struct Factory
{
Factory()
: spam(std::make_shared<Spam>()),
egg(boost::make_shared<Egg>())
{
spam->someFunc("factory");
egg->someFunc("factory");
}
std::shared_ptr<Spam> spam;
boost::shared_ptr<Egg> egg;
};
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
// Expose Factory class and its member variables.
python::class_<Factory>("Factory")
// std::shared_ptr<Spam>
.add_property("spam", python::make_getter(&Factory::spam,
python::return_value_policy<python::return_by_value>()))
// boost::shared_ptr<Egg>
.def_readonly("egg", &Factory::egg)
;
// Expose Spam as being held by std::shared_ptr.
python::class_<Spam, std::shared_ptr<Spam>>("Spam")
.def("someFunc", &Spam::someFunc)
;
// Expose Egg as being held by boost::shared_ptr.
python::class_<Egg, boost::shared_ptr<Egg>>("Egg")
.def("someFunc", &Egg::someFunc)
;
}
Run Code Online (Sandbox Code Playgroud)
交互式Python演示使用和对象生存期:
>>> import example
>>> factory = example.Factory()
Spam::someFunc() 0x8d73250 : factory
Egg::someFunc() 0x8d5dbc9 : factory
>>> factory.spam.someFunc("python")
Spam::someFunc() 0x8d73250 : python
>>> factory.egg.someFunc("python")
Egg::someFunc() 0x8d5dbc9 : python
>>> factory = None
~Egg()
~Spam()
>>> factory = example.Factory()
Spam::someFunc() 0x8d73250 : factory
Egg::someFunc() 0x8d06569 : factory
>>> spam = factory.spam
>>> factory = None
~Egg()
>>> spam.someFunc("python")
Spam::someFunc() 0x8d73250 : python
>>> spam = None
~Spam()
>>> factory = example.Factory()
Spam::someFunc() 0x8d73250 : factory
Egg::someFunc() 0x8ce10f9 : factory
>>> egg = factory.egg
>>> factory = None
~Spam()
>>> egg.someFunc("python")
Egg::someFunc() 0x8ce10f9 : python
>>> egg = None
~Egg()
Run Code Online (Sandbox Code Playgroud)