boost/python: how to: call memberfunctions of an existing c++ object from python?

Hi Boost/Python developers, at first I have to say that I am no boost nor a python expert. I've searched for hours now on the web for my problem but was unable to find some solution for my problem: Currently I am working on the possibility that an python-script is called by an existing c++ object. This was "easy" to do with and without the boost python-classes. To make this more complicate, the script must be able to call some member functions of this object. So to make this possible I created an BOOST_PYTHON_MODULE() containing the class and the to be called member function-definitions. Now it is possible to create new instances of the class and call them from python. It is also possible to do the following in my c++ object-code: "ret = boost::python::call<int>(PyFunction.ptr(), this );" But now my problem: boost copies the object and passes only that copy to the function. Is it and how is it possible to make the object accessible to the function within the python-script? I have found some hints in the boost-source for using smartptr but I was unable to use them as an parameter for call(): "TypeError: No to_python (by-value) converter found for C++ type: class boost::shared_ptr<TYPE>". I am also don't known if this is the right way to do. Thanks for help! Marco Borm

Marco Borm <mb.ml.boost@adisoft-systems.de> writes:
It is also possible to do the following in my c++ object-code: "ret = boost::python::call<int>(PyFunction.ptr(), this );"
But not so elegant or safe as: ret = boost::python::extract<int>( PyFunction( *this ) );
But now my problem: boost copies the object and passes only that copy to the function. Is it and how is it possible to make the object accessible to the function within the python-script?
ret = boost::python::extract<int>( PyFunction( boost::ref(*this) ) ); -- Dave Abrahams Boost Consulting www.boost-consulting.com

Marco Borm <mb.ml.boost@adisoft-systems.de> writes:
It is also possible to do the following in my c++ object-code: "ret = boost::python::call<int>(PyFunction.ptr(), this );"
But not so elegant or safe as: ret = boost::python::extract<int>( PyFunction( *this ) );
David Abrahams wrote: thats true :-)
But now my problem: boost copies the object and passes only that copy to the function. Is it and how is it possible to make the object accessible to the function within the python-script?
ret = boost::python::extract<int>( PyFunction( boost::ref(*this) ) ); To clarify: "this" isn't an instance of an boost/python object. The Instance is created somewhere else without boost/python. Because this, the code you gave me crashs in wrapper_base.hpp on an "dynamic_cast<wrapper_base const volatile*>(x)"
An example for what I want: class CppClass { public: void foo(); void bar(); .... }; BOOST_PYTHON_MODULE(test) { boost::python::class_<CppClass>("CppClass", boost::python::no_init) .def("bar", &CppClass::bar) ... ; } void CppClass::foo() { .... // call "foohelper" in python-script ret = boost::python::extract<int>( PyFunction( boost::ref(*this) ) ); .... } // python script def foohelper(cppobj): cppobj.bar() return 1234; main() { CppClass* object = new CppClass; object->foo(); } Thanks for help! Marco Borm

Marco Borm <mb.ml.boost@adisoft-systems.de> writes:
Marco Borm <mb.ml.boost@adisoft-systems.de> writes:
It is also possible to do the following in my c++ object-code: "ret = boost::python::call<int>(PyFunction.ptr(), this );" But not so elegant or safe as: ret = boost::python::extract<int>( PyFunction( *this ) );
David Abrahams wrote: thats true :-)
But now my problem: boost copies the object and passes only that copy to the function. Is it and how is it possible to make the object accessible to the function within the python-script? ret = boost::python::extract<int>( PyFunction( boost::ref(*this) ) ); To clarify: "this" isn't an instance of an boost/python object. The Instance is created somewhere else without boost/python.
Right, I expected that. The code I gave you is designed to account for that.
Because this, the code you gave me crashs in wrapper_base.hpp on an "dynamic_cast<wrapper_base const volatile*>(x)"
I'm pretty sure that is not why your code is crashing.
An example for what I want:
If you post an actual compilable example that reproduces the problem to the C++-sig (http://www.boost.org/more/mailing_lists.htm#cplussig) I might find time to debug it. -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (2)
-
David Abrahams
-
Marco Borm