Name
Py_BuildValue
Synopsis
PyObject* Py_BuildValue(char* format,...)
format is a C string that describes the
Python object to build. The following arguments of
Py_BuildValue are C values from which the result
is built. The PyObject* result is a new reference.
Table 24-2 lists the commonly used code strings, of
which zero or more are joined into string
format. Py_BuildValue
builds and returns a tuple if format
contains two or more format codes, or if
format begins with (
and ends with ). Otherwise, the result is not a
tuple. When you pass buffers, as for example in the case of format
code s#, Py_BuildValue copies
the data. You can therefore modify, abandon, or free( ) your original copy of the data after
Py_BuildValue returns.
Py_BuildValue always returns a new reference
(except for format code N). Called with an empty
format,
Py_BuildValue("") returns a new reference to
None.
Table 24-2. Format codes for Py_BuildValue
|
Code |
C type |
Meaning |
|---|---|---|
c |
char |
A C |
d |
double |
A C |
D |
Py_Complex |
A C |
i |
int |
A C |
l |
long |
A C |
N |
PyObject* |
Passes a Python object and steals a reference |
O |
PyObject* |
Passes a Python object and |
O& |
convert + void* |
Arbitrary conversion (see below) |
s |
char* |
C null-terminated |
s# |
char* + int |
C |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access