I am having a strange problem when calling a C++ function in python.
I exposed a class from which I want to call a function:
class_<MyClass, std::shared_ptr<MyClass>>("MyClass", init<>())
// ...
.def("someFunc", &MyClass::someFunc)
;
I get a std::shared_ptr<MyClass>
from a member variable from another class which is exposed via .def_readonly(...)
When I try to call the function, I get the following error:
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)
As far as I see, the signatures do match.
Does someone see the problem?
As tracked in this ticket, Boost.Python does not fully support std::shared_ptr
.
In short, two easy solutions are to either:
- Use
boost::shared_ptr
instead of std::shared_ptr
.
- Expose the
std::shared_ptr
member variable with via add_property()
, providing a boost::python::return_value_policy
with a type of boost::python::return_by_value
.
While the signatures in the exception look the same, the subtle detail is that the Python MyClass
object embeds a std::shared_ptr<MyClass>
. Thus, Boost.Python must perform a conversion from an std::shared_ptr<MyClass>
to an lvalue MyClass
. However, Boost.Python does not currently support custom lvalue conversions. Thus, an ArgumentError
exception is thrown.
When exposing member variables with def_readonly("spam", &Factory::spam)
, it is the equivalent of exposing it via:
add_property("spam", make_getter(&Factory::spam, return_internal_reference()))
Boost.Python has special code when the type being exposed in this manner is a boost::shared_ptr
. As it is a read-only property and std::shared_ptr
is intended to be copied, it is safe to expose a copy of std::shared_ptr
with a return value policy with a type of return_by_value
.
Here is a complete example where Factory
exposes a Spam
object held by std::shared_ptr
and an Egg
object held by 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)
;
}
Interactive Python demonstrating usage and object lifetime:
>>> 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()
Haven't tested it, but this may work:
boost::python::register_ptr_to_python<std::shared_ptr<MyClass>>();
Source:
http://www.boost.org/doc/libs/1_55_0/libs/python/doc/v2/register_ptr_to_python.html