Name

PyArg_ParseTuple

Synopsis

int PyArg_ParseTuple(PyObject* tuple,char* format,...)

Returns 0 for errors, a value not equal to 0 for success. tuple is the PyObject* that was the C function’s second argument. format is a C string that describes mandatory and optional arguments. The following arguments of PyArg_ParseTuple are the addresses of the C variables in which to put the values extracted from the tuple. Any PyObject* variables among the C variables are borrowed references. Table 24-1 lists the commonly used code strings, of which zero or more are joined to form string format.

Table 24-1. Format codes for PyArg_ParseTuple

Code

C type

Meaning

c
char

A Python string of length 1 becomes a C char

d
double

A Python float becomes a C double

D
Py_Complex

A Python complex becomes a C Py_Complex

f
float

A Python float becomes a C float

i
int

A Python int becomes a C int

l
long

A Python int becomes a C long

L
long long

A Python int becomes a C long long (or _int64 on Windows)

O
PyObject*

Gets non-NULL borrowed reference to a Python argument

O!
type + PyObject*

Like code O, plus type checking or TypeError (see below)

O&
convert + void*

Arbitrary conversion (see below)

s
char*

Python string without embedded nulls to C char*

s#
char* + int

Any Python string to C address and length

t#
char* + int

Read-only single-segment buffer to C address and length

u
Py_UNICODE*

Python Unicode without embedded nulls to C (UTF-16)

u#
Py_UNICODE* + int

Get Python in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.