@@ -356,14 +356,17 @@ \section{The Module's Method Table and Initialization Function}
356356\samp {METH_VARARGS | METH_KEYWORDS}; a value of \samp {0} means that an
357357obsolete variant of \code {PyArg_ParseTuple()} is used.
358358
359+ When using only \samp {METH_VARARGS}, the function should expect
360+ the Python-level parameters to be passed in as a tuple acceptable for
361+ parsing via \cfunction {PyArg_ParseTuple()}; more information on this
362+ function is provided below.
363+
359364The \code {METH_KEYWORDS} bit may be set in the third field if keyword
360365arguments should be passed to the function. In this case, the \C {}
361366function should accept a third \samp {PyObject *} parameter which will
362367be a dictionary of keywords. Use \code {PyArg_ParseTupleAndKeywords()}
363368to parse the arguemts to such a function.
364369
365- XXX --- need to explain PyArg_ParseTupleAndKeywords() in detail.
366-
367370The method table must be passed to the interpreter in the module's
368371initialization function (which should be the only non-\code {static}
369372item defined in the module file):
@@ -621,6 +624,9 @@ \section{Format Strings for \sectcode{PyArg_ParseTuple()}}
621624\item [\samp {d} (float) {[double]}]
622625Convert a Python floating point number to a \C {} \code {double}.
623626
627+ \item [\samp {D} (complex) {[Py_complex]}]
628+ Convert a Python complex number to a \C {} \code {Py_complex} structure.
629+
624630\item [\samp {O} (object) {[PyObject *]}]
625631Store a Python object (without any conversion) in a \C {} object pointer.
626632The \C {} program thus receives the actual object that was passed. The
@@ -736,8 +742,85 @@ \section{Format Strings for \sectcode{PyArg_ParseTuple()}}
736742 /* Possible Python call:
737743 f(((0, 0), (400, 300)), (10, 10)) */
738744 }
745+
746+ {
747+ Py_complex c;
748+ ok = PyArg_ParseTuple(args, "D:myfunction", &c);
749+ /* a complex, also providing a function name for errors */
750+ /* Possible Python call: myfunction(1+2j) */
751+ }
739752\end {verbatim }
740- %
753+
754+
755+ \section {Keyword Parsing with \sectcode {PyArg_ParseTupleAndKeywords()} }
756+
757+ The \cfunction {PyArg_ParseTupleAndKeywords()} function is declared as
758+ follows:
759+
760+ \bcode \begin {verbatim }
761+ int PyArg_ParseTupleAndKeywords(PyObject *arg, PyObject *kwdict,
762+ char *format, char **kwlist, ...);
763+ \end {verbatim }\ecode
764+
765+ The \var {arg} and \var {format} parameters are identical to those of the
766+ \cfunction {PyArg_ParseTuple()} function. The \var {kwdict} parameter
767+ is the dictionary of keywords received as the third parameter from the
768+ Python runtime. The \var {kwlist} parameter is a \NULL {}-terminated
769+ list of strings which identify the parameters; the names are matched
770+ with the type information from \var {format} from left to right.
771+
772+ \strong {Note:} Nested tuples cannot be parsed when using keyword
773+ arguments! Keyword parameters passed in which are not present in the
774+ \var {kwlist} will cause a \exception {TypeError} to be raised.
775+
776+ Here is an example module which uses keywords, based on an example by
777+ Geoff Philbrick (
\email {
[email protected] }):
778+
779+ \begin {verbatim }
780+ #include <stdio.h>
781+ #include "Python.h"
782+
783+ static PyObject *
784+ keywdarg_parrot(self, args, keywds)
785+ PyObject *self;
786+ PyObject *args;
787+ PyObject *keywds;
788+ {
789+ int voltage;
790+ char *state = "a stiff";
791+ char *action = "voom";
792+ char *type = "Norwegian Blue";
793+
794+ static char *kwlist[] = {"voltage", "state", "action", "type", NULL};
795+
796+ if (!PyArg_ParseTupleAndKeywords(args, keywds, "i|sss", kwlist,
797+ &voltage, &state, &action, &type))
798+ return NULL;
799+
800+ printf("-- This parrot wouldn't %s if you put %i Volts through it.\n",
801+ action, voltage);
802+ printf("-- Lovely plumage, the %s -- It's %s!\n", type, state);
803+
804+ Py_INCREF(Py_None);
805+
806+ return Py_None;
807+ }
808+
809+ static PyMethodDef keywdarg_methods[] = {
810+ {"parrot", (PyCFunction)keywdarg_parrot, METH_VARARGS|METH_KEYWORDS},
811+ {NULL, NULL} /* sentinel */
812+ };
813+
814+ void
815+ initkeywdarg()
816+ {
817+ /* Create the module and add the functions */
818+ Py_InitModule("keywdarg", keywdarg_methods);
819+
820+ }
821+ \end {verbatim }
822+
823+
741824\section {The \sectcode {Py_BuildValue()} Function }
742825
743826This function is the counterpart to \code {PyArg_ParseTuple()}. It is
0 commit comments