473,394 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Boost Python properties/getter functions for strings

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
4 4220
I forgot to mention, getname is defined as:
const std::string &Entity::getName() const;

Mar 19 '07 #2
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
2
by: Lachlan Hunt | last post by:
Hi, In JavaScript 1.5, objects can use special getter and setter functions for properties. However, these only seem to be implemented in Gecko and, AFAICT, don't seem to be part of ECMAScript. ...
112
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them...
11
by: Kondapanaidu | last post by:
Hi, What is the difference between Properties and functions. Why dont we go for functions instead of properties. Regrads
122
by: Edward Diener No Spam | last post by:
The definition of a component model I use below is a class which allows properties, methods, and events in a structured way which can be recognized, usually through some form of introspection...
1
by: gabriel.becedillas | last post by:
I have a lot of functions returning "const std::string&". Every time I wrap one of those I have to do it like this: class_.def("name", &someclass::bla,...
122
by: C.L. | last post by:
I was looking for a function or method that would return the index to the first matching element in a list. Coming from a C++ STL background, I thought it might be called "find". My first stop was...
71
by: Jack | last post by:
I understand that the standard Python distribution is considered the C-Python. Howerver, the current C-Python is really a combination of C and Python implementation. There are about 2000 Python...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.