Converting Data from C to Python

The following 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 (see Table B.3) is similar to that used with the PyArg_Parse* functions.

Table B.3. Format Specifiers for Py_BuildValue()
Format PyType C Type Description
"s" String char * Null-terminated string. If the C string pointer is NULL, None is returned.
"s#" String char *, int String and length. May contain null bytes. If the C string pointer is NULL, None is returned.

Get Python Essential Reference, Second 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.