PyArg_BuildValue, how to return a PyString object?

Michael Hudson mwh at python.net
Tue Apr 2 03:44:36 EST 2002


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