Converting Data from C to Python

The following C function is used to convert the values contained in C variables to a Python object:

						PyObject *Py_BuildValue(char
						*format,
						...)

This constructs a Python object from a series of C variables. format is a string describing the desired conversion. The remaining arguments are the values of C variables to be converted.

The format specifier is similar to that used with the PyArg_ParseTuple* functions, as shown in Table 27.3.

Table 27.3. Format Specifiers for Py_BuildValue()
FormatPython TypeC TypeDescription
“s”Stringchar *Null-terminated string. If the C string pointer is NULL,None is returned.
“s#”Stringchar *, intString and length. May contain null bytes. If the C string pointer is NULL, None is returned. ...

Get Python: Essential Reference, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.