Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1 | ====================== |
| 2 | Argument Clinic How-To |
| 3 | ====================== |
| 4 | |
| 5 | :author: Larry Hastings |
| 6 | |
| 7 | |
| 8 | .. topic:: Abstract |
| 9 | |
| 10 | Argument Clinic is a preprocessor for CPython C files. |
| 11 | Its purpose is to automate all the boilerplate involved |
| 12 | with writing argument parsing code for "builtins". |
| 13 | This document shows you how to convert your first C |
| 14 | function to work with Argument Clinic, and then introduces |
| 15 | some advanced topics on Argument Clinic usage. |
| 16 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 17 | Currently Argument Clinic is considered internal-only |
| 18 | for CPython. Its use is not supported for files outside |
| 19 | CPython, and no guarantees are made regarding backwards |
| 20 | compatibility for future versions. In other words: if you |
| 21 | maintain an external C extension for CPython, you're welcome |
| 22 | to experiment with Argument Clinic in your own code. But the |
| 23 | version of Argument Clinic that ships with CPython 3.5 *could* |
| 24 | be totally incompatible and break all your code. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 25 | |
| 26 | ======================== |
| 27 | Basic Concepts And Usage |
| 28 | ======================== |
| 29 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 30 | Argument Clinic ships with CPython; you'll find it in ``Tools/clinic/clinic.py``. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 31 | If you run that script, specifying a C file as an argument:: |
| 32 | |
| 33 | % python3 Tools/clinic/clinic.py foo.c |
| 34 | |
| 35 | Argument Clinic will scan over the file looking for lines that |
| 36 | look exactly like this:: |
| 37 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 38 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 39 | |
| 40 | When it finds one, it reads everything up to a line that looks |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 41 | exactly like this:: |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 42 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 43 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 44 | |
| 45 | Everything in between these two lines is input for Argument Clinic. |
| 46 | All of these lines, including the beginning and ending comment |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 47 | lines, are collectively called an Argument Clinic "block". |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 48 | |
| 49 | When Argument Clinic parses one of these blocks, it |
| 50 | generates output. This output is rewritten into the C file |
| 51 | immediately after the block, followed by a comment containing a checksum. |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 52 | The Argument Clinic block now looks like this:: |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 53 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 54 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 55 | ... clinic input goes here ... |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 56 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 57 | ... clinic output goes here ... |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 58 | /*[clinic end generated code: checksum=...]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 59 | |
| 60 | If you run Argument Clinic on the same file a second time, Argument Clinic |
| 61 | will discard the old output and write out the new output with a fresh checksum |
| 62 | line. However, if the input hasn't changed, the output won't change either. |
| 63 | |
| 64 | You should never modify the output portion of an Argument Clinic block. Instead, |
| 65 | change the input until it produces the output you want. (That's the purpose of the |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 66 | checksum--to detect if someone changed the output, as these edits would be lost |
| 67 | the next time Argument Clinic writes out fresh output.) |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 68 | |
| 69 | For the sake of clarity, here's the terminology we'll use with Argument Clinic: |
| 70 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 71 | * The first line of the comment (``/*[clinic input]``) is the *start line*. |
| 72 | * The last line of the initial comment (``[clinic start generated code]*/``) is the *end line*. |
| 73 | * The last line (``/*[clinic end generated code: checksum=...]*/``) is the *checksum line*. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 74 | * In between the start line and the end line is the *input*. |
| 75 | * In between the end line and the checksum line is the *output*. |
| 76 | * All the text collectively, from the start line to the checksum line inclusively, |
| 77 | is the *block*. (A block that hasn't been successfully processed by Argument |
| 78 | Clinic yet doesn't have output or a checksum line, but it's still considered |
| 79 | a block.) |
| 80 | |
| 81 | |
| 82 | ============================== |
| 83 | Converting Your First Function |
| 84 | ============================== |
| 85 | |
| 86 | The best way to get a sense of how Argument Clinic works is to |
| 87 | convert a function to work with it. Let's dive in! |
| 88 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 89 | 0. Make sure you're working with a freshly updated checkout |
| 90 | of the CPython trunk. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 91 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 92 | 1. Find a Python builtin that calls either :c:func:`PyArg_ParseTuple` |
| 93 | or :c:func:`PyArg_ParseTupleAndKeywords`, and hasn't been converted |
| 94 | to work with Argument Clinic yet. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 95 | For my example I'm using ``pickle.Pickler.dump()``. |
| 96 | |
| 97 | 2. If the call to the ``PyArg_Parse`` function uses any of the |
| 98 | following format units:: |
| 99 | |
| 100 | O& |
| 101 | O! |
| 102 | es |
| 103 | es# |
| 104 | et |
| 105 | et# |
| 106 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 107 | or if it has multiple calls to :c:func:`PyArg_ParseTuple`, |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 108 | you should choose a different function. Argument Clinic *does* |
| 109 | support all of these scenarios. But these are advanced |
| 110 | topics--let's do something simpler for your first function. |
| 111 | |
| 112 | 3. Add the following boilerplate above the function, creating our block:: |
| 113 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 114 | /*[clinic input] |
| 115 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 116 | |
| 117 | 4. Cut the docstring and paste it in between the ``[clinic]`` lines, |
| 118 | removing all the junk that makes it a properly quoted C string. |
| 119 | When you're done you should have just the text, based at the left |
| 120 | margin, with no line wider than 80 characters. |
| 121 | (Argument Clinic will preserve indents inside the docstring.) |
| 122 | |
| 123 | Sample:: |
| 124 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 125 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 126 | Write a pickled representation of obj to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 127 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 128 | |
| 129 | 5. If your docstring doesn't have a "summary" line, Argument Clinic will |
| 130 | complain. So let's make sure it has one. The "summary" line should |
| 131 | be a paragraph consisting of a single 80-column line |
| 132 | at the beginning of the docstring. |
| 133 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 134 | (Our example docstring consists solely of a summary line, so the sample |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 135 | code doesn't have to change for this step.) |
| 136 | |
| 137 | 6. Above the docstring, enter the name of the function, followed |
| 138 | by a blank line. This should be the Python name of the function, |
| 139 | and should be the full dotted path |
| 140 | to the function--it should start with the name of the module, |
| 141 | include any sub-modules, and if the function is a method on |
| 142 | a class it should include the class name too. |
| 143 | |
| 144 | Sample:: |
| 145 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 146 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 147 | pickle.Pickler.dump |
| 148 | |
| 149 | Write a pickled representation of obj to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 150 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 151 | |
| 152 | 7. If this is the first time that module or class has been used with Argument |
| 153 | Clinic in this C file, |
| 154 | you must declare the module and/or class. Proper Argument Clinic hygiene |
| 155 | prefers declaring these in a separate block somewhere near the |
| 156 | top of the C file, in the same way that include files and statics go at |
| 157 | the top. (In our sample code we'll just show the two blocks next to |
| 158 | each other.) |
| 159 | |
| 160 | Sample:: |
| 161 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 162 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 163 | module pickle |
| 164 | class pickle.Pickler |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 165 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 166 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 167 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 168 | pickle.Pickler.dump |
| 169 | |
| 170 | Write a pickled representation of obj to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 171 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 172 | |
| 173 | |
| 174 | 8. Declare each of the parameters to the function. Each parameter |
| 175 | should get its own line. All the parameter lines should be |
| 176 | indented from the function name and the docstring. |
| 177 | |
| 178 | The general form of these parameter lines is as follows:: |
| 179 | |
| 180 | name_of_parameter: converter |
| 181 | |
| 182 | If the parameter has a default value, add that after the |
| 183 | converter:: |
| 184 | |
| 185 | name_of_parameter: converter = default_value |
| 186 | |
| 187 | Add a blank line below the parameters. |
| 188 | |
| 189 | What's a "converter"? It establishes both the type |
| 190 | of the variable used in C, and the method to convert the Python |
| 191 | value into a C value at runtime. |
| 192 | For now you're going to use what's called a "legacy converter"--a |
| 193 | convenience syntax intended to make porting old code into Argument |
| 194 | Clinic easier. |
| 195 | |
| 196 | For each parameter, copy the "format unit" for that |
| 197 | parameter from the ``PyArg_Parse()`` format argument and |
| 198 | specify *that* as its converter, as a quoted |
| 199 | string. ("format unit" is the formal name for the one-to-three |
| 200 | character substring of the ``format`` parameter that tells |
| 201 | the argument parsing function what the type of the variable |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 202 | is and how to convert it. For more on format units please |
| 203 | see :ref:`arg-parsing`.) |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 204 | |
| 205 | For multicharacter format units like ``z#``, use the |
| 206 | entire two-or-three character string. |
| 207 | |
| 208 | Sample:: |
| 209 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 210 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 211 | module pickle |
| 212 | class pickle.Pickler |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 213 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 214 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 215 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 216 | pickle.Pickler.dump |
| 217 | |
| 218 | obj: 'O' |
| 219 | |
| 220 | Write a pickled representation of obj to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 221 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 222 | |
| 223 | 9. If your function has ``|`` in the format string, meaning some |
| 224 | parameters have default values, you can ignore it. Argument |
| 225 | Clinic infers which parameters are optional based on whether |
| 226 | or not they have default values. |
| 227 | |
| 228 | If your function has ``$`` in the format string, meaning it |
| 229 | takes keyword-only arguments, specify ``*`` on a line by |
| 230 | itself before the first keyword-only argument, indented the |
| 231 | same as the parameter lines. |
| 232 | |
| 233 | (``pickle.Pickler.dump`` has neither, so our sample is unchanged.) |
| 234 | |
| 235 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 236 | 10. If the existing C function calls :c:func:`PyArg_ParseTuple` |
| 237 | (as opposed to :c:func:`PyArg_ParseTupleAndKeywords`), then all its |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 238 | arguments are positional-only. |
| 239 | |
| 240 | To mark all parameters as positional-only in Argument Clinic, |
| 241 | add a ``/`` on a line by itself after the last parameter, |
| 242 | indented the same as the parameter lines. |
| 243 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 244 | Currently this is all-or-nothing; either all parameters are |
| 245 | positional-only, or none of them are. (In the future Argument |
| 246 | Clinic may relax this restriction.) |
| 247 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 248 | Sample:: |
| 249 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 250 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 251 | module pickle |
| 252 | class pickle.Pickler |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 253 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 254 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 255 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 256 | pickle.Pickler.dump |
| 257 | |
| 258 | obj: 'O' |
| 259 | / |
| 260 | |
| 261 | Write a pickled representation of obj to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 262 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 263 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 264 | 11. It's helpful to write a per-parameter docstring for each parameter. |
| 265 | But per-parameter docstrings are optional; you can skip this step |
| 266 | if you prefer. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 267 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 268 | Here's how to add a per-parameter docstring. The first line |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 269 | of the per-parameter docstring must be indented further than the |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 270 | parameter definition. The left margin of this first line establishes |
| 271 | the left margin for the whole per-parameter docstring; all the text |
| 272 | you write will be outdented by this amount. You can write as much |
| 273 | text as you like, across multiple lines if you wish. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 274 | |
| 275 | Sample:: |
| 276 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 277 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 278 | module pickle |
| 279 | class pickle.Pickler |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 280 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 281 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 282 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 283 | pickle.Pickler.dump |
| 284 | |
| 285 | obj: 'O' |
| 286 | The object to be pickled. |
| 287 | / |
| 288 | |
| 289 | Write a pickled representation of obj to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 290 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 291 | |
| 292 | 12. Save and close the file, then run ``Tools/clinic/clinic.py`` on it. |
| 293 | With luck everything worked and your block now has output! Reopen |
| 294 | the file in your text editor to see:: |
| 295 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 296 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 297 | module pickle |
| 298 | class pickle.Pickler |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 299 | [clinic start generated code]*/ |
| 300 | /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 301 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 302 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 303 | pickle.Pickler.dump |
| 304 | |
| 305 | obj: 'O' |
| 306 | The object to be pickled. |
| 307 | / |
| 308 | |
| 309 | Write a pickled representation of obj to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 310 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 311 | |
| 312 | PyDoc_STRVAR(pickle_Pickler_dump__doc__, |
| 313 | "Write a pickled representation of obj to the open file.\n" |
| 314 | "\n" |
| 315 | ... |
| 316 | static PyObject * |
| 317 | pickle_Pickler_dump_impl(PyObject *self, PyObject *obj) |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 318 | /*[clinic end generated code: checksum=3bd30745bf206a48f8b576a1da3d90f55a0a4187]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 319 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 320 | Obviously, if Argument Clinic didn't produce any output, it's because |
| 321 | it found an error in your input. Keep fixing your errors and retrying |
| 322 | until Argument Clinic processes your file without complaint. |
| 323 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 324 | 13. Double-check that the argument-parsing code Argument Clinic generated |
| 325 | looks basically the same as the existing code. |
| 326 | |
| 327 | First, ensure both places use the same argument-parsing function. |
| 328 | The existing code must call either |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 329 | :c:func:`PyArg_ParseTuple` or :c:func:`PyArg_ParseTupleAndKeywords`; |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 330 | ensure that the code generated by Argument Clinic calls the |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 331 | *exact* same function. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 332 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 333 | Second, the format string passed in to :c:func:`PyArg_ParseTuple` or |
| 334 | :c:func:`PyArg_ParseTupleAndKeywords` should be *exactly* the same |
| 335 | as the hand-written one in the existing function, up to the colon |
| 336 | or semi-colon. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 337 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 338 | (Argument Clinic always generates its format strings |
| 339 | with a ``:`` followed by the name of the function. If the |
| 340 | existing code's format string ends with ``;``, to provide |
| 341 | usage help, this change is harmless--don't worry about it.) |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 342 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 343 | Third, for parameters whose format units require two arguments |
| 344 | (like a length variable, or an encoding string, or a pointer |
| 345 | to a conversion function), ensure that the second argument is |
| 346 | *exactly* the same between the two invocations. |
| 347 | |
| 348 | Fourth, inside the output portion of the block you'll find a preprocessor |
| 349 | macro defining the appropriate static :c:type:`PyMethodDef` structure for |
| 350 | this builtin:: |
| 351 | |
| 352 | #define _PICKLE_PICKLER_DUMP_METHODDEF \ |
| 353 | {"dump", (PyCFunction)_pickle_Pickler_dump, METH_O, _pickle_Pickler_dump__doc__}, |
| 354 | |
| 355 | This static structure should be *exactly* the same as the existing static |
| 356 | :c:type:`PyMethodDef` structure for this builtin. |
| 357 | |
| 358 | If any of these items differ in *any way*, |
| 359 | adjust your Argument Clinic function specification and rerun |
| 360 | ``Tools/clinic/clinic.py`` until they *are* the same. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 361 | |
| 362 | |
| 363 | 14. Notice that the last line of its output is the declaration |
| 364 | of your "impl" function. This is where the builtin's implementation goes. |
| 365 | Delete the existing prototype of the function you're modifying, but leave |
| 366 | the opening curly brace. Now delete its argument parsing code and the |
| 367 | declarations of all the variables it dumps the arguments into. |
| 368 | Notice how the Python arguments are now arguments to this impl function; |
| 369 | if the implementation used different names for these variables, fix it. |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 370 | |
| 371 | Let's reiterate, just because it's kind of weird. Your code should now |
| 372 | look like this:: |
| 373 | |
| 374 | static return_type |
| 375 | your_function_impl(...) |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 376 | /*[clinic end generated code: checksum=...]*/ |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 377 | { |
| 378 | ... |
| 379 | |
| 380 | Argument Clinic generated the checksum line and the function prototype just |
| 381 | above it. You should write the opening (and closing) curly braces for the |
| 382 | function, and the implementation inside. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 383 | |
| 384 | Sample:: |
| 385 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 386 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 387 | module pickle |
| 388 | class pickle.Pickler |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 389 | [clinic start generated code]*/ |
| 390 | /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 391 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 392 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 393 | pickle.Pickler.dump |
| 394 | |
| 395 | obj: 'O' |
| 396 | The object to be pickled. |
| 397 | / |
| 398 | |
| 399 | Write a pickled representation of obj to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 400 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 401 | |
| 402 | PyDoc_STRVAR(pickle_Pickler_dump__doc__, |
| 403 | "Write a pickled representation of obj to the open file.\n" |
| 404 | "\n" |
| 405 | ... |
| 406 | static PyObject * |
| 407 | pickle_Pickler_dump_impl(PyObject *self, PyObject *obj) |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 408 | /*[clinic end generated code: checksum=3bd30745bf206a48f8b576a1da3d90f55a0a4187]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 409 | { |
| 410 | /* Check whether the Pickler was initialized correctly (issue3664). |
| 411 | Developers often forget to call __init__() in their subclasses, which |
| 412 | would trigger a segfault without this check. */ |
| 413 | if (self->write == NULL) { |
| 414 | PyErr_Format(PicklingError, |
| 415 | "Pickler.__init__() was not called by %s.__init__()", |
| 416 | Py_TYPE(self)->tp_name); |
| 417 | return NULL; |
| 418 | } |
| 419 | |
| 420 | if (_Pickler_ClearBuffer(self) < 0) |
| 421 | return NULL; |
| 422 | |
| 423 | ... |
| 424 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 425 | 15. Remember the macro with the :c:type:`PyMethodDef` structure for this |
| 426 | function? Find the existing :c:type:`PyMethodDef` structure for this |
| 427 | function and replace it with a reference to the macro. (If the builtin |
| 428 | is at module scope, this will probably be very near the end of the file; |
| 429 | if the builtin is a class method, this will probably be below but relatively |
| 430 | near to the implementation.) |
| 431 | |
| 432 | Note that the body of the macro contains a trailing comma. So when you |
| 433 | replace the existing static :c:type:`PyMethodDef` structure with the macro, |
| 434 | *don't* add a comma to the end. |
| 435 | |
| 436 | Sample:: |
| 437 | |
| 438 | static struct PyMethodDef Pickler_methods[] = { |
| 439 | _PICKLE_PICKLER_DUMP_METHODDEF |
| 440 | _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF |
| 441 | {NULL, NULL} /* sentinel */ |
| 442 | }; |
| 443 | |
| 444 | |
| 445 | 16. Compile, then run the relevant portions of the regression-test suite. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 446 | This change should not introduce any new compile-time warnings or errors, |
| 447 | and there should be no externally-visible change to Python's behavior. |
| 448 | |
| 449 | Well, except for one difference: ``inspect.signature()`` run on your function |
| 450 | should now provide a valid signature! |
| 451 | |
| 452 | Congratulations, you've ported your first function to work with Argument Clinic! |
| 453 | |
| 454 | =============== |
| 455 | Advanced Topics |
| 456 | =============== |
| 457 | |
| 458 | |
| 459 | Renaming the C functions generated by Argument Clinic |
| 460 | ----------------------------------------------------- |
| 461 | |
| 462 | Argument Clinic automatically names the functions it generates for you. |
| 463 | Occasionally this may cause a problem, if the generated name collides with |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 464 | the name of an existing C function. There's an easy solution: override the names |
| 465 | used for the C functions. Just add the keyword ``"as"`` |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 466 | to your function declaration line, followed by the function name you wish to use. |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 467 | Argument Clinic will use that function name for the base (generated) function, |
| 468 | then add ``"_impl"`` to the end and use that for the name of the impl function. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 469 | |
| 470 | For example, if we wanted to rename the C function names generated for |
| 471 | ``pickle.Pickler.dump``, it'd look like this:: |
| 472 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 473 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 474 | pickle.Pickler.dump as pickler_dumper |
| 475 | |
| 476 | ... |
| 477 | |
| 478 | The base function would now be named ``pickler_dumper()``, |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 479 | and the impl function would now be named ``pickler_dumper_impl()``. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 480 | |
| 481 | |
| 482 | Optional Groups |
| 483 | --------------- |
| 484 | |
| 485 | Some legacy functions have a tricky approach to parsing their arguments: |
| 486 | they count the number of positional arguments, then use a ``switch`` statement |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 487 | to call one of several different :c:func:`PyArg_ParseTuple` calls depending on |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 488 | how many positional arguments there are. (These functions cannot accept |
| 489 | keyword-only arguments.) This approach was used to simulate optional |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 490 | arguments back before :c:func:`PyArg_ParseTupleAndKeywords` was created. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 491 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 492 | While functions using this approach can often be converted to |
| 493 | use :c:func:`PyArg_ParseTupleAndKeywords`, optional arguments, and default values, |
| 494 | it's not always possible. Some of these legacy functions have |
| 495 | behaviors :c:func:`PyArg_ParseTupleAndKeywords` doesn't directly support. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 496 | The most obvious example is the builtin function ``range()``, which has |
| 497 | an optional argument on the *left* side of its required argument! |
| 498 | Another example is ``curses.window.addch()``, which has a group of two |
| 499 | arguments that must always be specified together. (The arguments are |
| 500 | called ``x`` and ``y``; if you call the function passing in ``x``, |
| 501 | you must also pass in ``y``--and if you don't pass in ``x`` you may not |
| 502 | pass in ``y`` either.) |
| 503 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 504 | In any case, the goal of Argument Clinic is to support argument parsing |
| 505 | for all existing CPython builtins without changing their semantics. |
| 506 | Therefore Argument Clinic supports |
| 507 | this alternate approach to parsing, using what are called *optional groups*. |
| 508 | Optional groups are groups of arguments that must all be passed in together. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 509 | They can be to the left or the right of the required arguments. They |
| 510 | can *only* be used with positional-only parameters. |
| 511 | |
| 512 | To specify an optional group, add a ``[`` on a line by itself before |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 513 | the parameters you wish to group together, and a ``]`` on a line by itself |
| 514 | after these parameters. As an example, here's how ``curses.window.addch`` |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 515 | uses optional groups to make the first two parameters and the last |
| 516 | parameter optional:: |
| 517 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 518 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 519 | |
| 520 | curses.window.addch |
| 521 | |
| 522 | [ |
| 523 | x: int |
| 524 | X-coordinate. |
| 525 | y: int |
| 526 | Y-coordinate. |
| 527 | ] |
| 528 | |
| 529 | ch: object |
| 530 | Character to add. |
| 531 | |
| 532 | [ |
| 533 | attr: long |
| 534 | Attributes for the character. |
| 535 | ] |
| 536 | / |
| 537 | |
| 538 | ... |
| 539 | |
| 540 | |
| 541 | Notes: |
| 542 | |
| 543 | * For every optional group, one additional parameter will be passed into the |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 544 | impl function representing the group. The parameter will be an int named |
| 545 | ``group_{direction}_{number}``, |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 546 | where ``{direction}`` is either ``right`` or ``left`` depending on whether the group |
| 547 | is before or after the required parameters, and ``{number}`` is a monotonically |
| 548 | increasing number (starting at 1) indicating how far away the group is from |
| 549 | the required parameters. When the impl is called, this parameter will be set |
| 550 | to zero if this group was unused, and set to non-zero if this group was used. |
| 551 | (By used or unused, I mean whether or not the parameters received arguments |
| 552 | in this invocation.) |
| 553 | |
| 554 | * If there are no required arguments, the optional groups will behave |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 555 | as if they're to the right of the required arguments. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 556 | |
| 557 | * In the case of ambiguity, the argument parsing code |
| 558 | favors parameters on the left (before the required parameters). |
| 559 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 560 | * Optional groups can only contain positional-only parameters. |
| 561 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 562 | * Optional groups are *only* intended for legacy code. Please do not |
| 563 | use optional groups for new code. |
| 564 | |
| 565 | |
| 566 | Using real Argument Clinic converters, instead of "legacy converters" |
| 567 | --------------------------------------------------------------------- |
| 568 | |
| 569 | To save time, and to minimize how much you need to learn |
| 570 | to achieve your first port to Argument Clinic, the walkthrough above tells |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 571 | you to use "legacy converters". "Legacy converters" are a convenience, |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 572 | designed explicitly to make porting existing code to Argument Clinic |
| 573 | easier. And to be clear, their use is entirely acceptable when porting |
| 574 | code for Python 3.4. |
| 575 | |
| 576 | However, in the long term we probably want all our blocks to |
| 577 | use Argument Clinic's real syntax for converters. Why? A couple |
| 578 | reasons: |
| 579 | |
| 580 | * The proper converters are far easier to read and clearer in their intent. |
| 581 | * There are some format units that are unsupported as "legacy converters", |
| 582 | because they require arguments, and the legacy converter syntax doesn't |
| 583 | support specifying arguments. |
| 584 | * In the future we may have a new argument parsing library that isn't |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 585 | restricted to what :c:func:`PyArg_ParseTuple` supports; this flexibility |
| 586 | won't be available to parameters using legacy converters. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 587 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 588 | Therefore, if you don't mind a little extra effort, you should consider |
| 589 | using normal converters instead of legacy converters. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 590 | |
| 591 | In a nutshell, the syntax for Argument Clinic (non-legacy) converters |
| 592 | looks like a Python function call. However, if there are no explicit |
| 593 | arguments to the function (all functions take their default values), |
| 594 | you may omit the parentheses. Thus ``bool`` and ``bool()`` are exactly |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 595 | the same converters. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 596 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 597 | All arguments to Argument Clinic converters are keyword-only. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 598 | All Argument Clinic converters accept the following arguments: |
| 599 | |
| 600 | ``doc_default`` |
| 601 | If the parameter takes a default value, normally this value is also |
| 602 | provided in the ``inspect.Signature`` metadata, and displayed in the |
| 603 | docstring. ``doc_default`` lets you override the value used in these |
| 604 | two places: pass in a string representing the Python value you wish |
| 605 | to use in these two contexts. |
| 606 | |
| 607 | ``required`` |
| 608 | If a parameter takes a default value, Argument Clinic infers that the |
| 609 | parameter is optional. However, you may want a parameter to take a |
| 610 | default value in C, but not behave in Python as if the parameter is |
| 611 | optional. Passing in ``required=True`` to a converter tells Argument |
| 612 | Clinic that this parameter is not optional, even if it has a default |
| 613 | value. |
| 614 | |
| 615 | ``annotation`` |
| 616 | The annotation value for this parameter. Not currently supported, |
| 617 | because PEP 8 mandates that the Python library may not use |
| 618 | annotations. |
| 619 | |
| 620 | Below is a table showing the mapping of legacy converters into real |
| 621 | Argument Clinic converters. On the left is the legacy converter, |
| 622 | on the right is the text you'd replace it with. |
| 623 | |
| 624 | ========= ================================================================================= |
| 625 | ``'B'`` ``byte(bitwise=True)`` |
| 626 | ``'b'`` ``byte`` |
| 627 | ``'c'`` ``char`` |
| 628 | ``'C'`` ``int(types='str')`` |
| 629 | ``'d'`` ``double`` |
| 630 | ``'D'`` ``Py_complex`` |
| 631 | ``'es#'`` ``str(encoding='name_of_encoding', length=True, zeroes=True)`` |
| 632 | ``'es'`` ``str(encoding='name_of_encoding')`` |
| 633 | ``'et#'`` ``str(encoding='name_of_encoding', types='bytes bytearray str', length=True)`` |
| 634 | ``'et'`` ``str(encoding='name_of_encoding', types='bytes bytearray str')`` |
| 635 | ``'f'`` ``float`` |
| 636 | ``'h'`` ``short`` |
| 637 | ``'H'`` ``unsigned_short`` |
| 638 | ``'i'`` ``int`` |
| 639 | ``'I'`` ``unsigned_int`` |
| 640 | ``'K'`` ``unsigned_PY_LONG_LONG`` |
| 641 | ``'L'`` ``PY_LONG_LONG`` |
| 642 | ``'n'`` ``Py_ssize_t`` |
Larry Hastings | 77561cc | 2014-01-07 12:13:13 -0800 | [diff] [blame] | 643 | ``'O!'`` ``object(subclass_of='&PySomething_Type')`` |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 644 | ``'O&'`` ``object(converter='name_of_c_function')`` |
| 645 | ``'O'`` ``object`` |
| 646 | ``'p'`` ``bool`` |
| 647 | ``'s#'`` ``str(length=True)`` |
| 648 | ``'S'`` ``PyBytesObject`` |
| 649 | ``'s'`` ``str`` |
| 650 | ``'s*'`` ``Py_buffer(types='str bytes bytearray buffer')`` |
| 651 | ``'u#'`` ``Py_UNICODE(length=True)`` |
| 652 | ``'u'`` ``Py_UNICODE`` |
| 653 | ``'U'`` ``unicode`` |
| 654 | ``'w*'`` ``Py_buffer(types='bytearray rwbuffer')`` |
| 655 | ``'y#'`` ``str(type='bytes', length=True)`` |
| 656 | ``'Y'`` ``PyByteArrayObject`` |
| 657 | ``'y'`` ``str(type='bytes')`` |
| 658 | ``'y*'`` ``Py_buffer`` |
| 659 | ``'Z#'`` ``Py_UNICODE(nullable=True, length=True)`` |
| 660 | ``'z#'`` ``str(nullable=True, length=True)`` |
| 661 | ``'Z'`` ``Py_UNICODE(nullable=True)`` |
| 662 | ``'z'`` ``str(nullable=True)`` |
| 663 | ``'z*'`` ``Py_buffer(types='str bytes bytearray buffer', nullable=True)`` |
| 664 | ========= ================================================================================= |
| 665 | |
| 666 | As an example, here's our sample ``pickle.Pickler.dump`` using the proper |
| 667 | converter:: |
| 668 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 669 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 670 | pickle.Pickler.dump |
| 671 | |
| 672 | obj: object |
| 673 | The object to be pickled. |
| 674 | / |
| 675 | |
| 676 | Write a pickled representation of obj to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 677 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 678 | |
| 679 | Argument Clinic will show you all the converters it has |
| 680 | available. For each converter it'll show you all the parameters |
| 681 | it accepts, along with the default value for each parameter. |
| 682 | Just run ``Tools/clinic/clinic.py --converters`` to see the full list. |
| 683 | |
| 684 | |
| 685 | Advanced converters |
| 686 | ------------------- |
| 687 | |
| 688 | Remeber those format units you skipped for your first |
| 689 | time because they were advanced? Here's how to handle those too. |
| 690 | |
| 691 | The trick is, all those format units take arguments--either |
| 692 | conversion functions, or types, or strings specifying an encoding. |
| 693 | (But "legacy converters" don't support arguments. That's why we |
| 694 | skipped them for your first function.) The argument you specified |
| 695 | to the format unit is now an argument to the converter; this |
Larry Hastings | 77561cc | 2014-01-07 12:13:13 -0800 | [diff] [blame] | 696 | argument is either ``converter`` (for ``O&``), ``subclass_of`` (for ``O!``), |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 697 | or ``encoding`` (for all the format units that start with ``e``). |
| 698 | |
Larry Hastings | 77561cc | 2014-01-07 12:13:13 -0800 | [diff] [blame] | 699 | When using ``subclass_of``, you may also want to use the other |
| 700 | custom argument for ``object()``: ``type``, which lets you set the type |
| 701 | actually used for the parameter. For example, if you want to ensure |
| 702 | that the object is a subclass of ``PyUnicode_Type``, you probably want |
| 703 | to use the converter ``object(type='PyUnicodeObject *', subclass_of='&PyUnicode_Type')``. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 704 | |
Larry Hastings | 77561cc | 2014-01-07 12:13:13 -0800 | [diff] [blame] | 705 | One possible problem with using Argument Clinic: it takes away some possible |
| 706 | flexibility for the format units starting with ``e``. When writing a |
| 707 | ``PyArg_Parse`` call by hand, you could theoretically decide at runtime what |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 708 | encoding string to pass in to :c:func:`PyArg_ParseTuple`. But now this string must |
Larry Hastings | 77561cc | 2014-01-07 12:13:13 -0800 | [diff] [blame] | 709 | be hard-coded at Argument-Clinic-preprocessing-time. This limitation is deliberate; |
| 710 | it made supporting this format unit much easier, and may allow for future optimizations. |
| 711 | This restriction doesn't seem unreasonable; CPython itself always passes in static |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 712 | hard-coded encoding strings for parameters whose format units start with ``e``. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 713 | |
| 714 | |
| 715 | Using a return converter |
| 716 | ------------------------ |
| 717 | |
| 718 | By default the impl function Argument Clinic generates for you returns ``PyObject *``. |
| 719 | But your C function often computes some C type, then converts it into the ``PyObject *`` |
| 720 | at the last moment. Argument Clinic handles converting your inputs from Python types |
| 721 | into native C types--why not have it convert your return value from a native C type |
| 722 | into a Python type too? |
| 723 | |
| 724 | That's what a "return converter" does. It changes your impl function to return |
| 725 | some C type, then adds code to the generated (non-impl) function to handle converting |
| 726 | that value into the appropriate ``PyObject *``. |
| 727 | |
| 728 | The syntax for return converters is similar to that of parameter converters. |
| 729 | You specify the return converter like it was a return annotation on the |
| 730 | function itself. Return converters behave much the same as parameter converters; |
| 731 | they take arguments, the arguments are all keyword-only, and if you're not changing |
| 732 | any of the default arguments you can omit the parentheses. |
| 733 | |
| 734 | (If you use both ``"as"`` *and* a return converter for your function, |
| 735 | the ``"as"`` should come before the return converter.) |
| 736 | |
| 737 | There's one additional complication when using return converters: how do you |
| 738 | indicate an error has occured? Normally, a function returns a valid (non-``NULL``) |
| 739 | pointer for success, and ``NULL`` for failure. But if you use an integer return converter, |
| 740 | all integers are valid. How can Argument Clinic detect an error? Its solution: each return |
| 741 | converter implicitly looks for a special value that indicates an error. If you return |
| 742 | that value, and an error has been set (``PyErr_Occurred()`` returns a true |
| 743 | value), then the generated code will propogate the error. Otherwise it will |
| 744 | encode the value you return like normal. |
| 745 | |
| 746 | Currently Argument Clinic supports only a few return converters:: |
| 747 | |
| 748 | int |
| 749 | long |
| 750 | Py_ssize_t |
| 751 | DecodeFSDefault |
| 752 | |
| 753 | None of these take parameters. For the first three, return -1 to indicate |
| 754 | error. For ``DecodeFSDefault``, the return type is ``char *``; return a NULL |
| 755 | pointer to indicate an error. |
| 756 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 757 | To see all the return converters Argument Clinic supports, along with |
| 758 | their parameters (if any), |
| 759 | just run ``Tools/clinic/clinic.py --converters`` for the full list. |
| 760 | |
| 761 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 762 | Calling Python code |
| 763 | ------------------- |
| 764 | |
| 765 | The rest of the advanced topics require you to write Python code |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 766 | which lives inside your C file and modifies Argument Clinic's |
| 767 | runtime state. This is simple: you simply define a Python block. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 768 | |
| 769 | A Python block uses different delimiter lines than an Argument |
| 770 | Clinic function block. It looks like this:: |
| 771 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 772 | /*[python input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 773 | # python code goes here |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 774 | [python start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 775 | |
| 776 | All the code inside the Python block is executed at the |
| 777 | time it's parsed. All text written to stdout inside the block |
| 778 | is redirected into the "output" after the block. |
| 779 | |
| 780 | As an example, here's a Python block that adds a static integer |
| 781 | variable to the C code:: |
| 782 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 783 | /*[python input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 784 | print('static int __ignored_unused_variable__ = 0;') |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 785 | [python start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 786 | static int __ignored_unused_variable__ = 0; |
| 787 | /*[python checksum:...]*/ |
| 788 | |
| 789 | |
| 790 | Using a "self converter" |
| 791 | ------------------------ |
| 792 | |
| 793 | Argument Clinic automatically adds a "self" parameter for you |
| 794 | using a default converter. However, you can override |
| 795 | Argument Clinic's converter and specify one yourself. |
| 796 | Just add your own ``self`` parameter as the first parameter in a |
| 797 | block, and ensure that its converter is an instance of |
| 798 | ``self_converter`` or a subclass thereof. |
| 799 | |
| 800 | What's the point? This lets you automatically cast ``self`` |
Larry Hastings | 77561cc | 2014-01-07 12:13:13 -0800 | [diff] [blame] | 801 | from ``PyObject *`` to a custom type, just like ``object()`` |
| 802 | does with its ``type`` parameter. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 803 | |
| 804 | How do you specify the custom type you want to cast ``self`` to? |
| 805 | If you only have one or two functions with the same type for ``self``, |
| 806 | you can directly use Argument Clinic's existing ``self`` converter, |
| 807 | passing in the type you want to use as the ``type`` parameter:: |
| 808 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 809 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 810 | |
| 811 | _pickle.Pickler.dump |
| 812 | |
| 813 | self: self(type="PicklerObject *") |
| 814 | obj: object |
| 815 | / |
| 816 | |
| 817 | Write a pickled representation of the given object to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 818 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 819 | |
| 820 | On the other hand, if you have a lot of functions that will use the same |
| 821 | type for ``self``, it's best to create your own converter, subclassing |
| 822 | ``self_converter`` but overwriting the ``type`` member:: |
| 823 | |
Zachary Ware | c1cb227 | 2014-01-09 21:41:23 -0600 | [diff] [blame^] | 824 | /*[python input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 825 | class PicklerObject_converter(self_converter): |
| 826 | type = "PicklerObject *" |
Zachary Ware | c1cb227 | 2014-01-09 21:41:23 -0600 | [diff] [blame^] | 827 | [python start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 828 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 829 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 830 | |
| 831 | _pickle.Pickler.dump |
| 832 | |
| 833 | self: PicklerObject |
| 834 | obj: object |
| 835 | / |
| 836 | |
| 837 | Write a pickled representation of the given object to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 838 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 839 | |
| 840 | |
| 841 | |
| 842 | Writing a custom converter |
| 843 | -------------------------- |
| 844 | |
| 845 | As we hinted at in the previous section... you can write your own converters! |
| 846 | A converter is simply a Python class that inherits from ``CConverter``. |
| 847 | The main purpose of a custom converter is if you have a parameter using |
| 848 | the ``O&`` format unit--parsing this parameter means calling |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 849 | a :c:func:`PyArg_ParseTuple` "converter function". |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 850 | |
| 851 | Your converter class should be named ``*something*_converter``. |
| 852 | If the name follows this convention, then your converter class |
| 853 | will be automatically registered with Argument Clinic; its name |
| 854 | will be the name of your class with the ``_converter`` suffix |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 855 | stripped off. (This is accomplished with a metaclass.) |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 856 | |
| 857 | You shouldn't subclass ``CConverter.__init__``. Instead, you should |
| 858 | write a ``converter_init()`` function. ``converter_init()`` |
| 859 | always accepts a ``self`` parameter; after that, all additional |
| 860 | parameters *must* be keyword-only. Any arguments passed in to |
| 861 | the converter in Argument Clinic will be passed along to your |
| 862 | ``converter_init()``. |
| 863 | |
| 864 | There are some additional members of ``CConverter`` you may wish |
| 865 | to specify in your subclass. Here's the current list: |
| 866 | |
| 867 | ``type`` |
| 868 | The C type to use for this variable. |
| 869 | ``type`` should be a Python string specifying the type, e.g. ``int``. |
| 870 | If this is a pointer type, the type string should end with ``' *'``. |
| 871 | |
| 872 | ``default`` |
| 873 | The Python default value for this parameter, as a Python value. |
| 874 | Or the magic value ``unspecified`` if there is no default. |
| 875 | |
| 876 | ``doc_default`` |
| 877 | ``default`` as it should appear in the documentation, |
| 878 | as a string. |
| 879 | Or ``None`` if there is no default. |
| 880 | This string, when run through ``eval()``, should produce |
| 881 | a Python value. |
| 882 | |
| 883 | ``py_default`` |
| 884 | ``default`` as it should appear in Python code, |
| 885 | as a string. |
| 886 | Or ``None`` if there is no default. |
| 887 | |
| 888 | ``c_default`` |
| 889 | ``default`` as it should appear in C code, |
| 890 | as a string. |
| 891 | Or ``None`` if there is no default. |
| 892 | |
| 893 | ``c_ignored_default`` |
| 894 | The default value used to initialize the C variable when |
| 895 | there is no default, but not specifying a default may |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 896 | result in an "uninitialized variable" warning. This can |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 897 | easily happen when using option groups--although |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 898 | properly-written code will never actually use this value, |
| 899 | the variable does get passed in to the impl, and the |
| 900 | C compiler will complain about the "use" of the |
| 901 | uninitialized value. This value should always be a |
| 902 | non-empty string. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 903 | |
| 904 | ``converter`` |
| 905 | The name of the C converter function, as a string. |
| 906 | |
| 907 | ``impl_by_reference`` |
| 908 | A boolean value. If true, |
| 909 | Argument Clinic will add a ``&`` in front of the name of |
| 910 | the variable when passing it into the impl function. |
| 911 | |
| 912 | ``parse_by_reference`` |
| 913 | A boolean value. If true, |
| 914 | Argument Clinic will add a ``&`` in front of the name of |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 915 | the variable when passing it into :c:func:`PyArg_ParseTuple`. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 916 | |
| 917 | |
| 918 | Here's the simplest example of a custom converter, from ``Modules/zlibmodule.c``:: |
| 919 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 920 | /*[python input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 921 | |
| 922 | class uint_converter(CConverter): |
| 923 | type = 'unsigned int' |
| 924 | converter = 'uint_converter' |
| 925 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 926 | [python start generated code]*/ |
| 927 | /*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 928 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 929 | This block adds a converter to Argument Clinic named ``uint``. Parameters |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 930 | declared as ``uint`` will be declared as type ``unsigned int``, and will |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 931 | be parsed by the ``'O&'`` format unit, which will call the ``uint_converter`` |
| 932 | converter function. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 933 | ``uint`` variables automatically support default values. |
| 934 | |
| 935 | More sophisticated custom converters can insert custom C code to |
| 936 | handle initialization and cleanup. |
| 937 | You can see more examples of custom converters in the CPython |
| 938 | source tree; grep the C files for the string ``CConverter``. |
| 939 | |
| 940 | Writing a custom return converter |
| 941 | --------------------------------- |
| 942 | |
| 943 | Writing a custom return converter is much like writing |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 944 | a custom converter. Except it's somewhat simpler, because return |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 945 | converters are themselves much simpler. |
| 946 | |
| 947 | Return converters must subclass ``CReturnConverter``. |
| 948 | There are no examples yet of custom return converters, |
| 949 | because they are not widely used yet. If you wish to |
| 950 | write your own return converter, please read ``Tools/clinic/clinic.py``, |
| 951 | specifically the implementation of ``CReturnConverter`` and |
| 952 | all its subclasses. |
| 953 | |
| 954 | |
| 955 | Using Argument Clinic in Python files |
| 956 | ------------------------------------- |
| 957 | |
| 958 | It's actually possible to use Argument Clinic to preprocess Python files. |
| 959 | There's no point to using Argument Clinic blocks, of course, as the output |
| 960 | wouldn't make any sense to the Python interpreter. But using Argument Clinic |
| 961 | to run Python blocks lets you use Python as a Python preprocessor! |
| 962 | |
| 963 | Since Python comments are different from C comments, Argument Clinic |
| 964 | blocks embedded in Python files look slightly different. They look like this:: |
| 965 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 966 | #/*[python input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 967 | #print("def foo(): pass") |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 968 | #[python start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 969 | def foo(): pass |
| 970 | #/*[python checksum:...]*/ |