在python中调用C ++函数时遇到一个奇怪的问题。
我公开了一个我想要调用函数的类:
class_<MyClass, std::shared_ptr<MyClass>>("MyClass", init<>())
// ...
.def("someFunc", &MyClass::someFunc)
;
我从另一个通过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)
据我所知,签名执行匹配。 有人看到了问题吗?
答案 0 :(得分:6)
正如此ticket中所追踪的那样,Boost.Python并不完全支持std::shared_ptr
。
简而言之,两个简单的解决方案是:
boost::shared_ptr
代替std::shared_ptr
。std::shared_ptr
公开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目前不支持custom lvalue conversions。因此,抛出ArgumentError
异常。
使用def_readonly("spam", &Factory::spam)
公开成员变量时,它相当于通过以下方式公开:
add_property("spam", make_getter(&Factory::spam, return_internal_reference()))
当以这种方式公开的类型是boost::shared_ptr
时,Boost.Python有特殊代码。由于它是一个只读属性,并且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)
;
}
交互式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()
答案 1 :(得分:0)
尚未测试过,但这可能有效:
boost::python::register_ptr_to_python<std::shared_ptr<MyClass>>();
来源: http://www.boost.org/doc/libs/1_55_0/libs/python/doc/v2/register_ptr_to_python.html