We've (partially) covered turning Python objects into C objects. What
about the reverse? A function that returns to Python must return a
pointer to a
PyObject, not a raw C long or double.
Fortunately, converting simple C types such as integers and strings to
the corresponding Python types is pretty easy: as you can see from the
PyInt_FromLong does the former job and the
PyString_FromString does the latter (for
NULL-terminated strings anyway; being aware of the
PyString_FromStringAndSize function is useful for when that's
not what you want).
For more complex situations, Python provides the function
PyAPI_FUNC(PyObject *) Py_BuildValue(char *format, ...);
The string format is similar to that passed to
PyArg_ParseTuple, with the difference that the following
arguments are not pointers to variables of appropriate types, but
values of these types.
The type of object returned by
Py_BuildValue depends on the
number of ``format units''...
XXX need an example of using Py_BuildValue.
XXX describe Py_BuildValue, PyList_*, etc here? Mention, at least?
THIS DOCUMENT IS A DRAFT! Comments to email@example.com please.