PyArg_BuildValue, how to return a PyString object?

Kevin Cazabon kevin at cazabon.com
Tue Apr 2 10:07:43 EST 2002


Thanks for the help Michael, I'll give it a try.  I did do some
mucking about last night, and the null bytes in the strings I'm using
are causing all sorts of problems.

I think I might try passing in an array instead, and using the buffer
interface to the data, I'm just not sure yet how to pass the
manipulated data back to python as an array (or convert it to an array
in Python)... more reading to do I guess.

Kevin.

Michael Hudson <mwh at python.net> wrote in message news:<lkelhya3rg.fsf at elios.maths.bris.ac.uk>...
> kevin at cazabon.com (Kevin Cazabon) writes:
> 
> > I'm writing a C extension dll for Python, and am having a bit of
> > trouble figuring out how to return a PyString_FromStringAndSize object
> > (along with some normal stuff).
> > 
> > (I need to use PyString_FromStringAndSize because the string may
> > contain null bytes, am I correct in this, or is there an easier way?)
> 
> Doubt it.
> 
> > Can I use something like this (it doesn't seem to work like this
> > though):
> > 
> > PyObject *myPyStringObject = PyString_FromStringAndSize(cstringarray,
> > size);
> > PyObject *myReturnObject =  Py_BuildValue("llo", long1, long2,
> > myPyStringObject);
> 
> I think you have to mae that "(llO)", don't you?
> 
> There might be a s# format code, I can't rememeber -- Py_BuildValue
> *is* documented, though so you can check for yourself:
> 
> > Or do I have to build a tuple manually?
> 
> No.  Rememebr to DECREF "O" args after calling Py_BuildValue --
> something I'm always forgetting.
> 
> Cheers,
> M.



More information about the Python-list mailing list