The PyArg_ParseTuple()
function is declared as follows:
int PyArg_ParseTuple(PyObject *arg, char *format, ...);
The arg argument must be a tuple object containing an argument list passed from Python to a C function. The format argument must be a format string, whose syntax is explained below. The remaining arguments must be addresses of variables whose type is determined by the format string. For the conversion to succeed, the arg object must match the format and the format must be exhausted.
Note that while PyArg_ParseTuple()
checks that the Python
arguments have the required types, it cannot check the validity of the
addresses of C variables passed to the call: if you make mistakes
there, your code will probably crash or at least overwrite random bits
in memory. So be careful!
A format string consists of zero or more ``format units''. A format
unit describes one Python object; it is usually a single character or
a parenthesized sequence of format units. With a few exceptions, a
format unit that is not a parenthesized sequence normally corresponds
to a single address argument to PyArg_ParseTuple()
. In the
following description, the quoted form is the format unit; the entry
in (round) parentheses is the Python object type that matches the
format unit; and the entry in [square] brackets is the type of the C
variable(s) whose address should be passed. (Use the `&'
operator to pass a variable's address.)
TypeError
exception is raised.
's'
stores into two C variables, the first one
a pointer to a character string, the second one its length. In this
case the Python string may contain embedded null bytes.
None
) [char *]
None
, in which
case the C pointer is set to NULL
.
None
) [char *, int]
's#'
as 'z'
is to 's'
.
char
.
short int
.
int
.
long int
.
char
.
float
.
double
.
NULL
.
PyObject *
) into which the object pointer is stored.
If the Python object does not have the required type, a
TypeError
exception is raised.
void *
. The converter function in turn is called as
follows:
status = converter(object, address);
where object is the Python object to be converted and
address is the void *
argument that was passed to
PyArg_ConvertTuple()
. The returned status should be
1
for a successful conversion and 0
if the conversion
has failed. When the conversion fails, the converter function
should raise an exception.
TypeError
exception that the object
is a string object. The C variable may also be declared as
PyObject *
.
It is possible to pass Python long integers where integers are requested; however no proper range checking is done -- the most significant bits are silently truncated when the receiving field is too small to receive the value (actually, the semantics are inherited from downcasts in C --- your milage may vary).
A few other characters have a meaning in a format string. These may not occur inside nested parentheses. They are:
PyArg_ParseTuple
does not touch the contents
of the corresponding C variable(s).
PyArg_ParseTuple
raises).
Some example calls:
int ok; int i, j; long k, l; char *s; int size; ok = PyArg_ParseTuple(args, ""); /* No arguments */ /* Python call: f() */ ok = PyArg_ParseTuple(args, "s", &s); /* A string */ /* Possible Python call: f('whoops!') */ ok = PyArg_ParseTuple(args, "lls", &k, &l, &s); /* Two longs and a string */ /* Possible Python call: f(1, 2, 'three') */ ok = PyArg_ParseTuple(args, "(ii)s#", &i, &j, &s, &size); /* A pair of ints and a string, whose size is also returned */ /* Possible Python call: f(1, 2, 'three') */ { char *file; char *mode = "r"; int bufsize = 0; ok = PyArg_ParseTuple(args, "s|si", &file, &mode, &bufsize); /* A string, and optionally another string and an integer */ /* Possible Python calls: f('spam') f('spam', 'w') f('spam', 'wb', 100000) */ } { int left, top, right, bottom, h, v; ok = PyArg_ParseTuple(args, "((ii)(ii))(ii)", &left, &top, &right, &bottom, &h, &v); /* A rectangle and a point */ /* Possible Python call: f(((0, 0), (400, 300)), (10, 10)) */ }