O'Reilly logo

Python: Essential Reference, Third Edition by David Beazley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required