By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

Boost Python properties/getter functions for strings

P: n/a
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.

Expand|Select|Wrap|Line Numbers
  1. boost::python::class_<Entity, std::auto_ptr<pyEntity("Entity")
  2. //publics
  3. .def("isActive", &Entity::isActive) //bool
  4. .def("activate", &Entity::activate) //bool
  5. .def("deactivate", &Entity::deactivate) //bool
  6. //...
  7. .add_property("name", &Entity::getName) //compile error (1)
  8. .def("getName", &Entity::getName,
  9. boost::python::return_internal_reference<>()); //runtime error(2)
  10.  
  11.  
  12. Compile error (1) shows this: C:/MinGW/include/boost/python/detail/
  13. invoke.hpp: In function `PyObject*
  14. boost::python::detail::invoke(boost::python::detail::invoke_tag_<
  15. false,  true>, const RC&, F&, TC&) [with RC =
  16. boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<const
  17. std::string&>, F = const std::string&(rsblsb::Entity::*)() const, TC =
  18. boost::python::arg_from_python<rsblsb::Entity&>]':
  19. C:/MinGW/include/boost/python/detail/caller.hpp:199:   instantiated
  20. from `PyObject* boost::python::detail::caller_arity<1u>::impl<F,
  21. Policies, Sig>::operator()(PyObject*, PyObject*) [with F = const
  22. std::string&(rsblsb::Entity::*)() const, Policies =
  23. boost::python::default_call_policies, Sig = boost::mpl::vector2<const
  24. std::string&, rsblsb::Entity&>]'
  25. C:/MinGW/include/boost/python/object/py_function.hpp:38:
  26. instantiated from `PyObject*
  27. boost::python::objects::caller_py_function_impl<Caller>::operator()
  28. (PyObject*, PyObject*) [with Caller =
  29. boost::python::detail::caller<const std::string&(rsblsb::Entity::*)()
  30. const, boost::python::default_call_policies, boost::mpl::vector2<const
  31. std::string&, rsblsb::Entity&]'
  32. C:\Game\svn\Platform\Framework\Python\PyModuleSetup.cc:58:
  33. instantiated from here
  34. C:/MinGW/include/boost/python/detail/invoke.hpp:88: error: no match
  35. for call to `(const
  36. boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<const
  37. std::string&>) (const std::basic_string<char, std::char_traits<char>,
  38. 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.
Mar 19 '07 #1
Share this Question
Share on Google+
4 Replies


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

Mar 19 '07 #2

P: n/a
On Mar 19, 12:00 pm, "Shawn McGrath" <shawn.mcgr...@gmail.comwrote:
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.

Mar 19 '07 #3

P: n/a
On 19 Mar, 16:40, "Shawn McGrath" <shawn.mcgr...@gmail.comwrote:
On Mar 19, 12:00 pm, "Shawn McGrath" <shawn.mcgr...@gmail.comwrote:
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?
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.
Mar 19 '07 #4

P: n/a
On Mar 19, 12:49 pm, "Jon Clements" <jon...@googlemail.comwrote:
On 19 Mar, 16:40, "Shawn McGrath" <shawn.mcgr...@gmail.comwrote:
On Mar 19, 12:00 pm, "Shawn McGrath" <shawn.mcgr...@gmail.comwrote:
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?

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.

Mar 19 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.