Boost Python properties/getter functions for strings

S

Shawn McGrath

Hi, I'm trying to expose a C++ class' internals to python via
boost::python. I can do integer/boolean functions fine, but as soon
as I do a string get/set it craps out.

boost::python::class_<Entity, std::auto_ptr<pyEntity> >("Entity")
//publics
.def("isActive", &Entity::isActive) //bool
.def("activate", &Entity::activate) //bool
.def("deactivate", &Entity::deactivate) //bool
//...
.add_property("name", &Entity::getName) //compile error (1)
.def("getName", &Entity::getName,
boost::python::return_internal_reference<>()); //runtime error(2)


Compile error (1) shows this: C:/MinGW/include/boost/python/detail/
invoke.hpp: In function `PyObject*
boost::python::detail::invoke(boost::python::detail::invoke_tag_<
false, true>, const RC&, F&, TC&) [with RC =
boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<const
std::string&>, F = const std::string&(rsblsb::Entity::*)() const, TC =
boost::python::arg_from_python<rsblsb::Entity&>]':
C:/MinGW/include/boost/python/detail/caller.hpp:199: instantiated
from `PyObject* boost::python::detail::caller_arity<1u>::impl<F,
Policies, Sig>::eek:perator()(PyObject*, PyObject*) [with F = const
std::string&(rsblsb::Entity::*)() const, Policies =
boost::python::default_call_policies, Sig = boost::mpl::vector2<const
std::string&, rsblsb::Entity&>]'
C:/MinGW/include/boost/python/object/py_function.hpp:38:
instantiated from `PyObject*
boost::python::eek:bjects::caller_py_function_impl<Caller>::eek:perator()
(PyObject*, PyObject*) [with Caller =
boost::python::detail::caller<const std::string&(rsblsb::Entity::*)()
const, boost::python::default_call_policies, boost::mpl::vector2<const
std::string&, rsblsb::Entity&> >]'
C:\Game\svn\Platform\Framework\Python\PyModuleSetup.cc:58:
instantiated from here
C:/MinGW/include/boost/python/detail/invoke.hpp:88: error: no match
for call to `(const
boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<const
std::string&>) (const std::basic_string<char, std::char_traits<char>,
std::allocator<char> >&)'

Runtime error 2 just crashes whenever I try:
import modulename
I = modulename.Entity()
I.getName()

Anyone have any idea what I can try? thanks a lot!

-Shawn.
 
S

Shawn McGrath

I forgot to mention, getname is defined as:
const std::string &Entity::getName() const;
 
S

Shawn McGrath

I forgot to mention, getname is defined as:
const std::string &Entity::getName() const;

After more reading I found the copy_const_reference, and replaced:
boost::python::return_internal_reference<>());
with:

boost::python::return_value_policy<boost::python::copy_const_reference>());

and it fixed my problem. Is there any downside to using
copy_const_reference over return_internal_reference?

Thanks,
Shawn.
 
J

Jon Clements

After more reading I found the copy_const_reference, and replaced:
boost::python::return_internal_reference<>());
with:

boost::python::return_value_policy<boost::python::copy_const_reference>());

and it fixed my problem. Is there any downside to using
copy_const_reference over return_internal_reference?

You might get some answers here; if not, can I suggest
http://mail.python.org/mailman/listinfo/c++-sig ? I think a lot of the
Boost.Python developers hang around on that list.


hth,

Jon.
 
S

Shawn McGrath

You might get some answers here; if not, can I suggesthttp://mail.python.org/mailman/listinfo/c++-sig? I think a lot of the
Boost.Python developers hang around on that list.

hth,

Jon.

Cool thanks a lot.

The problem is actually due to python's strings being immutable (I
knew this, but I thought returning const std::string& would do it).
return_internal_reference<> works for other pointers/references, just
not strings.

(I just answered it so if it gets searched later on people will find
the solution)

-Shawn.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top