()) // ... .def("-6ren">
gpt4 book ai didi

c++ - boost .python : Argument types did not match C++ signature

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:24:52 24 4
gpt4 key购买 nike

我在 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)

据我所知,签名确实匹配。有人看到问题了吗?

最佳答案

在此 ticket 中跟踪, Boost.Python 不完全支持 std::shared_ptr .

简而言之,两个简单的解决方案是:


虽然异常中的签名看起来相同,但微妙的细节是 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()

关于c++ - boost .python : Argument types did not match C++ signature,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20825662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com