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>::operator()(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::objects::caller_py_function_impl<Caller>::operator()
-
(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.
4 4220
I forgot to mention, getname is defined as:
const std::string &Entity::getName() const;
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.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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.
...
|
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.
...
|
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...
|
by: Kondapanaidu |
last post by:
Hi,
What is the difference between Properties and functions.
Why dont we go for functions instead of properties.
Regrads
|
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...
|
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,...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |