| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="ext.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="ext.html" title='Extending and Embedding the Python Interpreter' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='last' href='about.html' title='About this document...' /> |
| 10 | <link rel='help' href='about.html' title='About this document...' /> |
| 11 | <link rel="next" href="buildValue.html" /> |
| 12 | <link rel="prev" href="parseTuple.html" /> |
| 13 | <link rel="parent" href="intro.html" /> |
| 14 | <link rel="next" href="buildValue.html" /> |
| 15 | <meta name='aesop' content='information' /> |
| 16 | <title>1.8 Keyword Parameters for Extension Functions |
| 17 | </title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="1.7 Extracting Parameters in" |
| 25 | href="parseTuple.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="1. Extending Python with" |
| 28 | href="intro.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="1.9 Building Arbitrary Values" |
| 31 | href="buildValue.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Extending and Embedding the Python Interpreter</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><img src='../icons/blank.png' |
| 38 | border='0' height='32' alt='' width='32' /></td> |
| 39 | <td class='online-navigation'><img src='../icons/blank.png' |
| 40 | border='0' height='32' alt='' width='32' /></td> |
| 41 | </tr></table> |
| 42 | <div class='online-navigation'> |
| 43 | <b class="navlabel">Previous:</b> |
| 44 | <a class="sectref" rel="prev" href="parseTuple.html">1.7 Extracting Parameters in</A> |
| 45 | <b class="navlabel">Up:</b> |
| 46 | <a class="sectref" rel="parent" href="intro.html">1. Extending Python with</A> |
| 47 | <b class="navlabel">Next:</b> |
| 48 | <a class="sectref" rel="next" href="buildValue.html">1.9 Building Arbitrary Values</A> |
| 49 | </div> |
| 50 | <hr /></div> |
| 51 | </DIV> |
| 52 | <!--End of Navigation Panel--> |
| 53 | |
| 54 | <H1><A NAME="SECTION003800000000000000000"></A><A NAME="parseTupleAndKeywords"></A><a id='l2h-3' xml:id='l2h-3'></a> |
| 55 | <BR> |
| 56 | 1.8 Keyword Parameters for Extension Functions |
| 57 | |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | <a id='l2h-4' xml:id='l2h-4'></a> |
| 62 | |
| 63 | <P> |
| 64 | The <tt class="cfunction">PyArg_ParseTupleAndKeywords()</tt> function is declared as |
| 65 | follows: |
| 66 | |
| 67 | <P> |
| 68 | <div class="verbatim"><pre> |
| 69 | int PyArg_ParseTupleAndKeywords(PyObject *arg, PyObject *kwdict, |
| 70 | char *format, char *kwlist[], ...); |
| 71 | </pre></div> |
| 72 | |
| 73 | <P> |
| 74 | The <var>arg</var> and <var>format</var> parameters are identical to those of the |
| 75 | <tt class="cfunction">PyArg_ParseTuple()</tt> function. The <var>kwdict</var> parameter |
| 76 | is the dictionary of keywords received as the third parameter from the |
| 77 | Python runtime. The <var>kwlist</var> parameter is a <tt class="constant">NULL</tt>-terminated |
| 78 | list of strings which identify the parameters; the names are matched |
| 79 | with the type information from <var>format</var> from left to right. On |
| 80 | success, <tt class="cfunction">PyArg_ParseTupleAndKeywords()</tt> returns true, |
| 81 | otherwise it returns false and raises an appropriate exception. |
| 82 | |
| 83 | <P> |
| 84 | <span class="note"><b class="label">Note:</b> |
| 85 | Nested tuples cannot be parsed when using keyword |
| 86 | arguments! Keyword parameters passed in which are not present in the |
| 87 | <var>kwlist</var> will cause <tt class="exception">TypeError</tt> to be raised.</span> |
| 88 | |
| 89 | <P> |
| 90 | Here is an example module which uses keywords, based on an example by |
| 91 | Geoff Philbrick (<span class="email">philbrick@hks.com</span>): |
| 92 | <P> |
| 93 | <div class="verbatim"><pre> |
| 94 | #include "Python.h" |
| 95 | |
| 96 | static PyObject * |
| 97 | keywdarg_parrot(PyObject *self, PyObject *args, PyObject *keywds) |
| 98 | { |
| 99 | int voltage; |
| 100 | char *state = "a stiff"; |
| 101 | char *action = "voom"; |
| 102 | char *type = "Norwegian Blue"; |
| 103 | |
| 104 | static char *kwlist[] = {"voltage", "state", "action", "type", NULL}; |
| 105 | |
| 106 | if (!PyArg_ParseTupleAndKeywords(args, keywds, "i|sss", kwlist, |
| 107 | &voltage, &state, &action, &type)) |
| 108 | return NULL; |
| 109 | |
| 110 | printf("-- This parrot wouldn't %s if you put %i Volts through it.\n", |
| 111 | action, voltage); |
| 112 | printf("-- Lovely plumage, the %s -- It's %s!\n", type, state); |
| 113 | |
| 114 | Py_INCREF(Py_None); |
| 115 | |
| 116 | return Py_None; |
| 117 | } |
| 118 | |
| 119 | static PyMethodDef keywdarg_methods[] = { |
| 120 | /* The cast of the function is necessary since PyCFunction values |
| 121 | * only take two PyObject* parameters, and keywdarg_parrot() takes |
| 122 | * three. |
| 123 | */ |
| 124 | {"parrot", (PyCFunction)keywdarg_parrot, METH_VARARGS | METH_KEYWORDS, |
| 125 | "Print a lovely skit to standard output."}, |
| 126 | {NULL, NULL, 0, NULL} /* sentinel */ |
| 127 | }; |
| 128 | </pre></div> |
| 129 | |
| 130 | <P> |
| 131 | <div class="verbatim"><pre> |
| 132 | void |
| 133 | initkeywdarg(void) |
| 134 | { |
| 135 | /* Create the module and add the functions */ |
| 136 | Py_InitModule("keywdarg", keywdarg_methods); |
| 137 | } |
| 138 | </pre></div> |
| 139 | |
| 140 | <P> |
| 141 | |
| 142 | <DIV CLASS="navigation"> |
| 143 | <div class='online-navigation'> |
| 144 | <p></p><hr /> |
| 145 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 146 | <tr> |
| 147 | <td class='online-navigation'><a rel="prev" title="1.7 Extracting Parameters in" |
| 148 | href="parseTuple.html"><img src='../icons/previous.png' |
| 149 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 150 | <td class='online-navigation'><a rel="parent" title="1. Extending Python with" |
| 151 | href="intro.html"><img src='../icons/up.png' |
| 152 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 153 | <td class='online-navigation'><a rel="next" title="1.9 Building Arbitrary Values" |
| 154 | href="buildValue.html"><img src='../icons/next.png' |
| 155 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 156 | <td align="center" width="100%">Extending and Embedding the Python Interpreter</td> |
| 157 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 158 | href="contents.html"><img src='../icons/contents.png' |
| 159 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 160 | <td class='online-navigation'><img src='../icons/blank.png' |
| 161 | border='0' height='32' alt='' width='32' /></td> |
| 162 | <td class='online-navigation'><img src='../icons/blank.png' |
| 163 | border='0' height='32' alt='' width='32' /></td> |
| 164 | </tr></table> |
| 165 | <div class='online-navigation'> |
| 166 | <b class="navlabel">Previous:</b> |
| 167 | <a class="sectref" rel="prev" href="parseTuple.html">1.7 Extracting Parameters in</A> |
| 168 | <b class="navlabel">Up:</b> |
| 169 | <a class="sectref" rel="parent" href="intro.html">1. Extending Python with</A> |
| 170 | <b class="navlabel">Next:</b> |
| 171 | <a class="sectref" rel="next" href="buildValue.html">1.9 Building Arbitrary Values</A> |
| 172 | </div> |
| 173 | </div> |
| 174 | <hr /> |
| 175 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 176 | </DIV> |
| 177 | <!--End of Navigation Panel--> |
| 178 | <ADDRESS> |
| 179 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 180 | </ADDRESS> |
| 181 | </BODY> |
| 182 | </HTML> |