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') */ 

{ const char *file; const 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)) */ } 

{ Py_complex c; ok =PyArg_ParseTuple(args, "D:myfunction ",&c); /* a complex, also providing a function name for errors */ /* Possible Python call: myfunction(1+2j) */ } 

## 2.1.8 Keyword Parameters for Extension Functions 

The$_{PyArg_ParseTupleAndKeywords()}$ function is declared as follows: 

int PyArg_ParseTupleAndKeywords(PyObject*arg, PyObject*kwdict, const char *format, char *kwlist[], ...); 

The arg and format parameters are identical to those of the$_{PyArg_ParseTuple()}$ function. The kwdict param- eter is the dictionary of keywords received as the third parameter from the Python runtime. The kwlist parameter is a$_{NULL}$ -terminated list of strings which identify the parameters; the names are matched with the type information from format from left to right. On success, PyArg_ParseTupleAndKeywords() returns true, otherwise it returns false and raises an appropriate exception. 

Note: Nested tuples cannot be parsed when using keyword arguments! Keyword parameters passed in which are not present in the kwlist will cause$_{TypeError}$ to be raised. 

Here is an example module which uses keywords, based on an example by Geoff Philbrick (philbrick@hks.com): 

#define PY_SSIZE_T_CLEAN /* Make "s#" use Py_ssize_t rather than int. */ #include<Python.h> static PyObject * keywdarg_parrot(PyObject *self, PyObject*args, PyObject*keywds) { int voltage; 

(continues on next page) 

