how to pass pointer to boost.python function in python?

Dear all, I wraped a v_varray class into python by boost. I called it in python, most of its attributes work well. Only when I call a.push(1.0),it crashed due to not match type.The problem is that my input argument in c++ is float*(for passing an array) while python has no pointer type. How can I pass a variable in python to a.push() to make sure it work correctly? Many thanks for help! Javen /*************************running result in python:****************/
a.push(1.0) Traceback (most recent call last): File "<stdin>", line 1, in ? Boost.Python.ArgumentError: Python argument types in array_floatstar.push(array_floatstar, float) did not match C++ signature: push(v_array<float*> {lvalue}, float*)
/*************************v_array def in stack.h:****************/ #ifndef _STACK_H #define _STACK_H #include <stdlib.h> template<class T> class v_array{ public: int index; int length; T* elements; T last() { return elements[index-1];} void decr() { index--;} v_array() { index = 0; length=0; elements = NULL;} T& operator[](unsigned int i) { return elements[i]; } void push(const T &new_ele) { while(index >= length) { length = 2*length + 3; elements = (T *)realloc(elements,sizeof(T) * length); } this->elements[index++] = new_ele; } void alloc(int newLength) { elements = (T *)realloc(elements, sizeof(T) * newLength); length = newLength; } T pop() { if (index > 0) return this->elements[--index]; else return T(); } }; #endif /*************************stack.cpp for boost.python(bjam need it to build a stack.so file):****************/ // Boost Includes ============================================================== #include <boost/python.hpp> #include <boost/cstdint.hpp> // Includes ==================================================================== #include "stack.h" // Using ======================================================================= using namespace boost::python; // Declarations ================================================================ BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(float) // Module ====================================================================== BOOST_PYTHON_MODULE(stack) { class_< v_array<float*> >("array_floatstar", init< >()) .def(init< const v_array<float*>& >()) .def_readwrite("index", &v_array<float*>::index) .def_readwrite("length", &v_array<float*>::length) .def_readwrite("elements", &v_array<float*>::elements) .def("last", &v_array<float*>::last, return_value_policy< return_opaque_pointer >()) .def("decr", &v_array<float*>::decr) .def("push", &v_array<float*>::push) .def("alloc", &v_array<float*>::alloc) .def("pop", &v_array<float*>::pop, return_value_policy< return_opaque_pointer >()) ; } -- Qinfeng(Javen) Shi Research School of Information Sciences and Engineering Australian National University Locked Bag 8001 Canberra ACT 2601

"Qinfeng(Javen) Shi " <shiqinfeng@gmail.com> writes:
Dear all,
I wraped a v_varray class into python by boost. I called it in python, most of its attributes work well. Only when I call a.push(1.0),it crashed due to not match type.
For the record, that's not a crash.
The problem is that my input argument in c++ is float*(for passing an array) while python has no pointer type.
That's one way to look at it.
How can I pass a variable in python to a.push() to make sure it work correctly?
Depends what you mean by "work correctly." You need to decide what semantics you want. If you want your float to be treated as an array of one element, you could use a "thin wrapper": void push(v_array<float*>& self, float x) { self.push(&x); } class_<v_array<float*> >("array_floatstar") .def("push", push) ... ; Of course, any modifications push makes to the float at the other end of the non-const pointer will be lost. Oh, and future Boost.Python questions should probably be directed to the C++-sig: http://www.boost.org/more/mailing_lists.htm#cplussig HTH, -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (2)
-
David Abrahams
-
Qinfeng(Javen) Shi