Martin Panter | cfa9bad | 2016-12-10 04:10:45 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
Ezio Melotti | a3642b6 | 2014-01-25 17:27:46 +0200 | [diff] [blame] | 3 | ********************** |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 4 | Argument Clinic How-To |
Ezio Melotti | a3642b6 | 2014-01-25 17:27:46 +0200 | [diff] [blame] | 5 | ********************** |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 6 | |
| 7 | :author: Larry Hastings |
| 8 | |
| 9 | |
| 10 | .. topic:: Abstract |
| 11 | |
| 12 | Argument Clinic is a preprocessor for CPython C files. |
| 13 | Its purpose is to automate all the boilerplate involved |
| 14 | with writing argument parsing code for "builtins". |
| 15 | This document shows you how to convert your first C |
| 16 | function to work with Argument Clinic, and then introduces |
| 17 | some advanced topics on Argument Clinic usage. |
| 18 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 19 | Currently Argument Clinic is considered internal-only |
| 20 | for CPython. Its use is not supported for files outside |
| 21 | CPython, and no guarantees are made regarding backwards |
| 22 | compatibility for future versions. In other words: if you |
| 23 | maintain an external C extension for CPython, you're welcome |
| 24 | to experiment with Argument Clinic in your own code. But the |
Eitan Adler | 9572132 | 2018-05-20 07:38:01 -0700 | [diff] [blame] | 25 | version of Argument Clinic that ships with the next version |
| 26 | of CPython *could* be totally incompatible and break all your code. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 27 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 28 | The Goals Of Argument Clinic |
| 29 | ============================ |
| 30 | |
| 31 | Argument Clinic's primary goal |
| 32 | is to take over responsibility for all argument parsing code |
| 33 | inside CPython. This means that, when you convert a function |
| 34 | to work with Argument Clinic, that function should no longer |
Martin Panter | 357ed2e | 2016-11-21 00:15:20 +0000 | [diff] [blame] | 35 | do any of its own argument parsing—the code generated by |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 36 | Argument Clinic should be a "black box" to you, where CPython |
| 37 | calls in at the top, and your code gets called at the bottom, |
| 38 | with ``PyObject *args`` (and maybe ``PyObject *kwargs``) |
| 39 | magically converted into the C variables and types you need. |
| 40 | |
| 41 | In order for Argument Clinic to accomplish its primary goal, |
| 42 | it must be easy to use. Currently, working with CPython's |
| 43 | argument parsing library is a chore, requiring maintaining |
| 44 | redundant information in a surprising number of places. |
| 45 | When you use Argument Clinic, you don't have to repeat yourself. |
| 46 | |
| 47 | Obviously, no one would want to use Argument Clinic unless |
Martin Panter | 357ed2e | 2016-11-21 00:15:20 +0000 | [diff] [blame] | 48 | it's solving their problem—and without creating new problems of |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 49 | its own. |
Larry Hastings | 537d760 | 2014-01-18 01:08:50 -0800 | [diff] [blame] | 50 | So it's paramount that Argument Clinic generate correct code. |
| 51 | It'd be nice if the code was faster, too, but at the very least |
| 52 | it should not introduce a major speed regression. (Eventually Argument |
Martin Panter | 357ed2e | 2016-11-21 00:15:20 +0000 | [diff] [blame] | 53 | Clinic *should* make a major speedup possible—we could |
Larry Hastings | 537d760 | 2014-01-18 01:08:50 -0800 | [diff] [blame] | 54 | rewrite its code generator to produce tailor-made argument |
| 55 | parsing code, rather than calling the general-purpose CPython |
| 56 | argument parsing library. That would make for the fastest |
| 57 | argument parsing possible!) |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 58 | |
| 59 | Additionally, Argument Clinic must be flexible enough to |
| 60 | work with any approach to argument parsing. Python has |
| 61 | some functions with some very strange parsing behaviors; |
| 62 | Argument Clinic's goal is to support all of them. |
| 63 | |
| 64 | Finally, the original motivation for Argument Clinic was |
| 65 | to provide introspection "signatures" for CPython builtins. |
| 66 | It used to be, the introspection query functions would throw |
| 67 | an exception if you passed in a builtin. With Argument |
| 68 | Clinic, that's a thing of the past! |
| 69 | |
| 70 | One idea you should keep in mind, as you work with |
| 71 | Argument Clinic: the more information you give it, the |
| 72 | better job it'll be able to do. |
| 73 | Argument Clinic is admittedly relatively simple right |
| 74 | now. But as it evolves it will get more sophisticated, |
| 75 | and it should be able to do many interesting and smart |
| 76 | things with all the information you give it. |
| 77 | |
| 78 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 79 | Basic Concepts And Usage |
| 80 | ======================== |
| 81 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 82 | Argument Clinic ships with CPython; you'll find it in ``Tools/clinic/clinic.py``. |
Martin Panter | cfa9bad | 2016-12-10 04:10:45 +0000 | [diff] [blame] | 83 | If you run that script, specifying a C file as an argument: |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 84 | |
Martin Panter | cfa9bad | 2016-12-10 04:10:45 +0000 | [diff] [blame] | 85 | .. code-block:: shell-session |
| 86 | |
| 87 | $ python3 Tools/clinic/clinic.py foo.c |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 88 | |
| 89 | Argument Clinic will scan over the file looking for lines that |
Martin Panter | cfa9bad | 2016-12-10 04:10:45 +0000 | [diff] [blame] | 90 | look exactly like this: |
| 91 | |
| 92 | .. code-block:: none |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 93 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 94 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 95 | |
| 96 | When it finds one, it reads everything up to a line that looks |
Martin Panter | cfa9bad | 2016-12-10 04:10:45 +0000 | [diff] [blame] | 97 | exactly like this: |
| 98 | |
| 99 | .. code-block:: none |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 100 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 101 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 102 | |
| 103 | Everything in between these two lines is input for Argument Clinic. |
| 104 | All of these lines, including the beginning and ending comment |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 105 | lines, are collectively called an Argument Clinic "block". |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 106 | |
| 107 | When Argument Clinic parses one of these blocks, it |
| 108 | generates output. This output is rewritten into the C file |
| 109 | immediately after the block, followed by a comment containing a checksum. |
Martin Panter | cfa9bad | 2016-12-10 04:10:45 +0000 | [diff] [blame] | 110 | The Argument Clinic block now looks like this: |
| 111 | |
| 112 | .. code-block:: none |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 113 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 114 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 115 | ... clinic input goes here ... |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 116 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 117 | ... clinic output goes here ... |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 118 | /*[clinic end generated code: checksum=...]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 119 | |
| 120 | If you run Argument Clinic on the same file a second time, Argument Clinic |
| 121 | will discard the old output and write out the new output with a fresh checksum |
| 122 | line. However, if the input hasn't changed, the output won't change either. |
| 123 | |
| 124 | You should never modify the output portion of an Argument Clinic block. Instead, |
| 125 | change the input until it produces the output you want. (That's the purpose of the |
Martin Panter | 357ed2e | 2016-11-21 00:15:20 +0000 | [diff] [blame] | 126 | checksum—to detect if someone changed the output, as these edits would be lost |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 127 | the next time Argument Clinic writes out fresh output.) |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 128 | |
| 129 | For the sake of clarity, here's the terminology we'll use with Argument Clinic: |
| 130 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 131 | * The first line of the comment (``/*[clinic input]``) is the *start line*. |
| 132 | * The last line of the initial comment (``[clinic start generated code]*/``) is the *end line*. |
| 133 | * The last line (``/*[clinic end generated code: checksum=...]*/``) is the *checksum line*. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 134 | * In between the start line and the end line is the *input*. |
| 135 | * In between the end line and the checksum line is the *output*. |
| 136 | * All the text collectively, from the start line to the checksum line inclusively, |
| 137 | is the *block*. (A block that hasn't been successfully processed by Argument |
| 138 | Clinic yet doesn't have output or a checksum line, but it's still considered |
| 139 | a block.) |
| 140 | |
| 141 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 142 | Converting Your First Function |
| 143 | ============================== |
| 144 | |
| 145 | The best way to get a sense of how Argument Clinic works is to |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 146 | convert a function to work with it. Here, then, are the bare |
| 147 | minimum steps you'd need to follow to convert a function to |
| 148 | work with Argument Clinic. Note that for code you plan to |
| 149 | check in to CPython, you really should take the conversion farther, |
| 150 | using some of the advanced concepts you'll see later on in |
| 151 | the document (like "return converters" and "self converters"). |
| 152 | But we'll keep it simple for this walkthrough so you can learn. |
| 153 | |
| 154 | Let's dive in! |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 155 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 156 | 0. Make sure you're working with a freshly updated checkout |
| 157 | of the CPython trunk. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 158 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 159 | 1. Find a Python builtin that calls either :c:func:`PyArg_ParseTuple` |
| 160 | or :c:func:`PyArg_ParseTupleAndKeywords`, and hasn't been converted |
| 161 | to work with Argument Clinic yet. |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 162 | For my example I'm using ``_pickle.Pickler.dump()``. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 163 | |
| 164 | 2. If the call to the ``PyArg_Parse`` function uses any of the |
Martin Panter | 1050d2d | 2016-07-26 11:18:21 +0200 | [diff] [blame] | 165 | following format units: |
| 166 | |
| 167 | .. code-block:: none |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 168 | |
| 169 | O& |
| 170 | O! |
| 171 | es |
| 172 | es# |
| 173 | et |
| 174 | et# |
| 175 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 176 | or if it has multiple calls to :c:func:`PyArg_ParseTuple`, |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 177 | you should choose a different function. Argument Clinic *does* |
| 178 | support all of these scenarios. But these are advanced |
Martin Panter | 357ed2e | 2016-11-21 00:15:20 +0000 | [diff] [blame] | 179 | topics—let's do something simpler for your first function. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 180 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 181 | Also, if the function has multiple calls to :c:func:`PyArg_ParseTuple` |
| 182 | or :c:func:`PyArg_ParseTupleAndKeywords` where it supports different |
| 183 | types for the same argument, or if the function uses something besides |
| 184 | PyArg_Parse functions to parse its arguments, it probably |
| 185 | isn't suitable for conversion to Argument Clinic. Argument Clinic |
| 186 | doesn't support generic functions or polymorphic parameters. |
| 187 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 188 | 3. Add the following boilerplate above the function, creating our block:: |
| 189 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 190 | /*[clinic input] |
| 191 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 192 | |
| 193 | 4. Cut the docstring and paste it in between the ``[clinic]`` lines, |
| 194 | removing all the junk that makes it a properly quoted C string. |
| 195 | When you're done you should have just the text, based at the left |
| 196 | margin, with no line wider than 80 characters. |
| 197 | (Argument Clinic will preserve indents inside the docstring.) |
| 198 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 199 | If the old docstring had a first line that looked like a function |
| 200 | signature, throw that line away. (The docstring doesn't need it |
Martin Panter | 357ed2e | 2016-11-21 00:15:20 +0000 | [diff] [blame] | 201 | anymore—when you use ``help()`` on your builtin in the future, |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 202 | the first line will be built automatically based on the function's |
| 203 | signature.) |
| 204 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 205 | Sample:: |
| 206 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 207 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 208 | Write a pickled representation of obj to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 209 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 210 | |
| 211 | 5. If your docstring doesn't have a "summary" line, Argument Clinic will |
| 212 | complain. So let's make sure it has one. The "summary" line should |
| 213 | be a paragraph consisting of a single 80-column line |
| 214 | at the beginning of the docstring. |
| 215 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 216 | (Our example docstring consists solely of a summary line, so the sample |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 217 | code doesn't have to change for this step.) |
| 218 | |
| 219 | 6. Above the docstring, enter the name of the function, followed |
| 220 | by a blank line. This should be the Python name of the function, |
| 221 | and should be the full dotted path |
Martin Panter | 357ed2e | 2016-11-21 00:15:20 +0000 | [diff] [blame] | 222 | to the function—it should start with the name of the module, |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 223 | include any sub-modules, and if the function is a method on |
| 224 | a class it should include the class name too. |
| 225 | |
| 226 | Sample:: |
| 227 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 228 | /*[clinic input] |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 229 | _pickle.Pickler.dump |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 230 | |
| 231 | Write a pickled representation of obj to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 232 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 233 | |
| 234 | 7. If this is the first time that module or class has been used with Argument |
| 235 | Clinic in this C file, |
| 236 | you must declare the module and/or class. Proper Argument Clinic hygiene |
| 237 | prefers declaring these in a separate block somewhere near the |
| 238 | top of the C file, in the same way that include files and statics go at |
| 239 | the top. (In our sample code we'll just show the two blocks next to |
| 240 | each other.) |
| 241 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 242 | The name of the class and module should be the same as the one |
| 243 | seen by Python. Check the name defined in the :c:type:`PyModuleDef` |
| 244 | or :c:type:`PyTypeObject` as appropriate. |
| 245 | |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 246 | When you declare a class, you must also specify two aspects of its type |
| 247 | in C: the type declaration you'd use for a pointer to an instance of |
| 248 | this class, and a pointer to the :c:type:`PyTypeObject` for this class. |
| 249 | |
| 250 | Sample:: |
| 251 | |
| 252 | /*[clinic input] |
| 253 | module _pickle |
| 254 | class _pickle.Pickler "PicklerObject *" "&Pickler_Type" |
| 255 | [clinic start generated code]*/ |
| 256 | |
| 257 | /*[clinic input] |
| 258 | _pickle.Pickler.dump |
| 259 | |
| 260 | Write a pickled representation of obj to the open file. |
| 261 | [clinic start generated code]*/ |
| 262 | |
| 263 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 264 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 265 | |
| 266 | 8. Declare each of the parameters to the function. Each parameter |
| 267 | should get its own line. All the parameter lines should be |
| 268 | indented from the function name and the docstring. |
| 269 | |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 270 | The general form of these parameter lines is as follows: |
| 271 | |
| 272 | .. code-block:: none |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 273 | |
| 274 | name_of_parameter: converter |
| 275 | |
| 276 | If the parameter has a default value, add that after the |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 277 | converter: |
| 278 | |
| 279 | .. code-block:: none |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 280 | |
| 281 | name_of_parameter: converter = default_value |
| 282 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 283 | Argument Clinic's support for "default values" is quite sophisticated; |
| 284 | please see :ref:`the section below on default values <default_values>` |
| 285 | for more information. |
| 286 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 287 | Add a blank line below the parameters. |
| 288 | |
| 289 | What's a "converter"? It establishes both the type |
| 290 | of the variable used in C, and the method to convert the Python |
| 291 | value into a C value at runtime. |
Martin Panter | 357ed2e | 2016-11-21 00:15:20 +0000 | [diff] [blame] | 292 | For now you're going to use what's called a "legacy converter"—a |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 293 | convenience syntax intended to make porting old code into Argument |
| 294 | Clinic easier. |
| 295 | |
| 296 | For each parameter, copy the "format unit" for that |
| 297 | parameter from the ``PyArg_Parse()`` format argument and |
| 298 | specify *that* as its converter, as a quoted |
| 299 | string. ("format unit" is the formal name for the one-to-three |
| 300 | character substring of the ``format`` parameter that tells |
| 301 | the argument parsing function what the type of the variable |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 302 | is and how to convert it. For more on format units please |
| 303 | see :ref:`arg-parsing`.) |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 304 | |
| 305 | For multicharacter format units like ``z#``, use the |
| 306 | entire two-or-three character string. |
| 307 | |
| 308 | Sample:: |
| 309 | |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 310 | /*[clinic input] |
| 311 | module _pickle |
| 312 | class _pickle.Pickler "PicklerObject *" "&Pickler_Type" |
| 313 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 314 | |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 315 | /*[clinic input] |
| 316 | _pickle.Pickler.dump |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 317 | |
| 318 | obj: 'O' |
| 319 | |
| 320 | Write a pickled representation of obj to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 321 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 322 | |
| 323 | 9. If your function has ``|`` in the format string, meaning some |
| 324 | parameters have default values, you can ignore it. Argument |
| 325 | Clinic infers which parameters are optional based on whether |
| 326 | or not they have default values. |
| 327 | |
| 328 | If your function has ``$`` in the format string, meaning it |
| 329 | takes keyword-only arguments, specify ``*`` on a line by |
| 330 | itself before the first keyword-only argument, indented the |
| 331 | same as the parameter lines. |
| 332 | |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 333 | (``_pickle.Pickler.dump`` has neither, so our sample is unchanged.) |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 334 | |
| 335 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 336 | 10. If the existing C function calls :c:func:`PyArg_ParseTuple` |
| 337 | (as opposed to :c:func:`PyArg_ParseTupleAndKeywords`), then all its |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 338 | arguments are positional-only. |
| 339 | |
| 340 | To mark all parameters as positional-only in Argument Clinic, |
| 341 | add a ``/`` on a line by itself after the last parameter, |
| 342 | indented the same as the parameter lines. |
| 343 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 344 | Currently this is all-or-nothing; either all parameters are |
| 345 | positional-only, or none of them are. (In the future Argument |
| 346 | Clinic may relax this restriction.) |
| 347 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 348 | Sample:: |
| 349 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 350 | /*[clinic input] |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 351 | module _pickle |
| 352 | class _pickle.Pickler "PicklerObject *" "&Pickler_Type" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 353 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 354 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 355 | /*[clinic input] |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 356 | _pickle.Pickler.dump |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 357 | |
| 358 | obj: 'O' |
| 359 | / |
| 360 | |
| 361 | Write a pickled representation of obj to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 362 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 363 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 364 | 11. It's helpful to write a per-parameter docstring for each parameter. |
| 365 | But per-parameter docstrings are optional; you can skip this step |
| 366 | if you prefer. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 367 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 368 | Here's how to add a per-parameter docstring. The first line |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 369 | of the per-parameter docstring must be indented further than the |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 370 | parameter definition. The left margin of this first line establishes |
| 371 | the left margin for the whole per-parameter docstring; all the text |
| 372 | you write will be outdented by this amount. You can write as much |
| 373 | text as you like, across multiple lines if you wish. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 374 | |
| 375 | Sample:: |
| 376 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 377 | /*[clinic input] |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 378 | module _pickle |
| 379 | class _pickle.Pickler "PicklerObject *" "&Pickler_Type" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 380 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 381 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 382 | /*[clinic input] |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 383 | _pickle.Pickler.dump |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 384 | |
| 385 | obj: 'O' |
| 386 | The object to be pickled. |
| 387 | / |
| 388 | |
| 389 | Write a pickled representation of obj to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 390 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 391 | |
Martin Panter | a277c13 | 2016-12-10 03:49:12 +0000 | [diff] [blame] | 392 | 12. Save and close the file, then run ``Tools/clinic/clinic.py`` on |
| 393 | it. With luck everything worked---your block now has output, and |
| 394 | a ``.c.h`` file has been generated! Reopen the file in your |
Martin Panter | cfa9bad | 2016-12-10 04:10:45 +0000 | [diff] [blame] | 395 | text editor to see:: |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 396 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 397 | /*[clinic input] |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 398 | _pickle.Pickler.dump |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 399 | |
| 400 | obj: 'O' |
| 401 | The object to be pickled. |
| 402 | / |
| 403 | |
| 404 | Write a pickled representation of obj to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 405 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 406 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 407 | static PyObject * |
Martin Panter | a277c13 | 2016-12-10 03:49:12 +0000 | [diff] [blame] | 408 | _pickle_Pickler_dump(PicklerObject *self, PyObject *obj) |
| 409 | /*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 410 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 411 | Obviously, if Argument Clinic didn't produce any output, it's because |
| 412 | it found an error in your input. Keep fixing your errors and retrying |
| 413 | until Argument Clinic processes your file without complaint. |
| 414 | |
Martin Panter | a277c13 | 2016-12-10 03:49:12 +0000 | [diff] [blame] | 415 | For readability, most of the glue code has been generated to a ``.c.h`` |
| 416 | file. You'll need to include that in your original ``.c`` file, |
Martin Panter | cfa9bad | 2016-12-10 04:10:45 +0000 | [diff] [blame] | 417 | typically right after the clinic module block:: |
Martin Panter | a277c13 | 2016-12-10 03:49:12 +0000 | [diff] [blame] | 418 | |
| 419 | #include "clinic/_pickle.c.h" |
| 420 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 421 | 13. Double-check that the argument-parsing code Argument Clinic generated |
| 422 | looks basically the same as the existing code. |
| 423 | |
| 424 | First, ensure both places use the same argument-parsing function. |
| 425 | The existing code must call either |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 426 | :c:func:`PyArg_ParseTuple` or :c:func:`PyArg_ParseTupleAndKeywords`; |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 427 | ensure that the code generated by Argument Clinic calls the |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 428 | *exact* same function. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 429 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 430 | Second, the format string passed in to :c:func:`PyArg_ParseTuple` or |
| 431 | :c:func:`PyArg_ParseTupleAndKeywords` should be *exactly* the same |
| 432 | as the hand-written one in the existing function, up to the colon |
| 433 | or semi-colon. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 434 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 435 | (Argument Clinic always generates its format strings |
| 436 | with a ``:`` followed by the name of the function. If the |
| 437 | existing code's format string ends with ``;``, to provide |
Martin Panter | 357ed2e | 2016-11-21 00:15:20 +0000 | [diff] [blame] | 438 | usage help, this change is harmless—don't worry about it.) |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 439 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 440 | Third, for parameters whose format units require two arguments |
| 441 | (like a length variable, or an encoding string, or a pointer |
| 442 | to a conversion function), ensure that the second argument is |
| 443 | *exactly* the same between the two invocations. |
| 444 | |
| 445 | Fourth, inside the output portion of the block you'll find a preprocessor |
| 446 | macro defining the appropriate static :c:type:`PyMethodDef` structure for |
| 447 | this builtin:: |
| 448 | |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 449 | #define __PICKLE_PICKLER_DUMP_METHODDEF \ |
| 450 | {"dump", (PyCFunction)__pickle_Pickler_dump, METH_O, __pickle_Pickler_dump__doc__}, |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 451 | |
| 452 | This static structure should be *exactly* the same as the existing static |
| 453 | :c:type:`PyMethodDef` structure for this builtin. |
| 454 | |
| 455 | If any of these items differ in *any way*, |
| 456 | adjust your Argument Clinic function specification and rerun |
| 457 | ``Tools/clinic/clinic.py`` until they *are* the same. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 458 | |
| 459 | |
| 460 | 14. Notice that the last line of its output is the declaration |
| 461 | of your "impl" function. This is where the builtin's implementation goes. |
| 462 | Delete the existing prototype of the function you're modifying, but leave |
| 463 | the opening curly brace. Now delete its argument parsing code and the |
| 464 | declarations of all the variables it dumps the arguments into. |
| 465 | Notice how the Python arguments are now arguments to this impl function; |
| 466 | if the implementation used different names for these variables, fix it. |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 467 | |
| 468 | Let's reiterate, just because it's kind of weird. Your code should now |
| 469 | look like this:: |
| 470 | |
| 471 | static return_type |
| 472 | your_function_impl(...) |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 473 | /*[clinic end generated code: checksum=...]*/ |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 474 | { |
| 475 | ... |
| 476 | |
| 477 | Argument Clinic generated the checksum line and the function prototype just |
| 478 | above it. You should write the opening (and closing) curly braces for the |
| 479 | function, and the implementation inside. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 480 | |
| 481 | Sample:: |
| 482 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 483 | /*[clinic input] |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 484 | module _pickle |
| 485 | class _pickle.Pickler "PicklerObject *" "&Pickler_Type" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 486 | [clinic start generated code]*/ |
| 487 | /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 488 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 489 | /*[clinic input] |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 490 | _pickle.Pickler.dump |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 491 | |
| 492 | obj: 'O' |
| 493 | The object to be pickled. |
| 494 | / |
| 495 | |
| 496 | Write a pickled representation of obj to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 497 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 498 | |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 499 | PyDoc_STRVAR(__pickle_Pickler_dump__doc__, |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 500 | "Write a pickled representation of obj to the open file.\n" |
| 501 | "\n" |
| 502 | ... |
| 503 | static PyObject * |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 504 | _pickle_Pickler_dump_impl(PicklerObject *self, PyObject *obj) |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 505 | /*[clinic end generated code: checksum=3bd30745bf206a48f8b576a1da3d90f55a0a4187]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 506 | { |
| 507 | /* Check whether the Pickler was initialized correctly (issue3664). |
| 508 | Developers often forget to call __init__() in their subclasses, which |
| 509 | would trigger a segfault without this check. */ |
| 510 | if (self->write == NULL) { |
| 511 | PyErr_Format(PicklingError, |
| 512 | "Pickler.__init__() was not called by %s.__init__()", |
| 513 | Py_TYPE(self)->tp_name); |
| 514 | return NULL; |
| 515 | } |
| 516 | |
| 517 | if (_Pickler_ClearBuffer(self) < 0) |
| 518 | return NULL; |
| 519 | |
| 520 | ... |
| 521 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 522 | 15. Remember the macro with the :c:type:`PyMethodDef` structure for this |
| 523 | function? Find the existing :c:type:`PyMethodDef` structure for this |
| 524 | function and replace it with a reference to the macro. (If the builtin |
| 525 | is at module scope, this will probably be very near the end of the file; |
| 526 | if the builtin is a class method, this will probably be below but relatively |
| 527 | near to the implementation.) |
| 528 | |
| 529 | Note that the body of the macro contains a trailing comma. So when you |
| 530 | replace the existing static :c:type:`PyMethodDef` structure with the macro, |
| 531 | *don't* add a comma to the end. |
| 532 | |
| 533 | Sample:: |
| 534 | |
| 535 | static struct PyMethodDef Pickler_methods[] = { |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 536 | __PICKLE_PICKLER_DUMP_METHODDEF |
| 537 | __PICKLE_PICKLER_CLEAR_MEMO_METHODDEF |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 538 | {NULL, NULL} /* sentinel */ |
| 539 | }; |
| 540 | |
| 541 | |
| 542 | 16. Compile, then run the relevant portions of the regression-test suite. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 543 | This change should not introduce any new compile-time warnings or errors, |
| 544 | and there should be no externally-visible change to Python's behavior. |
| 545 | |
| 546 | Well, except for one difference: ``inspect.signature()`` run on your function |
| 547 | should now provide a valid signature! |
| 548 | |
| 549 | Congratulations, you've ported your first function to work with Argument Clinic! |
| 550 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 551 | Advanced Topics |
| 552 | =============== |
| 553 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 554 | Now that you've had some experience working with Argument Clinic, it's time |
| 555 | for some advanced topics. |
| 556 | |
| 557 | |
| 558 | Symbolic default values |
| 559 | ----------------------- |
| 560 | |
| 561 | The default value you provide for a parameter can't be any arbitrary |
| 562 | expression. Currently the following are explicitly supported: |
| 563 | |
| 564 | * Numeric constants (integer and float) |
| 565 | * String constants |
| 566 | * ``True``, ``False``, and ``None`` |
| 567 | * Simple symbolic constants like ``sys.maxsize``, which must |
| 568 | start with the name of the module |
| 569 | |
| 570 | In case you're curious, this is implemented in ``from_builtin()`` |
| 571 | in ``Lib/inspect.py``. |
| 572 | |
| 573 | (In the future, this may need to get even more elaborate, |
| 574 | to allow full expressions like ``CONSTANT - 1``.) |
| 575 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 576 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 577 | Renaming the C functions and variables generated by Argument Clinic |
| 578 | ------------------------------------------------------------------- |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 579 | |
| 580 | Argument Clinic automatically names the functions it generates for you. |
| 581 | Occasionally this may cause a problem, if the generated name collides with |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 582 | the name of an existing C function. There's an easy solution: override the names |
| 583 | used for the C functions. Just add the keyword ``"as"`` |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 584 | 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] | 585 | Argument Clinic will use that function name for the base (generated) function, |
| 586 | 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] | 587 | |
| 588 | For example, if we wanted to rename the C function names generated for |
| 589 | ``pickle.Pickler.dump``, it'd look like this:: |
| 590 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 591 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 592 | pickle.Pickler.dump as pickler_dumper |
| 593 | |
| 594 | ... |
| 595 | |
| 596 | The base function would now be named ``pickler_dumper()``, |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 597 | and the impl function would now be named ``pickler_dumper_impl()``. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 598 | |
| 599 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 600 | Similarly, you may have a problem where you want to give a parameter |
| 601 | a specific Python name, but that name may be inconvenient in C. Argument |
| 602 | Clinic allows you to give a parameter different names in Python and in C, |
| 603 | using the same ``"as"`` syntax:: |
| 604 | |
| 605 | /*[clinic input] |
| 606 | pickle.Pickler.dump |
| 607 | |
| 608 | obj: object |
| 609 | file as file_obj: object |
| 610 | protocol: object = NULL |
| 611 | * |
| 612 | fix_imports: bool = True |
| 613 | |
| 614 | Here, the name used in Python (in the signature and the ``keywords`` |
| 615 | array) would be ``file``, but the C variable would be named ``file_obj``. |
| 616 | |
| 617 | You can use this to rename the ``self`` parameter too! |
| 618 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 619 | |
| 620 | Converting functions using PyArg_UnpackTuple |
| 621 | -------------------------------------------- |
| 622 | |
| 623 | To convert a function parsing its arguments with :c:func:`PyArg_UnpackTuple`, |
| 624 | simply write out all the arguments, specifying each as an ``object``. You |
| 625 | may specify the ``type`` argument to cast the type as appropriate. All |
| 626 | arguments should be marked positional-only (add a ``/`` on a line by itself |
| 627 | after the last argument). |
| 628 | |
| 629 | Currently the generated code will use :c:func:`PyArg_ParseTuple`, but this |
| 630 | will change soon. |
| 631 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 632 | Optional Groups |
| 633 | --------------- |
| 634 | |
| 635 | Some legacy functions have a tricky approach to parsing their arguments: |
| 636 | they count the number of positional arguments, then use a ``switch`` statement |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 637 | 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] | 638 | how many positional arguments there are. (These functions cannot accept |
| 639 | keyword-only arguments.) This approach was used to simulate optional |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 640 | arguments back before :c:func:`PyArg_ParseTupleAndKeywords` was created. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 641 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 642 | While functions using this approach can often be converted to |
| 643 | use :c:func:`PyArg_ParseTupleAndKeywords`, optional arguments, and default values, |
| 644 | it's not always possible. Some of these legacy functions have |
| 645 | behaviors :c:func:`PyArg_ParseTupleAndKeywords` doesn't directly support. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 646 | The most obvious example is the builtin function ``range()``, which has |
| 647 | an optional argument on the *left* side of its required argument! |
| 648 | Another example is ``curses.window.addch()``, which has a group of two |
| 649 | arguments that must always be specified together. (The arguments are |
| 650 | called ``x`` and ``y``; if you call the function passing in ``x``, |
Martin Panter | 357ed2e | 2016-11-21 00:15:20 +0000 | [diff] [blame] | 651 | you must also pass in ``y``—and if you don't pass in ``x`` you may not |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 652 | pass in ``y`` either.) |
| 653 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 654 | In any case, the goal of Argument Clinic is to support argument parsing |
| 655 | for all existing CPython builtins without changing their semantics. |
| 656 | Therefore Argument Clinic supports |
| 657 | this alternate approach to parsing, using what are called *optional groups*. |
| 658 | 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] | 659 | They can be to the left or the right of the required arguments. They |
| 660 | can *only* be used with positional-only parameters. |
| 661 | |
Larry Hastings | 42d9e1b | 2014-01-22 05:49:11 -0800 | [diff] [blame] | 662 | .. note:: Optional groups are *only* intended for use when converting |
| 663 | functions that make multiple calls to :c:func:`PyArg_ParseTuple`! |
| 664 | Functions that use *any* other approach for parsing arguments |
| 665 | should *almost never* be converted to Argument Clinic using |
| 666 | optional groups. Functions using optional groups currently |
Martin Panter | b4a2b36 | 2016-08-12 12:02:03 +0000 | [diff] [blame] | 667 | cannot have accurate signatures in Python, because Python just |
Larry Hastings | 42d9e1b | 2014-01-22 05:49:11 -0800 | [diff] [blame] | 668 | doesn't understand the concept. Please avoid using optional |
| 669 | groups wherever possible. |
| 670 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 671 | 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] | 672 | the parameters you wish to group together, and a ``]`` on a line by itself |
| 673 | after these parameters. As an example, here's how ``curses.window.addch`` |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 674 | uses optional groups to make the first two parameters and the last |
| 675 | parameter optional:: |
| 676 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 677 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 678 | |
| 679 | curses.window.addch |
| 680 | |
| 681 | [ |
| 682 | x: int |
| 683 | X-coordinate. |
| 684 | y: int |
| 685 | Y-coordinate. |
| 686 | ] |
| 687 | |
| 688 | ch: object |
| 689 | Character to add. |
| 690 | |
| 691 | [ |
| 692 | attr: long |
| 693 | Attributes for the character. |
| 694 | ] |
| 695 | / |
| 696 | |
| 697 | ... |
| 698 | |
| 699 | |
| 700 | Notes: |
| 701 | |
| 702 | * For every optional group, one additional parameter will be passed into the |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 703 | impl function representing the group. The parameter will be an int named |
| 704 | ``group_{direction}_{number}``, |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 705 | where ``{direction}`` is either ``right`` or ``left`` depending on whether the group |
| 706 | is before or after the required parameters, and ``{number}`` is a monotonically |
| 707 | increasing number (starting at 1) indicating how far away the group is from |
| 708 | the required parameters. When the impl is called, this parameter will be set |
| 709 | to zero if this group was unused, and set to non-zero if this group was used. |
| 710 | (By used or unused, I mean whether or not the parameters received arguments |
| 711 | in this invocation.) |
| 712 | |
| 713 | * If there are no required arguments, the optional groups will behave |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 714 | as if they're to the right of the required arguments. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 715 | |
| 716 | * In the case of ambiguity, the argument parsing code |
| 717 | favors parameters on the left (before the required parameters). |
| 718 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 719 | * Optional groups can only contain positional-only parameters. |
| 720 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 721 | * Optional groups are *only* intended for legacy code. Please do not |
| 722 | use optional groups for new code. |
| 723 | |
| 724 | |
| 725 | Using real Argument Clinic converters, instead of "legacy converters" |
| 726 | --------------------------------------------------------------------- |
| 727 | |
| 728 | To save time, and to minimize how much you need to learn |
| 729 | to achieve your first port to Argument Clinic, the walkthrough above tells |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 730 | you to use "legacy converters". "Legacy converters" are a convenience, |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 731 | designed explicitly to make porting existing code to Argument Clinic |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 732 | easier. And to be clear, their use is acceptable when porting code for |
| 733 | Python 3.4. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 734 | |
| 735 | However, in the long term we probably want all our blocks to |
| 736 | use Argument Clinic's real syntax for converters. Why? A couple |
| 737 | reasons: |
| 738 | |
| 739 | * The proper converters are far easier to read and clearer in their intent. |
| 740 | * There are some format units that are unsupported as "legacy converters", |
| 741 | because they require arguments, and the legacy converter syntax doesn't |
| 742 | support specifying arguments. |
| 743 | * 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] | 744 | restricted to what :c:func:`PyArg_ParseTuple` supports; this flexibility |
| 745 | won't be available to parameters using legacy converters. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 746 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 747 | Therefore, if you don't mind a little extra effort, please use the normal |
| 748 | converters instead of legacy converters. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 749 | |
| 750 | In a nutshell, the syntax for Argument Clinic (non-legacy) converters |
| 751 | looks like a Python function call. However, if there are no explicit |
| 752 | arguments to the function (all functions take their default values), |
| 753 | you may omit the parentheses. Thus ``bool`` and ``bool()`` are exactly |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 754 | the same converters. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 755 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 756 | All arguments to Argument Clinic converters are keyword-only. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 757 | All Argument Clinic converters accept the following arguments: |
| 758 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 759 | ``c_default`` |
| 760 | The default value for this parameter when defined in C. |
| 761 | Specifically, this will be the initializer for the variable declared |
| 762 | in the "parse function". See :ref:`the section on default values <default_values>` |
| 763 | for how to use this. |
| 764 | Specified as a string. |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 765 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 766 | ``annotation`` |
| 767 | The annotation value for this parameter. Not currently supported, |
Stéphane Wirtel | 12e696b | 2018-10-27 00:58:26 +0200 | [diff] [blame] | 768 | because :pep:`8` mandates that the Python library may not use |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 769 | annotations. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 770 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 771 | In addition, some converters accept additional arguments. Here is a list |
| 772 | of these arguments, along with their meanings: |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 773 | |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 774 | ``accept`` |
| 775 | A set of Python types (and possibly pseudo-types); |
| 776 | this restricts the allowable Python argument to values of these types. |
| 777 | (This is not a general-purpose facility; as a rule it only supports |
| 778 | specific lists of types as shown in the legacy converter table.) |
| 779 | |
| 780 | To accept ``None``, add ``NoneType`` to this set. |
| 781 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 782 | ``bitwise`` |
| 783 | Only supported for unsigned integers. The native integer value of this |
| 784 | Python argument will be written to the parameter without any range checking, |
| 785 | even for negative values. |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 786 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 787 | ``converter`` |
| 788 | Only supported by the ``object`` converter. Specifies the name of a |
| 789 | :ref:`C "converter function" <o_ampersand>` |
| 790 | to use to convert this object to a native type. |
| 791 | |
| 792 | ``encoding`` |
| 793 | Only supported for strings. Specifies the encoding to use when converting |
| 794 | this string from a Python str (Unicode) value into a C ``char *`` value. |
| 795 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 796 | |
| 797 | ``subclass_of`` |
| 798 | Only supported for the ``object`` converter. Requires that the Python |
| 799 | value be a subclass of a Python type, as expressed in C. |
| 800 | |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 801 | ``type`` |
| 802 | Only supported for the ``object`` and ``self`` converters. Specifies |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 803 | the C type that will be used to declare the variable. Default value is |
| 804 | ``"PyObject *"``. |
| 805 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 806 | ``zeroes`` |
| 807 | Only supported for strings. If true, embedded NUL bytes (``'\\0'``) are |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 808 | permitted inside the value. The length of the string will be passed in |
| 809 | to the impl function, just after the string parameter, as a parameter named |
| 810 | ``<parameter_name>_length``. |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 811 | |
| 812 | Please note, not every possible combination of arguments will work. |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 813 | Usually these arguments are implemented by specific ``PyArg_ParseTuple`` |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 814 | *format units*, with specific behavior. For example, currently you cannot |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 815 | call ``unsigned_short`` without also specifying ``bitwise=True``. |
| 816 | Although it's perfectly reasonable to think this would work, these semantics don't |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 817 | map to any existing format unit. So Argument Clinic doesn't support it. (Or, at |
| 818 | least, not yet.) |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 819 | |
| 820 | Below is a table showing the mapping of legacy converters into real |
| 821 | Argument Clinic converters. On the left is the legacy converter, |
| 822 | on the right is the text you'd replace it with. |
| 823 | |
| 824 | ========= ================================================================================= |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 825 | ``'B'`` ``unsigned_char(bitwise=True)`` |
| 826 | ``'b'`` ``unsigned_char`` |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 827 | ``'c'`` ``char`` |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 828 | ``'C'`` ``int(accept={str})`` |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 829 | ``'d'`` ``double`` |
| 830 | ``'D'`` ``Py_complex`` |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 831 | ``'es'`` ``str(encoding='name_of_encoding')`` |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 832 | ``'es#'`` ``str(encoding='name_of_encoding', zeroes=True)`` |
| 833 | ``'et'`` ``str(encoding='name_of_encoding', accept={bytes, bytearray, str})`` |
| 834 | ``'et#'`` ``str(encoding='name_of_encoding', accept={bytes, bytearray, str}, zeroes=True)`` |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 835 | ``'f'`` ``float`` |
| 836 | ``'h'`` ``short`` |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 837 | ``'H'`` ``unsigned_short(bitwise=True)`` |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 838 | ``'i'`` ``int`` |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 839 | ``'I'`` ``unsigned_int(bitwise=True)`` |
| 840 | ``'k'`` ``unsigned_long(bitwise=True)`` |
Benjamin Peterson | cc85449 | 2016-09-08 09:29:11 -0700 | [diff] [blame] | 841 | ``'K'`` ``unsigned_long_long(bitwise=True)`` |
Tal Einat | 97fceee | 2015-05-16 14:12:15 +0300 | [diff] [blame] | 842 | ``'l'`` ``long`` |
Benjamin Peterson | cc85449 | 2016-09-08 09:29:11 -0700 | [diff] [blame] | 843 | ``'L'`` ``long long`` |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 844 | ``'n'`` ``Py_ssize_t`` |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 845 | ``'O'`` ``object`` |
Larry Hastings | 77561cc | 2014-01-07 12:13:13 -0800 | [diff] [blame] | 846 | ``'O!'`` ``object(subclass_of='&PySomething_Type')`` |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 847 | ``'O&'`` ``object(converter='name_of_c_function')`` |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 848 | ``'p'`` ``bool`` |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 849 | ``'S'`` ``PyBytesObject`` |
| 850 | ``'s'`` ``str`` |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 851 | ``'s#'`` ``str(zeroes=True)`` |
| 852 | ``'s*'`` ``Py_buffer(accept={buffer, str})`` |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 853 | ``'U'`` ``unicode`` |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 854 | ``'u'`` ``Py_UNICODE`` |
| 855 | ``'u#'`` ``Py_UNICODE(zeroes=True)`` |
| 856 | ``'w*'`` ``Py_buffer(accept={rwbuffer})`` |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 857 | ``'Y'`` ``PyByteArrayObject`` |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 858 | ``'y'`` ``str(accept={bytes})`` |
| 859 | ``'y#'`` ``str(accept={robuffer}, zeroes=True)`` |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 860 | ``'y*'`` ``Py_buffer`` |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 861 | ``'Z'`` ``Py_UNICODE(accept={str, NoneType})`` |
| 862 | ``'Z#'`` ``Py_UNICODE(accept={str, NoneType}, zeroes=True)`` |
| 863 | ``'z'`` ``str(accept={str, NoneType})`` |
| 864 | ``'z#'`` ``str(accept={str, NoneType}, zeroes=True)`` |
| 865 | ``'z*'`` ``Py_buffer(accept={buffer, str, NoneType})`` |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 866 | ========= ================================================================================= |
| 867 | |
| 868 | As an example, here's our sample ``pickle.Pickler.dump`` using the proper |
| 869 | converter:: |
| 870 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 871 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 872 | pickle.Pickler.dump |
| 873 | |
| 874 | obj: object |
| 875 | The object to be pickled. |
| 876 | / |
| 877 | |
| 878 | Write a pickled representation of obj to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 879 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 880 | |
Serhiy Storchaka | 7cb7bcf | 2018-07-26 13:22:16 +0300 | [diff] [blame] | 881 | One advantage of real converters is that they're more flexible than legacy |
| 882 | converters. For example, the ``unsigned_int`` converter (and all the |
| 883 | ``unsigned_`` converters) can be specified without ``bitwise=True``. Their |
| 884 | default behavior performs range checking on the value, and they won't accept |
| 885 | negative numbers. You just can't do that with a legacy converter! |
| 886 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 887 | Argument Clinic will show you all the converters it has |
| 888 | available. For each converter it'll show you all the parameters |
| 889 | it accepts, along with the default value for each parameter. |
| 890 | Just run ``Tools/clinic/clinic.py --converters`` to see the full list. |
| 891 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 892 | Py_buffer |
| 893 | --------- |
| 894 | |
| 895 | When using the ``Py_buffer`` converter |
Larry Hastings | 0191be3 | 2014-01-12 13:57:36 -0800 | [diff] [blame] | 896 | (or the ``'s*'``, ``'w*'``, ``'*y'``, or ``'z*'`` legacy converters), |
| 897 | you *must* not call :c:func:`PyBuffer_Release` on the provided buffer. |
| 898 | Argument Clinic generates code that does it for you (in the parsing function). |
| 899 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 900 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 901 | |
| 902 | Advanced converters |
| 903 | ------------------- |
| 904 | |
Donald Stufft | 8b852f1 | 2014-05-20 12:58:38 -0400 | [diff] [blame] | 905 | Remember those format units you skipped for your first |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 906 | time because they were advanced? Here's how to handle those too. |
| 907 | |
Martin Panter | 357ed2e | 2016-11-21 00:15:20 +0000 | [diff] [blame] | 908 | The trick is, all those format units take arguments—either |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 909 | conversion functions, or types, or strings specifying an encoding. |
| 910 | (But "legacy converters" don't support arguments. That's why we |
| 911 | skipped them for your first function.) The argument you specified |
| 912 | to the format unit is now an argument to the converter; this |
Larry Hastings | 77561cc | 2014-01-07 12:13:13 -0800 | [diff] [blame] | 913 | argument is either ``converter`` (for ``O&``), ``subclass_of`` (for ``O!``), |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 914 | or ``encoding`` (for all the format units that start with ``e``). |
| 915 | |
Larry Hastings | 77561cc | 2014-01-07 12:13:13 -0800 | [diff] [blame] | 916 | When using ``subclass_of``, you may also want to use the other |
| 917 | custom argument for ``object()``: ``type``, which lets you set the type |
| 918 | actually used for the parameter. For example, if you want to ensure |
| 919 | that the object is a subclass of ``PyUnicode_Type``, you probably want |
| 920 | to use the converter ``object(type='PyUnicodeObject *', subclass_of='&PyUnicode_Type')``. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 921 | |
Larry Hastings | 77561cc | 2014-01-07 12:13:13 -0800 | [diff] [blame] | 922 | One possible problem with using Argument Clinic: it takes away some possible |
| 923 | flexibility for the format units starting with ``e``. When writing a |
| 924 | ``PyArg_Parse`` call by hand, you could theoretically decide at runtime what |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 925 | 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] | 926 | be hard-coded at Argument-Clinic-preprocessing-time. This limitation is deliberate; |
| 927 | it made supporting this format unit much easier, and may allow for future optimizations. |
| 928 | This restriction doesn't seem unreasonable; CPython itself always passes in static |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 929 | hard-coded encoding strings for parameters whose format units start with ``e``. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 930 | |
| 931 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 932 | .. _default_values: |
| 933 | |
| 934 | Parameter default values |
| 935 | ------------------------ |
| 936 | |
| 937 | Default values for parameters can be any of a number of values. |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 938 | At their simplest, they can be string, int, or float literals: |
| 939 | |
| 940 | .. code-block:: none |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 941 | |
| 942 | foo: str = "abc" |
| 943 | bar: int = 123 |
| 944 | bat: float = 45.6 |
| 945 | |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 946 | They can also use any of Python's built-in constants: |
| 947 | |
| 948 | .. code-block:: none |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 949 | |
| 950 | yep: bool = True |
| 951 | nope: bool = False |
| 952 | nada: object = None |
| 953 | |
| 954 | There's also special support for a default value of ``NULL``, and |
| 955 | for simple expressions, documented in the following sections. |
| 956 | |
| 957 | |
| 958 | The ``NULL`` default value |
| 959 | -------------------------- |
| 960 | |
| 961 | For string and object parameters, you can set them to ``None`` to indicate |
| 962 | that there's no default. However, that means the C variable will be |
| 963 | initialized to ``Py_None``. For convenience's sakes, there's a special |
| 964 | value called ``NULL`` for just this reason: from Python's perspective it |
| 965 | behaves like a default value of ``None``, but the C variable is initialized |
| 966 | with ``NULL``. |
| 967 | |
| 968 | Expressions specified as default values |
| 969 | --------------------------------------- |
| 970 | |
| 971 | The default value for a parameter can be more than just a literal value. |
| 972 | It can be an entire expression, using math operators and looking up attributes |
| 973 | on objects. However, this support isn't exactly simple, because of some |
| 974 | non-obvious semantics. |
| 975 | |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 976 | Consider the following example: |
| 977 | |
| 978 | .. code-block:: none |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 979 | |
| 980 | foo: Py_ssize_t = sys.maxsize - 1 |
| 981 | |
| 982 | ``sys.maxsize`` can have different values on different platforms. Therefore |
| 983 | Argument Clinic can't simply evaluate that expression locally and hard-code it |
| 984 | in C. So it stores the default in such a way that it will get evaluated at |
| 985 | runtime, when the user asks for the function's signature. |
| 986 | |
| 987 | What namespace is available when the expression is evaluated? It's evaluated |
| 988 | in the context of the module the builtin came from. So, if your module has an |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 989 | attribute called "``max_widgets``", you may simply use it: |
| 990 | |
| 991 | .. code-block:: none |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 992 | |
| 993 | foo: Py_ssize_t = max_widgets |
| 994 | |
| 995 | If the symbol isn't found in the current module, it fails over to looking in |
| 996 | ``sys.modules``. That's how it can find ``sys.maxsize`` for example. (Since you |
| 997 | don't know in advance what modules the user will load into their interpreter, |
| 998 | it's best to restrict yourself to modules that are preloaded by Python itself.) |
| 999 | |
| 1000 | Evaluating default values only at runtime means Argument Clinic can't compute |
| 1001 | the correct equivalent C default value. So you need to tell it explicitly. |
| 1002 | When you use an expression, you must also specify the equivalent expression |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 1003 | in C, using the ``c_default`` parameter to the converter: |
| 1004 | |
| 1005 | .. code-block:: none |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 1006 | |
| 1007 | foo: Py_ssize_t(c_default="PY_SSIZE_T_MAX - 1") = sys.maxsize - 1 |
| 1008 | |
| 1009 | Another complication: Argument Clinic can't know in advance whether or not the |
| 1010 | expression you supply is valid. It parses it to make sure it looks legal, but |
| 1011 | it can't *actually* know. You must be very careful when using expressions to |
| 1012 | specify values that are guaranteed to be valid at runtime! |
| 1013 | |
| 1014 | Finally, because expressions must be representable as static C values, there |
| 1015 | are many restrictions on legal expressions. Here's a list of Python features |
| 1016 | you're not permitted to use: |
| 1017 | |
| 1018 | * Function calls. |
| 1019 | * Inline if statements (``3 if foo else 5``). |
| 1020 | * Automatic sequence unpacking (``*[1, 2, 3]``). |
| 1021 | * List/set/dict comprehensions and generator expressions. |
| 1022 | * Tuple/list/set/dict literals. |
| 1023 | |
| 1024 | |
| 1025 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1026 | Using a return converter |
| 1027 | ------------------------ |
| 1028 | |
| 1029 | By default the impl function Argument Clinic generates for you returns ``PyObject *``. |
| 1030 | But your C function often computes some C type, then converts it into the ``PyObject *`` |
| 1031 | at the last moment. Argument Clinic handles converting your inputs from Python types |
Martin Panter | 357ed2e | 2016-11-21 00:15:20 +0000 | [diff] [blame] | 1032 | into native C types—why not have it convert your return value from a native C type |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1033 | into a Python type too? |
| 1034 | |
| 1035 | That's what a "return converter" does. It changes your impl function to return |
| 1036 | some C type, then adds code to the generated (non-impl) function to handle converting |
| 1037 | that value into the appropriate ``PyObject *``. |
| 1038 | |
| 1039 | The syntax for return converters is similar to that of parameter converters. |
| 1040 | You specify the return converter like it was a return annotation on the |
| 1041 | function itself. Return converters behave much the same as parameter converters; |
| 1042 | they take arguments, the arguments are all keyword-only, and if you're not changing |
| 1043 | any of the default arguments you can omit the parentheses. |
| 1044 | |
| 1045 | (If you use both ``"as"`` *and* a return converter for your function, |
| 1046 | the ``"as"`` should come before the return converter.) |
| 1047 | |
| 1048 | There's one additional complication when using return converters: how do you |
Donald Stufft | 8b852f1 | 2014-05-20 12:58:38 -0400 | [diff] [blame] | 1049 | indicate an error has occurred? Normally, a function returns a valid (non-``NULL``) |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1050 | pointer for success, and ``NULL`` for failure. But if you use an integer return converter, |
| 1051 | all integers are valid. How can Argument Clinic detect an error? Its solution: each return |
| 1052 | converter implicitly looks for a special value that indicates an error. If you return |
| 1053 | that value, and an error has been set (``PyErr_Occurred()`` returns a true |
Donald Stufft | 8b852f1 | 2014-05-20 12:58:38 -0400 | [diff] [blame] | 1054 | value), then the generated code will propagate the error. Otherwise it will |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1055 | encode the value you return like normal. |
| 1056 | |
Martin Panter | cfa9bad | 2016-12-10 04:10:45 +0000 | [diff] [blame] | 1057 | Currently Argument Clinic supports only a few return converters: |
| 1058 | |
| 1059 | .. code-block:: none |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1060 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 1061 | bool |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1062 | int |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 1063 | unsigned int |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1064 | long |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 1065 | unsigned int |
| 1066 | size_t |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1067 | Py_ssize_t |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 1068 | float |
| 1069 | double |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1070 | DecodeFSDefault |
| 1071 | |
| 1072 | None of these take parameters. For the first three, return -1 to indicate |
Serhiy Storchaka | 84b8e92 | 2017-03-30 10:01:03 +0300 | [diff] [blame] | 1073 | error. For ``DecodeFSDefault``, the return type is ``const char *``; return a NULL |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1074 | pointer to indicate an error. |
| 1075 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 1076 | (There's also an experimental ``NoneType`` converter, which lets you |
| 1077 | return ``Py_None`` on success or ``NULL`` on failure, without having |
| 1078 | to increment the reference count on ``Py_None``. I'm not sure it adds |
| 1079 | enough clarity to be worth using.) |
| 1080 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 1081 | To see all the return converters Argument Clinic supports, along with |
| 1082 | their parameters (if any), |
| 1083 | just run ``Tools/clinic/clinic.py --converters`` for the full list. |
| 1084 | |
| 1085 | |
Larry Hastings | 4a714d4 | 2014-01-14 22:22:41 -0800 | [diff] [blame] | 1086 | Cloning existing functions |
| 1087 | -------------------------- |
| 1088 | |
| 1089 | If you have a number of functions that look similar, you may be able to |
| 1090 | use Clinic's "clone" feature. When you clone an existing function, |
| 1091 | you reuse: |
| 1092 | |
| 1093 | * its parameters, including |
| 1094 | |
| 1095 | * their names, |
| 1096 | |
| 1097 | * their converters, with all parameters, |
| 1098 | |
| 1099 | * their default values, |
| 1100 | |
| 1101 | * their per-parameter docstrings, |
| 1102 | |
| 1103 | * their *kind* (whether they're positional only, |
| 1104 | positional or keyword, or keyword only), and |
| 1105 | |
| 1106 | * its return converter. |
| 1107 | |
| 1108 | The only thing not copied from the original function is its docstring; |
| 1109 | the syntax allows you to specify a new docstring. |
| 1110 | |
| 1111 | Here's the syntax for cloning a function:: |
| 1112 | |
| 1113 | /*[clinic input] |
| 1114 | module.class.new_function [as c_basename] = module.class.existing_function |
| 1115 | |
| 1116 | Docstring for new_function goes here. |
| 1117 | [clinic start generated code]*/ |
| 1118 | |
| 1119 | (The functions can be in different modules or classes. I wrote |
| 1120 | ``module.class`` in the sample just to illustrate that you must |
| 1121 | use the full path to *both* functions.) |
| 1122 | |
| 1123 | Sorry, there's no syntax for partially-cloning a function, or cloning a function |
| 1124 | then modifying it. Cloning is an all-or nothing proposition. |
| 1125 | |
| 1126 | Also, the function you are cloning from must have been previously defined |
| 1127 | in the current file. |
| 1128 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1129 | Calling Python code |
| 1130 | ------------------- |
| 1131 | |
| 1132 | The rest of the advanced topics require you to write Python code |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 1133 | which lives inside your C file and modifies Argument Clinic's |
| 1134 | runtime state. This is simple: you simply define a Python block. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1135 | |
| 1136 | A Python block uses different delimiter lines than an Argument |
| 1137 | Clinic function block. It looks like this:: |
| 1138 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1139 | /*[python input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1140 | # python code goes here |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1141 | [python start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1142 | |
| 1143 | All the code inside the Python block is executed at the |
| 1144 | time it's parsed. All text written to stdout inside the block |
| 1145 | is redirected into the "output" after the block. |
| 1146 | |
| 1147 | As an example, here's a Python block that adds a static integer |
| 1148 | variable to the C code:: |
| 1149 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1150 | /*[python input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1151 | print('static int __ignored_unused_variable__ = 0;') |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1152 | [python start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1153 | static int __ignored_unused_variable__ = 0; |
| 1154 | /*[python checksum:...]*/ |
| 1155 | |
| 1156 | |
| 1157 | Using a "self converter" |
| 1158 | ------------------------ |
| 1159 | |
| 1160 | Argument Clinic automatically adds a "self" parameter for you |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 1161 | using a default converter. It automatically sets the ``type`` |
| 1162 | of this parameter to the "pointer to an instance" you specified |
| 1163 | when you declared the type. However, you can override |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1164 | Argument Clinic's converter and specify one yourself. |
| 1165 | Just add your own ``self`` parameter as the first parameter in a |
| 1166 | block, and ensure that its converter is an instance of |
| 1167 | ``self_converter`` or a subclass thereof. |
| 1168 | |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 1169 | What's the point? This lets you override the type of ``self``, |
| 1170 | or give it a different default name. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1171 | |
| 1172 | How do you specify the custom type you want to cast ``self`` to? |
| 1173 | If you only have one or two functions with the same type for ``self``, |
| 1174 | you can directly use Argument Clinic's existing ``self`` converter, |
| 1175 | passing in the type you want to use as the ``type`` parameter:: |
| 1176 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1177 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1178 | |
| 1179 | _pickle.Pickler.dump |
| 1180 | |
| 1181 | self: self(type="PicklerObject *") |
| 1182 | obj: object |
| 1183 | / |
| 1184 | |
| 1185 | Write a pickled representation of the given object to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1186 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1187 | |
| 1188 | On the other hand, if you have a lot of functions that will use the same |
| 1189 | type for ``self``, it's best to create your own converter, subclassing |
| 1190 | ``self_converter`` but overwriting the ``type`` member:: |
| 1191 | |
Zachary Ware | c1cb227 | 2014-01-09 21:41:23 -0600 | [diff] [blame] | 1192 | /*[python input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1193 | class PicklerObject_converter(self_converter): |
| 1194 | type = "PicklerObject *" |
Zachary Ware | c1cb227 | 2014-01-09 21:41:23 -0600 | [diff] [blame] | 1195 | [python start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1196 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1197 | /*[clinic input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1198 | |
| 1199 | _pickle.Pickler.dump |
| 1200 | |
| 1201 | self: PicklerObject |
| 1202 | obj: object |
| 1203 | / |
| 1204 | |
| 1205 | Write a pickled representation of the given object to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1206 | [clinic start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1207 | |
| 1208 | |
| 1209 | |
| 1210 | Writing a custom converter |
| 1211 | -------------------------- |
| 1212 | |
| 1213 | As we hinted at in the previous section... you can write your own converters! |
| 1214 | A converter is simply a Python class that inherits from ``CConverter``. |
| 1215 | The main purpose of a custom converter is if you have a parameter using |
Martin Panter | 357ed2e | 2016-11-21 00:15:20 +0000 | [diff] [blame] | 1216 | the ``O&`` format unit—parsing this parameter means calling |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 1217 | a :c:func:`PyArg_ParseTuple` "converter function". |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1218 | |
| 1219 | Your converter class should be named ``*something*_converter``. |
| 1220 | If the name follows this convention, then your converter class |
| 1221 | will be automatically registered with Argument Clinic; its name |
| 1222 | will be the name of your class with the ``_converter`` suffix |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 1223 | stripped off. (This is accomplished with a metaclass.) |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1224 | |
| 1225 | You shouldn't subclass ``CConverter.__init__``. Instead, you should |
| 1226 | write a ``converter_init()`` function. ``converter_init()`` |
| 1227 | always accepts a ``self`` parameter; after that, all additional |
| 1228 | parameters *must* be keyword-only. Any arguments passed in to |
| 1229 | the converter in Argument Clinic will be passed along to your |
| 1230 | ``converter_init()``. |
| 1231 | |
| 1232 | There are some additional members of ``CConverter`` you may wish |
| 1233 | to specify in your subclass. Here's the current list: |
| 1234 | |
| 1235 | ``type`` |
| 1236 | The C type to use for this variable. |
| 1237 | ``type`` should be a Python string specifying the type, e.g. ``int``. |
| 1238 | If this is a pointer type, the type string should end with ``' *'``. |
| 1239 | |
| 1240 | ``default`` |
| 1241 | The Python default value for this parameter, as a Python value. |
| 1242 | Or the magic value ``unspecified`` if there is no default. |
| 1243 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1244 | ``py_default`` |
| 1245 | ``default`` as it should appear in Python code, |
| 1246 | as a string. |
| 1247 | Or ``None`` if there is no default. |
| 1248 | |
| 1249 | ``c_default`` |
| 1250 | ``default`` as it should appear in C code, |
| 1251 | as a string. |
| 1252 | Or ``None`` if there is no default. |
| 1253 | |
| 1254 | ``c_ignored_default`` |
| 1255 | The default value used to initialize the C variable when |
| 1256 | there is no default, but not specifying a default may |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 1257 | result in an "uninitialized variable" warning. This can |
Martin Panter | 357ed2e | 2016-11-21 00:15:20 +0000 | [diff] [blame] | 1258 | easily happen when using option groups—although |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 1259 | properly-written code will never actually use this value, |
| 1260 | the variable does get passed in to the impl, and the |
| 1261 | C compiler will complain about the "use" of the |
| 1262 | uninitialized value. This value should always be a |
| 1263 | non-empty string. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1264 | |
| 1265 | ``converter`` |
| 1266 | The name of the C converter function, as a string. |
| 1267 | |
| 1268 | ``impl_by_reference`` |
| 1269 | A boolean value. If true, |
| 1270 | Argument Clinic will add a ``&`` in front of the name of |
| 1271 | the variable when passing it into the impl function. |
| 1272 | |
| 1273 | ``parse_by_reference`` |
| 1274 | A boolean value. If true, |
| 1275 | Argument Clinic will add a ``&`` in front of the name of |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 1276 | the variable when passing it into :c:func:`PyArg_ParseTuple`. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1277 | |
| 1278 | |
| 1279 | Here's the simplest example of a custom converter, from ``Modules/zlibmodule.c``:: |
| 1280 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1281 | /*[python input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1282 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1283 | class ssize_t_converter(CConverter): |
| 1284 | type = 'Py_ssize_t' |
| 1285 | converter = 'ssize_t_converter' |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1286 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1287 | [python start generated code]*/ |
Martin Panter | e99e977 | 2015-11-20 08:13:35 +0000 | [diff] [blame] | 1288 | /*[python end generated code: output=da39a3ee5e6b4b0d input=35521e4e733823c7]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1289 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1290 | This block adds a converter to Argument Clinic named ``ssize_t``. Parameters |
| 1291 | declared as ``ssize_t`` will be declared as type ``Py_ssize_t``, and will |
Martin Panter | e99e977 | 2015-11-20 08:13:35 +0000 | [diff] [blame] | 1292 | be parsed by the ``'O&'`` format unit, which will call the |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1293 | ``ssize_t_converter`` converter function. ``ssize_t`` variables |
Martin Panter | e99e977 | 2015-11-20 08:13:35 +0000 | [diff] [blame] | 1294 | automatically support default values. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1295 | |
| 1296 | More sophisticated custom converters can insert custom C code to |
| 1297 | handle initialization and cleanup. |
| 1298 | You can see more examples of custom converters in the CPython |
| 1299 | source tree; grep the C files for the string ``CConverter``. |
| 1300 | |
| 1301 | Writing a custom return converter |
| 1302 | --------------------------------- |
| 1303 | |
| 1304 | Writing a custom return converter is much like writing |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 1305 | a custom converter. Except it's somewhat simpler, because return |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1306 | converters are themselves much simpler. |
| 1307 | |
| 1308 | Return converters must subclass ``CReturnConverter``. |
| 1309 | There are no examples yet of custom return converters, |
| 1310 | because they are not widely used yet. If you wish to |
| 1311 | write your own return converter, please read ``Tools/clinic/clinic.py``, |
| 1312 | specifically the implementation of ``CReturnConverter`` and |
| 1313 | all its subclasses. |
| 1314 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 1315 | METH_O and METH_NOARGS |
| 1316 | ---------------------------------------------- |
| 1317 | |
| 1318 | To convert a function using ``METH_O``, make sure the function's |
| 1319 | single argument is using the ``object`` converter, and mark the |
| 1320 | arguments as positional-only:: |
| 1321 | |
| 1322 | /*[clinic input] |
| 1323 | meth_o_sample |
| 1324 | |
| 1325 | argument: object |
| 1326 | / |
| 1327 | [clinic start generated code]*/ |
| 1328 | |
| 1329 | |
| 1330 | To convert a function using ``METH_NOARGS``, just don't specify |
| 1331 | any arguments. |
| 1332 | |
| 1333 | You can still use a self converter, a return converter, and specify |
| 1334 | a ``type`` argument to the object converter for ``METH_O``. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1335 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 1336 | tp_new and tp_init functions |
| 1337 | ---------------------------------------------- |
| 1338 | |
Larry Hastings | 42d9e1b | 2014-01-22 05:49:11 -0800 | [diff] [blame] | 1339 | You can convert ``tp_new`` and ``tp_init`` functions. Just name |
| 1340 | them ``__new__`` or ``__init__`` as appropriate. Notes: |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 1341 | |
| 1342 | * The function name generated for ``__new__`` doesn't end in ``__new__`` |
| 1343 | like it would by default. It's just the name of the class, converted |
| 1344 | into a valid C identifier. |
| 1345 | |
| 1346 | * No ``PyMethodDef`` ``#define`` is generated for these functions. |
| 1347 | |
| 1348 | * ``__init__`` functions return ``int``, not ``PyObject *``. |
| 1349 | |
Larry Hastings | 42d9e1b | 2014-01-22 05:49:11 -0800 | [diff] [blame] | 1350 | * Use the docstring as the class docstring. |
| 1351 | |
| 1352 | * Although ``__new__`` and ``__init__`` functions must always |
| 1353 | accept both the ``args`` and ``kwargs`` objects, when converting |
| 1354 | you may specify any signature for these functions that you like. |
| 1355 | (If your function doesn't support keywords, the parsing function |
| 1356 | generated will throw an exception if it receives any.) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 1357 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1358 | Changing and redirecting Clinic's output |
| 1359 | ---------------------------------------- |
| 1360 | |
| 1361 | It can be inconvenient to have Clinic's output interspersed with |
| 1362 | your conventional hand-edited C code. Luckily, Clinic is configurable: |
| 1363 | you can buffer up its output for printing later (or earlier!), or write |
| 1364 | its output to a separate file. You can also add a prefix or suffix to |
| 1365 | every line of Clinic's generated output. |
| 1366 | |
| 1367 | While changing Clinic's output in this manner can be a boon to readability, |
| 1368 | it may result in Clinic code using types before they are defined, or |
Martin Panter | b4a2b36 | 2016-08-12 12:02:03 +0000 | [diff] [blame] | 1369 | your code attempting to use Clinic-generated code before it is defined. |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1370 | These problems can be easily solved by rearranging the declarations in your file, |
| 1371 | or moving where Clinic's generated code goes. (This is why the default behavior |
| 1372 | of Clinic is to output everything into the current block; while many people |
| 1373 | consider this hampers readability, it will never require rearranging your |
| 1374 | code to fix definition-before-use problems.) |
| 1375 | |
| 1376 | Let's start with defining some terminology: |
| 1377 | |
| 1378 | *field* |
| 1379 | A field, in this context, is a subsection of Clinic's output. |
| 1380 | For example, the ``#define`` for the ``PyMethodDef`` structure |
| 1381 | is a field, called ``methoddef_define``. Clinic has seven |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 1382 | different fields it can output per function definition: |
| 1383 | |
| 1384 | .. code-block:: none |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1385 | |
| 1386 | docstring_prototype |
| 1387 | docstring_definition |
| 1388 | methoddef_define |
| 1389 | impl_prototype |
| 1390 | parser_prototype |
| 1391 | parser_definition |
| 1392 | impl_definition |
| 1393 | |
| 1394 | All the names are of the form ``"<a>_<b>"``, |
| 1395 | where ``"<a>"`` is the semantic object represented (the parsing function, |
| 1396 | the impl function, the docstring, or the methoddef structure) and ``"<b>"`` |
| 1397 | represents what kind of statement the field is. Field names that end in |
| 1398 | ``"_prototype"`` |
| 1399 | represent forward declarations of that thing, without the actual body/data |
| 1400 | of the thing; field names that end in ``"_definition"`` represent the actual |
| 1401 | definition of the thing, with the body/data of the thing. (``"methoddef"`` |
| 1402 | is special, it's the only one that ends with ``"_define"``, representing that |
| 1403 | it's a preprocessor #define.) |
| 1404 | |
| 1405 | *destination* |
| 1406 | A destination is a place Clinic can write output to. There are |
| 1407 | five built-in destinations: |
| 1408 | |
| 1409 | ``block`` |
| 1410 | The default destination: printed in the output section of |
| 1411 | the current Clinic block. |
| 1412 | |
| 1413 | ``buffer`` |
| 1414 | A text buffer where you can save text for later. Text sent |
Martin Panter | b4a2b36 | 2016-08-12 12:02:03 +0000 | [diff] [blame] | 1415 | here is appended to the end of any existing text. It's an |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1416 | error to have any text left in the buffer when Clinic finishes |
| 1417 | processing a file. |
| 1418 | |
| 1419 | ``file`` |
| 1420 | A separate "clinic file" that will be created automatically by Clinic. |
| 1421 | The filename chosen for the file is ``{basename}.clinic{extension}``, |
| 1422 | where ``basename`` and ``extension`` were assigned the output |
| 1423 | from ``os.path.splitext()`` run on the current file. (Example: |
| 1424 | the ``file`` destination for ``_pickle.c`` would be written to |
| 1425 | ``_pickle.clinic.c``.) |
| 1426 | |
| 1427 | **Important: When using a** ``file`` **destination, you** |
| 1428 | *must check in* **the generated file!** |
| 1429 | |
| 1430 | ``two-pass`` |
| 1431 | A buffer like ``buffer``. However, a two-pass buffer can only |
gfyoung | ec19ba2 | 2017-06-06 15:23:52 -0400 | [diff] [blame] | 1432 | be dumped once, and it prints out all text sent to it during |
| 1433 | all processing, even from Clinic blocks *after* the dumping point. |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1434 | |
| 1435 | ``suppress`` |
Martin Panter | 357ed2e | 2016-11-21 00:15:20 +0000 | [diff] [blame] | 1436 | The text is suppressed—thrown away. |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1437 | |
| 1438 | |
| 1439 | Clinic defines five new directives that let you reconfigure its output. |
| 1440 | |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 1441 | The first new directive is ``dump``: |
| 1442 | |
| 1443 | .. code-block:: none |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1444 | |
| 1445 | dump <destination> |
| 1446 | |
| 1447 | This dumps the current contents of the named destination into the output of |
| 1448 | the current block, and empties it. This only works with ``buffer`` and |
| 1449 | ``two-pass`` destinations. |
| 1450 | |
| 1451 | The second new directive is ``output``. The most basic form of ``output`` |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 1452 | is like this: |
| 1453 | |
| 1454 | .. code-block:: none |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1455 | |
| 1456 | output <field> <destination> |
| 1457 | |
| 1458 | This tells Clinic to output *field* to *destination*. ``output`` also |
| 1459 | supports a special meta-destination, called ``everything``, which tells |
| 1460 | Clinic to output *all* fields to that *destination*. |
| 1461 | |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 1462 | ``output`` has a number of other functions: |
| 1463 | |
| 1464 | .. code-block:: none |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1465 | |
| 1466 | output push |
| 1467 | output pop |
| 1468 | output preset <preset> |
| 1469 | |
| 1470 | |
| 1471 | ``output push`` and ``output pop`` allow you to push and pop |
| 1472 | configurations on an internal configuration stack, so that you |
| 1473 | can temporarily modify the output configuration, then easily restore |
| 1474 | the previous configuration. Simply push before your change to save |
| 1475 | the current configuration, then pop when you wish to restore the |
| 1476 | previous configuration. |
| 1477 | |
| 1478 | ``output preset`` sets Clinic's output to one of several built-in |
| 1479 | preset configurations, as follows: |
| 1480 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 1481 | ``block`` |
| 1482 | Clinic's original starting configuration. Writes everything |
| 1483 | immediately after the input block. |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1484 | |
| 1485 | Suppress the ``parser_prototype`` |
| 1486 | and ``docstring_prototype``, write everything else to ``block``. |
| 1487 | |
| 1488 | ``file`` |
| 1489 | Designed to write everything to the "clinic file" that it can. |
| 1490 | You then ``#include`` this file near the top of your file. |
| 1491 | You may need to rearrange your file to make this work, though |
| 1492 | usually this just means creating forward declarations for various |
| 1493 | ``typedef`` and ``PyTypeObject`` definitions. |
| 1494 | |
| 1495 | Suppress the ``parser_prototype`` |
| 1496 | and ``docstring_prototype``, write the ``impl_definition`` to |
| 1497 | ``block``, and write everything else to ``file``. |
| 1498 | |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 1499 | The default filename is ``"{dirname}/clinic/{basename}.h"``. |
| 1500 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1501 | ``buffer`` |
gfyoung | ec19ba2 | 2017-06-06 15:23:52 -0400 | [diff] [blame] | 1502 | Save up most of the output from Clinic, to be written into |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1503 | your file near the end. For Python files implementing modules |
| 1504 | or builtin types, it's recommended that you dump the buffer |
| 1505 | just above the static structures for your module or |
| 1506 | builtin type; these are normally very near the end. Using |
| 1507 | ``buffer`` may require even more editing than ``file``, if |
| 1508 | your file has static ``PyMethodDef`` arrays defined in the |
| 1509 | middle of the file. |
| 1510 | |
| 1511 | Suppress the ``parser_prototype``, ``impl_prototype``, |
| 1512 | and ``docstring_prototype``, write the ``impl_definition`` to |
| 1513 | ``block``, and write everything else to ``file``. |
| 1514 | |
| 1515 | ``two-pass`` |
| 1516 | Similar to the ``buffer`` preset, but writes forward declarations to |
| 1517 | the ``two-pass`` buffer, and definitions to the ``buffer``. |
| 1518 | This is similar to the ``buffer`` preset, but may require |
| 1519 | less editing than ``buffer``. Dump the ``two-pass`` buffer |
| 1520 | near the top of your file, and dump the ``buffer`` near |
| 1521 | the end just like you would when using the ``buffer`` preset. |
| 1522 | |
| 1523 | Suppresses the ``impl_prototype``, write the ``impl_definition`` |
| 1524 | to ``block``, write ``docstring_prototype``, ``methoddef_define``, |
| 1525 | and ``parser_prototype`` to ``two-pass``, write everything else |
| 1526 | to ``buffer``. |
| 1527 | |
| 1528 | ``partial-buffer`` |
| 1529 | Similar to the ``buffer`` preset, but writes more things to ``block``, |
| 1530 | only writing the really big chunks of generated code to ``buffer``. |
| 1531 | This avoids the definition-before-use problem of ``buffer`` completely, |
| 1532 | at the small cost of having slightly more stuff in the block's output. |
| 1533 | Dump the ``buffer`` near the end, just like you would when using |
| 1534 | the ``buffer`` preset. |
| 1535 | |
| 1536 | Suppresses the ``impl_prototype``, write the ``docstring_definition`` |
Berker Peksag | 315e104 | 2015-05-19 01:36:55 +0300 | [diff] [blame] | 1537 | and ``parser_definition`` to ``buffer``, write everything else to ``block``. |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1538 | |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 1539 | The third new directive is ``destination``: |
| 1540 | |
| 1541 | .. code-block:: none |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1542 | |
| 1543 | destination <name> <command> [...] |
| 1544 | |
| 1545 | This performs an operation on the destination named ``name``. |
| 1546 | |
| 1547 | There are two defined subcommands: ``new`` and ``clear``. |
| 1548 | |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 1549 | The ``new`` subcommand works like this: |
| 1550 | |
| 1551 | .. code-block:: none |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1552 | |
| 1553 | destination <name> new <type> |
| 1554 | |
| 1555 | This creates a new destination with name ``<name>`` and type ``<type>``. |
| 1556 | |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 1557 | There are five destination types: |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1558 | |
| 1559 | ``suppress`` |
| 1560 | Throws the text away. |
| 1561 | |
| 1562 | ``block`` |
| 1563 | Writes the text to the current block. This is what Clinic |
| 1564 | originally did. |
| 1565 | |
| 1566 | ``buffer`` |
| 1567 | A simple text buffer, like the "buffer" builtin destination above. |
| 1568 | |
| 1569 | ``file`` |
| 1570 | A text file. The file destination takes an extra argument, |
| 1571 | a template to use for building the filename, like so: |
| 1572 | |
| 1573 | destination <name> new <type> <file_template> |
| 1574 | |
| 1575 | The template can use three strings internally that will be replaced |
| 1576 | by bits of the filename: |
| 1577 | |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 1578 | {path} |
| 1579 | The full path to the file, including directory and full filename. |
| 1580 | {dirname} |
| 1581 | The name of the directory the file is in. |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1582 | {basename} |
Larry Hastings | 0e25410 | 2014-01-26 00:42:02 -0800 | [diff] [blame] | 1583 | Just the name of the file, not including the directory. |
| 1584 | {basename_root} |
| 1585 | Basename with the extension clipped off |
| 1586 | (everything up to but not including the last '.'). |
| 1587 | {basename_extension} |
| 1588 | The last '.' and everything after it. If the basename |
| 1589 | does not contain a period, this will be the empty string. |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1590 | |
| 1591 | If there are no periods in the filename, {basename} and {filename} |
| 1592 | are the same, and {extension} is empty. "{basename}{extension}" |
| 1593 | is always exactly the same as "{filename}"." |
| 1594 | |
| 1595 | ``two-pass`` |
| 1596 | A two-pass buffer, like the "two-pass" builtin destination above. |
| 1597 | |
| 1598 | |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 1599 | The ``clear`` subcommand works like this: |
| 1600 | |
| 1601 | .. code-block:: none |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1602 | |
| 1603 | destination <name> clear |
| 1604 | |
| 1605 | It removes all the accumulated text up to this point in the destination. |
| 1606 | (I don't know what you'd need this for, but I thought maybe it'd be |
| 1607 | useful while someone's experimenting.) |
| 1608 | |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 1609 | The fourth new directive is ``set``: |
| 1610 | |
| 1611 | .. code-block:: none |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1612 | |
| 1613 | set line_prefix "string" |
| 1614 | set line_suffix "string" |
| 1615 | |
| 1616 | ``set`` lets you set two internal variables in Clinic. |
| 1617 | ``line_prefix`` is a string that will be prepended to every line of Clinic's output; |
| 1618 | ``line_suffix`` is a string that will be appended to every line of Clinic's output. |
| 1619 | |
Donald Stufft | 8b852f1 | 2014-05-20 12:58:38 -0400 | [diff] [blame] | 1620 | Both of these support two format strings: |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1621 | |
| 1622 | ``{block comment start}`` |
| 1623 | Turns into the string ``/*``, the start-comment text sequence for C files. |
| 1624 | |
| 1625 | ``{block comment end}`` |
| 1626 | Turns into the string ``*/``, the end-comment text sequence for C files. |
| 1627 | |
| 1628 | The final new directive is one you shouldn't need to use directly, |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 1629 | called ``preserve``: |
| 1630 | |
| 1631 | .. code-block:: none |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1632 | |
| 1633 | preserve |
| 1634 | |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 1635 | This tells Clinic that the current contents of the output should be kept, unmodified. |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1636 | This is used internally by Clinic when dumping output into ``file`` files; wrapping |
| 1637 | it in a Clinic block lets Clinic use its existing checksum functionality to ensure |
| 1638 | the file was not modified by hand before it gets overwritten. |
| 1639 | |
| 1640 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 1641 | The #ifdef trick |
| 1642 | ---------------------------------------------- |
| 1643 | |
| 1644 | If you're converting a function that isn't available on all platforms, |
| 1645 | there's a trick you can use to make life a little easier. The existing |
| 1646 | code probably looks like this:: |
| 1647 | |
| 1648 | #ifdef HAVE_FUNCTIONNAME |
| 1649 | static module_functionname(...) |
| 1650 | { |
| 1651 | ... |
| 1652 | } |
| 1653 | #endif /* HAVE_FUNCTIONNAME */ |
| 1654 | |
| 1655 | And then in the ``PyMethodDef`` structure at the bottom the existing code |
Martin Panter | cfa9bad | 2016-12-10 04:10:45 +0000 | [diff] [blame] | 1656 | will have: |
| 1657 | |
| 1658 | .. code-block:: none |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 1659 | |
| 1660 | #ifdef HAVE_FUNCTIONNAME |
| 1661 | {'functionname', ... }, |
| 1662 | #endif /* HAVE_FUNCTIONNAME */ |
| 1663 | |
| 1664 | In this scenario, you should enclose the body of your impl function inside the ``#ifdef``, |
| 1665 | like so:: |
| 1666 | |
| 1667 | #ifdef HAVE_FUNCTIONNAME |
| 1668 | /*[clinic input] |
| 1669 | module.functionname |
| 1670 | ... |
| 1671 | [clinic start generated code]*/ |
| 1672 | static module_functionname(...) |
| 1673 | { |
| 1674 | ... |
| 1675 | } |
| 1676 | #endif /* HAVE_FUNCTIONNAME */ |
| 1677 | |
| 1678 | Then, remove those three lines from the ``PyMethodDef`` structure, |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 1679 | replacing them with the macro Argument Clinic generated: |
| 1680 | |
| 1681 | .. code-block:: none |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 1682 | |
| 1683 | MODULE_FUNCTIONNAME_METHODDEF |
| 1684 | |
| 1685 | (You can find the real name for this macro inside the generated code. |
| 1686 | Or you can calculate it yourself: it's the name of your function as defined |
| 1687 | on the first line of your block, but with periods changed to underscores, |
| 1688 | uppercased, and ``"_METHODDEF"`` added to the end.) |
| 1689 | |
| 1690 | Perhaps you're wondering: what if ``HAVE_FUNCTIONNAME`` isn't defined? |
| 1691 | The ``MODULE_FUNCTIONNAME_METHODDEF`` macro won't be defined either! |
| 1692 | |
| 1693 | Here's where Argument Clinic gets very clever. It actually detects that the |
| 1694 | Argument Clinic block might be deactivated by the ``#ifdef``. When that |
| 1695 | happens, it generates a little extra code that looks like this:: |
| 1696 | |
| 1697 | #ifndef MODULE_FUNCTIONNAME_METHODDEF |
| 1698 | #define MODULE_FUNCTIONNAME_METHODDEF |
| 1699 | #endif /* !defined(MODULE_FUNCTIONNAME_METHODDEF) */ |
| 1700 | |
| 1701 | That means the macro always works. If the function is defined, this turns |
| 1702 | into the correct structure, including the trailing comma. If the function is |
| 1703 | undefined, this turns into nothing. |
| 1704 | |
| 1705 | However, this causes one ticklish problem: where should Argument Clinic put this |
| 1706 | extra code when using the "block" output preset? It can't go in the output block, |
Martin Panter | b4a2b36 | 2016-08-12 12:02:03 +0000 | [diff] [blame] | 1707 | because that could be deactivated by the ``#ifdef``. (That's the whole point!) |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 1708 | |
| 1709 | In this situation, Argument Clinic writes the extra code to the "buffer" destination. |
Martin Panter | cfa9bad | 2016-12-10 04:10:45 +0000 | [diff] [blame] | 1710 | This may mean that you get a complaint from Argument Clinic: |
| 1711 | |
| 1712 | .. code-block:: none |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 1713 | |
| 1714 | Warning in file "Modules/posixmodule.c" on line 12357: |
| 1715 | Destination buffer 'buffer' not empty at end of file, emptying. |
| 1716 | |
| 1717 | When this happens, just open your file, find the ``dump buffer`` block that |
| 1718 | Argument Clinic added to your file (it'll be at the very bottom), then |
| 1719 | move it above the ``PyMethodDef`` structure where that macro is used. |
| 1720 | |
| 1721 | |
| 1722 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1723 | Using Argument Clinic in Python files |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1724 | ------------------------------------- |
| 1725 | |
| 1726 | It's actually possible to use Argument Clinic to preprocess Python files. |
| 1727 | There's no point to using Argument Clinic blocks, of course, as the output |
| 1728 | wouldn't make any sense to the Python interpreter. But using Argument Clinic |
| 1729 | to run Python blocks lets you use Python as a Python preprocessor! |
| 1730 | |
| 1731 | Since Python comments are different from C comments, Argument Clinic |
Martin Panter | cfa9bad | 2016-12-10 04:10:45 +0000 | [diff] [blame] | 1732 | blocks embedded in Python files look slightly different. They look like this: |
| 1733 | |
| 1734 | .. code-block:: python3 |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1735 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1736 | #/*[python input] |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1737 | #print("def foo(): pass") |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1738 | #[python start generated code]*/ |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1739 | def foo(): pass |
| 1740 | #/*[python checksum:...]*/ |