John Machin wrote: > [EMAIL PROTECTED] wrote: > > How does one transfer a buffer object from python -> c and back again > > (assuming the data gets modified)? > > I can't seem to get this or anything else to work, but am clueless as > > to what I'm doing wrong > > > > > > using namespace boost::python; > > Looks like C++, not C. > > > > > static PyObject * proc_buf(PyObject *self, PyObject *args) { > > [I'm not familiar with the boost gadget, but ...] Doesn't "static" mean > that this function is *not* externally visible
Yes. It's C++. I've built python extensions with Boost Python successfully and copied the structure of most of this example from other people's code. > > > PyObject *resultobj; > > char* output_samples; > > int len; > > if (!PyArg_ParseTuple(args,"s#|l",&output_samples, &len)) { > > You have made the length an optional argument, but not initialised the > receiving variable "len". Nothing to do with your current problem, but > highly dangerous. > > > return NULL; /* wrong arguments provided */ > > } > > for (int i=0;i<len;i++) { > > output_samples[i] *= 2; // for example > > } > > This is updating the internal representation of the input in situ. Not > a very good idea at all. Take a copy. Return the updated copy. Thanks for the pointers... > > > resultobj = PyString_FromStringAndSize(output_samples, len); > > return resultobj; > > } This is the part I need help with. I've also used PyBuffer_... subroutines which gave similar problems. Thanks for all of your other comments, but I was hoping someone could just tell me what was wrong with the code without having to worry about all of the other things that could go wrong. For completeness here is the complete c++ module & python output //======================================================================= // Boost Includes //============================================================== #include <boost/python.hpp> #include <boost/cstdint.hpp> #include <boost/python/def.hpp> #include <boost/python/args.hpp> #include <boost/python/overloads.hpp> // Using ======================================================================= using namespace boost::python; static PyObject * proc_buf(PyObject *self, PyObject *args) { PyObject *resultobj; char* output_samples; int len; if (!PyArg_ParseTuple(args,"s#|l",&output_samples, &len)) { return NULL; /* wrong arguments provided */ } for (int i=0;i<len;i++) output_samples[i] *= 2; resultobj = PyString_FromStringAndSize(output_samples, len); return resultobj; } // Module ====================================================================== BOOST_PYTHON_MODULE(spuctest) { def("pass_buf",&proc_buf); } # python code .... buffy = mf.read() print type(buffy) buf = pass_buf(buffy, len(buffy)) print type(buf) # #python output <type 'buffer'> <type 'Nonetype'> -- http://mail.python.org/mailman/listinfo/python-list