blob: 2324142c64203c7600878cf30a2aae6606f08094 [file] [log] [blame]
Larry Hastings78cf85c2014-01-04 12:44:57 -08001======================
2Argument Clinic How-To
3======================
4
5:author: Larry Hastings
6
7
8.. topic:: Abstract
9
10 Argument Clinic is a preprocessor for CPython C files.
11 Its purpose is to automate all the boilerplate involved
12 with writing argument parsing code for "builtins".
13 This document shows you how to convert your first C
14 function to work with Argument Clinic, and then introduces
15 some advanced topics on Argument Clinic usage.
16
Larry Hastings6d2ea212014-01-05 02:50:45 -080017 Currently Argument Clinic is considered internal-only
18 for CPython. Its use is not supported for files outside
19 CPython, and no guarantees are made regarding backwards
20 compatibility for future versions. In other words: if you
21 maintain an external C extension for CPython, you're welcome
22 to experiment with Argument Clinic in your own code. But the
23 version of Argument Clinic that ships with CPython 3.5 *could*
24 be totally incompatible and break all your code.
Larry Hastings78cf85c2014-01-04 12:44:57 -080025
26========================
27Basic Concepts And Usage
28========================
29
Larry Hastings6d2ea212014-01-05 02:50:45 -080030Argument Clinic ships with CPython; you'll find it in ``Tools/clinic/clinic.py``.
Larry Hastings78cf85c2014-01-04 12:44:57 -080031If you run that script, specifying a C file as an argument::
32
33 % python3 Tools/clinic/clinic.py foo.c
34
35Argument Clinic will scan over the file looking for lines that
36look exactly like this::
37
Larry Hastings61272b72014-01-07 12:41:53 -080038 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -080039
40When it finds one, it reads everything up to a line that looks
Larry Hastings61272b72014-01-07 12:41:53 -080041exactly like this::
Larry Hastings78cf85c2014-01-04 12:44:57 -080042
Larry Hastings61272b72014-01-07 12:41:53 -080043 [clinic start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -080044
45Everything in between these two lines is input for Argument Clinic.
46All of these lines, including the beginning and ending comment
Larry Hastings6d2ea212014-01-05 02:50:45 -080047lines, are collectively called an Argument Clinic "block".
Larry Hastings78cf85c2014-01-04 12:44:57 -080048
49When Argument Clinic parses one of these blocks, it
50generates output. This output is rewritten into the C file
51immediately after the block, followed by a comment containing a checksum.
Larry Hastings6d2ea212014-01-05 02:50:45 -080052The Argument Clinic block now looks like this::
Larry Hastings78cf85c2014-01-04 12:44:57 -080053
Larry Hastings61272b72014-01-07 12:41:53 -080054 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -080055 ... clinic input goes here ...
Larry Hastings61272b72014-01-07 12:41:53 -080056 [clinic start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -080057 ... clinic output goes here ...
Larry Hastings61272b72014-01-07 12:41:53 -080058 /*[clinic end generated code: checksum=...]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -080059
60If you run Argument Clinic on the same file a second time, Argument Clinic
61will discard the old output and write out the new output with a fresh checksum
62line. However, if the input hasn't changed, the output won't change either.
63
64You should never modify the output portion of an Argument Clinic block. Instead,
65change the input until it produces the output you want. (That's the purpose of the
Larry Hastings6d2ea212014-01-05 02:50:45 -080066checksum--to detect if someone changed the output, as these edits would be lost
67the next time Argument Clinic writes out fresh output.)
Larry Hastings78cf85c2014-01-04 12:44:57 -080068
69For the sake of clarity, here's the terminology we'll use with Argument Clinic:
70
Larry Hastings61272b72014-01-07 12:41:53 -080071* The first line of the comment (``/*[clinic input]``) is the *start line*.
72* The last line of the initial comment (``[clinic start generated code]*/``) is the *end line*.
73* The last line (``/*[clinic end generated code: checksum=...]*/``) is the *checksum line*.
Larry Hastings78cf85c2014-01-04 12:44:57 -080074* In between the start line and the end line is the *input*.
75* In between the end line and the checksum line is the *output*.
76* All the text collectively, from the start line to the checksum line inclusively,
77 is the *block*. (A block that hasn't been successfully processed by Argument
78 Clinic yet doesn't have output or a checksum line, but it's still considered
79 a block.)
80
81
82==============================
83Converting Your First Function
84==============================
85
86The best way to get a sense of how Argument Clinic works is to
87convert a function to work with it. Let's dive in!
88
Larry Hastings6d2ea212014-01-05 02:50:45 -0800890. Make sure you're working with a freshly updated checkout
90 of the CPython trunk.
Larry Hastings78cf85c2014-01-04 12:44:57 -080091
Larry Hastings6d2ea212014-01-05 02:50:45 -0800921. Find a Python builtin that calls either :c:func:`PyArg_ParseTuple`
93 or :c:func:`PyArg_ParseTupleAndKeywords`, and hasn't been converted
94 to work with Argument Clinic yet.
Larry Hastings78cf85c2014-01-04 12:44:57 -080095 For my example I'm using ``pickle.Pickler.dump()``.
96
972. If the call to the ``PyArg_Parse`` function uses any of the
98 following format units::
99
100 O&
101 O!
102 es
103 es#
104 et
105 et#
106
Larry Hastings6d2ea212014-01-05 02:50:45 -0800107 or if it has multiple calls to :c:func:`PyArg_ParseTuple`,
Larry Hastings78cf85c2014-01-04 12:44:57 -0800108 you should choose a different function. Argument Clinic *does*
109 support all of these scenarios. But these are advanced
110 topics--let's do something simpler for your first function.
111
1123. Add the following boilerplate above the function, creating our block::
113
Larry Hastings61272b72014-01-07 12:41:53 -0800114 /*[clinic input]
115 [clinic start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800116
1174. Cut the docstring and paste it in between the ``[clinic]`` lines,
118 removing all the junk that makes it a properly quoted C string.
119 When you're done you should have just the text, based at the left
120 margin, with no line wider than 80 characters.
121 (Argument Clinic will preserve indents inside the docstring.)
122
123 Sample::
124
Larry Hastings61272b72014-01-07 12:41:53 -0800125 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800126 Write a pickled representation of obj to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -0800127 [clinic start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800128
1295. If your docstring doesn't have a "summary" line, Argument Clinic will
130 complain. So let's make sure it has one. The "summary" line should
131 be a paragraph consisting of a single 80-column line
132 at the beginning of the docstring.
133
Larry Hastings6d2ea212014-01-05 02:50:45 -0800134 (Our example docstring consists solely of a summary line, so the sample
Larry Hastings78cf85c2014-01-04 12:44:57 -0800135 code doesn't have to change for this step.)
136
1376. Above the docstring, enter the name of the function, followed
138 by a blank line. This should be the Python name of the function,
139 and should be the full dotted path
140 to the function--it should start with the name of the module,
141 include any sub-modules, and if the function is a method on
142 a class it should include the class name too.
143
144 Sample::
145
Larry Hastings61272b72014-01-07 12:41:53 -0800146 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800147 pickle.Pickler.dump
148
149 Write a pickled representation of obj to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -0800150 [clinic start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800151
1527. If this is the first time that module or class has been used with Argument
153 Clinic in this C file,
154 you must declare the module and/or class. Proper Argument Clinic hygiene
155 prefers declaring these in a separate block somewhere near the
156 top of the C file, in the same way that include files and statics go at
157 the top. (In our sample code we'll just show the two blocks next to
158 each other.)
159
160 Sample::
161
Larry Hastings61272b72014-01-07 12:41:53 -0800162 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800163 module pickle
164 class pickle.Pickler
Larry Hastings61272b72014-01-07 12:41:53 -0800165 [clinic start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800166
Larry Hastings61272b72014-01-07 12:41:53 -0800167 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800168 pickle.Pickler.dump
169
170 Write a pickled representation of obj to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -0800171 [clinic start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800172
173
1748. Declare each of the parameters to the function. Each parameter
175 should get its own line. All the parameter lines should be
176 indented from the function name and the docstring.
177
178 The general form of these parameter lines is as follows::
179
180 name_of_parameter: converter
181
182 If the parameter has a default value, add that after the
183 converter::
184
185 name_of_parameter: converter = default_value
186
187 Add a blank line below the parameters.
188
189 What's a "converter"? It establishes both the type
190 of the variable used in C, and the method to convert the Python
191 value into a C value at runtime.
192 For now you're going to use what's called a "legacy converter"--a
193 convenience syntax intended to make porting old code into Argument
194 Clinic easier.
195
196 For each parameter, copy the "format unit" for that
197 parameter from the ``PyArg_Parse()`` format argument and
198 specify *that* as its converter, as a quoted
199 string. ("format unit" is the formal name for the one-to-three
200 character substring of the ``format`` parameter that tells
201 the argument parsing function what the type of the variable
Larry Hastings6d2ea212014-01-05 02:50:45 -0800202 is and how to convert it. For more on format units please
203 see :ref:`arg-parsing`.)
Larry Hastings78cf85c2014-01-04 12:44:57 -0800204
205 For multicharacter format units like ``z#``, use the
206 entire two-or-three character string.
207
208 Sample::
209
Larry Hastings61272b72014-01-07 12:41:53 -0800210 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800211 module pickle
212 class pickle.Pickler
Larry Hastings61272b72014-01-07 12:41:53 -0800213 [clinic start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800214
Larry Hastings61272b72014-01-07 12:41:53 -0800215 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800216 pickle.Pickler.dump
217
218 obj: 'O'
219
220 Write a pickled representation of obj to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -0800221 [clinic start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800222
2239. If your function has ``|`` in the format string, meaning some
224 parameters have default values, you can ignore it. Argument
225 Clinic infers which parameters are optional based on whether
226 or not they have default values.
227
228 If your function has ``$`` in the format string, meaning it
229 takes keyword-only arguments, specify ``*`` on a line by
230 itself before the first keyword-only argument, indented the
231 same as the parameter lines.
232
233 (``pickle.Pickler.dump`` has neither, so our sample is unchanged.)
234
235
Larry Hastings6d2ea212014-01-05 02:50:45 -080023610. If the existing C function calls :c:func:`PyArg_ParseTuple`
237 (as opposed to :c:func:`PyArg_ParseTupleAndKeywords`), then all its
Larry Hastings78cf85c2014-01-04 12:44:57 -0800238 arguments are positional-only.
239
240 To mark all parameters as positional-only in Argument Clinic,
241 add a ``/`` on a line by itself after the last parameter,
242 indented the same as the parameter lines.
243
Larry Hastings6d2ea212014-01-05 02:50:45 -0800244 Currently this is all-or-nothing; either all parameters are
245 positional-only, or none of them are. (In the future Argument
246 Clinic may relax this restriction.)
247
Larry Hastings78cf85c2014-01-04 12:44:57 -0800248 Sample::
249
Larry Hastings61272b72014-01-07 12:41:53 -0800250 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800251 module pickle
252 class pickle.Pickler
Larry Hastings61272b72014-01-07 12:41:53 -0800253 [clinic start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800254
Larry Hastings61272b72014-01-07 12:41:53 -0800255 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800256 pickle.Pickler.dump
257
258 obj: 'O'
259 /
260
261 Write a pickled representation of obj to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -0800262 [clinic start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800263
Larry Hastings6d2ea212014-01-05 02:50:45 -080026411. It's helpful to write a per-parameter docstring for each parameter.
265 But per-parameter docstrings are optional; you can skip this step
266 if you prefer.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800267
Larry Hastings6d2ea212014-01-05 02:50:45 -0800268 Here's how to add a per-parameter docstring. The first line
Larry Hastings78cf85c2014-01-04 12:44:57 -0800269 of the per-parameter docstring must be indented further than the
Larry Hastings6d2ea212014-01-05 02:50:45 -0800270 parameter definition. The left margin of this first line establishes
271 the left margin for the whole per-parameter docstring; all the text
272 you write will be outdented by this amount. You can write as much
273 text as you like, across multiple lines if you wish.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800274
275 Sample::
276
Larry Hastings61272b72014-01-07 12:41:53 -0800277 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800278 module pickle
279 class pickle.Pickler
Larry Hastings61272b72014-01-07 12:41:53 -0800280 [clinic start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800281
Larry Hastings61272b72014-01-07 12:41:53 -0800282 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800283 pickle.Pickler.dump
284
285 obj: 'O'
286 The object to be pickled.
287 /
288
289 Write a pickled representation of obj to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -0800290 [clinic start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800291
29212. Save and close the file, then run ``Tools/clinic/clinic.py`` on it.
293 With luck everything worked and your block now has output! Reopen
294 the file in your text editor to see::
295
Larry Hastings61272b72014-01-07 12:41:53 -0800296 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800297 module pickle
298 class pickle.Pickler
Larry Hastings61272b72014-01-07 12:41:53 -0800299 [clinic start generated code]*/
300 /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800301
Larry Hastings61272b72014-01-07 12:41:53 -0800302 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800303 pickle.Pickler.dump
304
305 obj: 'O'
306 The object to be pickled.
307 /
308
309 Write a pickled representation of obj to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -0800310 [clinic start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800311
312 PyDoc_STRVAR(pickle_Pickler_dump__doc__,
313 "Write a pickled representation of obj to the open file.\n"
314 "\n"
315 ...
316 static PyObject *
317 pickle_Pickler_dump_impl(PyObject *self, PyObject *obj)
Larry Hastings61272b72014-01-07 12:41:53 -0800318 /*[clinic end generated code: checksum=3bd30745bf206a48f8b576a1da3d90f55a0a4187]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800319
Larry Hastings6d2ea212014-01-05 02:50:45 -0800320 Obviously, if Argument Clinic didn't produce any output, it's because
321 it found an error in your input. Keep fixing your errors and retrying
322 until Argument Clinic processes your file without complaint.
323
Larry Hastings78cf85c2014-01-04 12:44:57 -080032413. Double-check that the argument-parsing code Argument Clinic generated
325 looks basically the same as the existing code.
326
327 First, ensure both places use the same argument-parsing function.
328 The existing code must call either
Larry Hastings6d2ea212014-01-05 02:50:45 -0800329 :c:func:`PyArg_ParseTuple` or :c:func:`PyArg_ParseTupleAndKeywords`;
Larry Hastings78cf85c2014-01-04 12:44:57 -0800330 ensure that the code generated by Argument Clinic calls the
Larry Hastings6d2ea212014-01-05 02:50:45 -0800331 *exact* same function.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800332
Larry Hastings6d2ea212014-01-05 02:50:45 -0800333 Second, the format string passed in to :c:func:`PyArg_ParseTuple` or
334 :c:func:`PyArg_ParseTupleAndKeywords` should be *exactly* the same
335 as the hand-written one in the existing function, up to the colon
336 or semi-colon.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800337
Larry Hastings6d2ea212014-01-05 02:50:45 -0800338 (Argument Clinic always generates its format strings
339 with a ``:`` followed by the name of the function. If the
340 existing code's format string ends with ``;``, to provide
341 usage help, this change is harmless--don't worry about it.)
Larry Hastings78cf85c2014-01-04 12:44:57 -0800342
Larry Hastings6d2ea212014-01-05 02:50:45 -0800343 Third, for parameters whose format units require two arguments
344 (like a length variable, or an encoding string, or a pointer
345 to a conversion function), ensure that the second argument is
346 *exactly* the same between the two invocations.
347
348 Fourth, inside the output portion of the block you'll find a preprocessor
349 macro defining the appropriate static :c:type:`PyMethodDef` structure for
350 this builtin::
351
352 #define _PICKLE_PICKLER_DUMP_METHODDEF \
353 {"dump", (PyCFunction)_pickle_Pickler_dump, METH_O, _pickle_Pickler_dump__doc__},
354
355 This static structure should be *exactly* the same as the existing static
356 :c:type:`PyMethodDef` structure for this builtin.
357
358 If any of these items differ in *any way*,
359 adjust your Argument Clinic function specification and rerun
360 ``Tools/clinic/clinic.py`` until they *are* the same.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800361
362
36314. Notice that the last line of its output is the declaration
364 of your "impl" function. This is where the builtin's implementation goes.
365 Delete the existing prototype of the function you're modifying, but leave
366 the opening curly brace. Now delete its argument parsing code and the
367 declarations of all the variables it dumps the arguments into.
368 Notice how the Python arguments are now arguments to this impl function;
369 if the implementation used different names for these variables, fix it.
Larry Hastings6d2ea212014-01-05 02:50:45 -0800370
371 Let's reiterate, just because it's kind of weird. Your code should now
372 look like this::
373
374 static return_type
375 your_function_impl(...)
Larry Hastings61272b72014-01-07 12:41:53 -0800376 /*[clinic end generated code: checksum=...]*/
Larry Hastings6d2ea212014-01-05 02:50:45 -0800377 {
378 ...
379
380 Argument Clinic generated the checksum line and the function prototype just
381 above it. You should write the opening (and closing) curly braces for the
382 function, and the implementation inside.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800383
384 Sample::
385
Larry Hastings61272b72014-01-07 12:41:53 -0800386 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800387 module pickle
388 class pickle.Pickler
Larry Hastings61272b72014-01-07 12:41:53 -0800389 [clinic start generated code]*/
390 /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800391
Larry Hastings61272b72014-01-07 12:41:53 -0800392 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800393 pickle.Pickler.dump
394
395 obj: 'O'
396 The object to be pickled.
397 /
398
399 Write a pickled representation of obj to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -0800400 [clinic start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800401
402 PyDoc_STRVAR(pickle_Pickler_dump__doc__,
403 "Write a pickled representation of obj to the open file.\n"
404 "\n"
405 ...
406 static PyObject *
407 pickle_Pickler_dump_impl(PyObject *self, PyObject *obj)
Larry Hastings61272b72014-01-07 12:41:53 -0800408 /*[clinic end generated code: checksum=3bd30745bf206a48f8b576a1da3d90f55a0a4187]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800409 {
410 /* Check whether the Pickler was initialized correctly (issue3664).
411 Developers often forget to call __init__() in their subclasses, which
412 would trigger a segfault without this check. */
413 if (self->write == NULL) {
414 PyErr_Format(PicklingError,
415 "Pickler.__init__() was not called by %s.__init__()",
416 Py_TYPE(self)->tp_name);
417 return NULL;
418 }
419
420 if (_Pickler_ClearBuffer(self) < 0)
421 return NULL;
422
423 ...
424
Larry Hastings6d2ea212014-01-05 02:50:45 -080042515. Remember the macro with the :c:type:`PyMethodDef` structure for this
426 function? Find the existing :c:type:`PyMethodDef` structure for this
427 function and replace it with a reference to the macro. (If the builtin
428 is at module scope, this will probably be very near the end of the file;
429 if the builtin is a class method, this will probably be below but relatively
430 near to the implementation.)
431
432 Note that the body of the macro contains a trailing comma. So when you
433 replace the existing static :c:type:`PyMethodDef` structure with the macro,
434 *don't* add a comma to the end.
435
436 Sample::
437
438 static struct PyMethodDef Pickler_methods[] = {
439 _PICKLE_PICKLER_DUMP_METHODDEF
440 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
441 {NULL, NULL} /* sentinel */
442 };
443
444
44516. Compile, then run the relevant portions of the regression-test suite.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800446 This change should not introduce any new compile-time warnings or errors,
447 and there should be no externally-visible change to Python's behavior.
448
449 Well, except for one difference: ``inspect.signature()`` run on your function
450 should now provide a valid signature!
451
452 Congratulations, you've ported your first function to work with Argument Clinic!
453
454===============
455Advanced Topics
456===============
457
458
459Renaming the C functions generated by Argument Clinic
460-----------------------------------------------------
461
462Argument Clinic automatically names the functions it generates for you.
463Occasionally this may cause a problem, if the generated name collides with
Larry Hastings6d2ea212014-01-05 02:50:45 -0800464the name of an existing C function. There's an easy solution: override the names
465used for the C functions. Just add the keyword ``"as"``
Larry Hastings78cf85c2014-01-04 12:44:57 -0800466to your function declaration line, followed by the function name you wish to use.
Larry Hastings6d2ea212014-01-05 02:50:45 -0800467Argument Clinic will use that function name for the base (generated) function,
468then add ``"_impl"`` to the end and use that for the name of the impl function.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800469
470For example, if we wanted to rename the C function names generated for
471``pickle.Pickler.dump``, it'd look like this::
472
Larry Hastings61272b72014-01-07 12:41:53 -0800473 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800474 pickle.Pickler.dump as pickler_dumper
475
476 ...
477
478The base function would now be named ``pickler_dumper()``,
Larry Hastings6d2ea212014-01-05 02:50:45 -0800479and the impl function would now be named ``pickler_dumper_impl()``.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800480
481
482Optional Groups
483---------------
484
485Some legacy functions have a tricky approach to parsing their arguments:
486they count the number of positional arguments, then use a ``switch`` statement
Larry Hastings6d2ea212014-01-05 02:50:45 -0800487to call one of several different :c:func:`PyArg_ParseTuple` calls depending on
Larry Hastings78cf85c2014-01-04 12:44:57 -0800488how many positional arguments there are. (These functions cannot accept
489keyword-only arguments.) This approach was used to simulate optional
Larry Hastings6d2ea212014-01-05 02:50:45 -0800490arguments back before :c:func:`PyArg_ParseTupleAndKeywords` was created.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800491
Larry Hastings6d2ea212014-01-05 02:50:45 -0800492While functions using this approach can often be converted to
493use :c:func:`PyArg_ParseTupleAndKeywords`, optional arguments, and default values,
494it's not always possible. Some of these legacy functions have
495behaviors :c:func:`PyArg_ParseTupleAndKeywords` doesn't directly support.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800496The most obvious example is the builtin function ``range()``, which has
497an optional argument on the *left* side of its required argument!
498Another example is ``curses.window.addch()``, which has a group of two
499arguments that must always be specified together. (The arguments are
500called ``x`` and ``y``; if you call the function passing in ``x``,
501you must also pass in ``y``--and if you don't pass in ``x`` you may not
502pass in ``y`` either.)
503
Larry Hastings6d2ea212014-01-05 02:50:45 -0800504In any case, the goal of Argument Clinic is to support argument parsing
505for all existing CPython builtins without changing their semantics.
506Therefore Argument Clinic supports
507this alternate approach to parsing, using what are called *optional groups*.
508Optional groups are groups of arguments that must all be passed in together.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800509They can be to the left or the right of the required arguments. They
510can *only* be used with positional-only parameters.
511
512To specify an optional group, add a ``[`` on a line by itself before
Larry Hastings6d2ea212014-01-05 02:50:45 -0800513the parameters you wish to group together, and a ``]`` on a line by itself
514after these parameters. As an example, here's how ``curses.window.addch``
Larry Hastings78cf85c2014-01-04 12:44:57 -0800515uses optional groups to make the first two parameters and the last
516parameter optional::
517
Larry Hastings61272b72014-01-07 12:41:53 -0800518 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800519
520 curses.window.addch
521
522 [
523 x: int
524 X-coordinate.
525 y: int
526 Y-coordinate.
527 ]
528
529 ch: object
530 Character to add.
531
532 [
533 attr: long
534 Attributes for the character.
535 ]
536 /
537
538 ...
539
540
541Notes:
542
543* For every optional group, one additional parameter will be passed into the
Larry Hastings6d2ea212014-01-05 02:50:45 -0800544 impl function representing the group. The parameter will be an int named
545 ``group_{direction}_{number}``,
Larry Hastings78cf85c2014-01-04 12:44:57 -0800546 where ``{direction}`` is either ``right`` or ``left`` depending on whether the group
547 is before or after the required parameters, and ``{number}`` is a monotonically
548 increasing number (starting at 1) indicating how far away the group is from
549 the required parameters. When the impl is called, this parameter will be set
550 to zero if this group was unused, and set to non-zero if this group was used.
551 (By used or unused, I mean whether or not the parameters received arguments
552 in this invocation.)
553
554* If there are no required arguments, the optional groups will behave
Larry Hastings6d2ea212014-01-05 02:50:45 -0800555 as if they're to the right of the required arguments.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800556
557* In the case of ambiguity, the argument parsing code
558 favors parameters on the left (before the required parameters).
559
Larry Hastings6d2ea212014-01-05 02:50:45 -0800560* Optional groups can only contain positional-only parameters.
561
Larry Hastings78cf85c2014-01-04 12:44:57 -0800562* Optional groups are *only* intended for legacy code. Please do not
563 use optional groups for new code.
564
565
566Using real Argument Clinic converters, instead of "legacy converters"
567---------------------------------------------------------------------
568
569To save time, and to minimize how much you need to learn
570to achieve your first port to Argument Clinic, the walkthrough above tells
Larry Hastings6d2ea212014-01-05 02:50:45 -0800571you to use "legacy converters". "Legacy converters" are a convenience,
Larry Hastings78cf85c2014-01-04 12:44:57 -0800572designed explicitly to make porting existing code to Argument Clinic
573easier. And to be clear, their use is entirely acceptable when porting
574code for Python 3.4.
575
576However, in the long term we probably want all our blocks to
577use Argument Clinic's real syntax for converters. Why? A couple
578reasons:
579
580* The proper converters are far easier to read and clearer in their intent.
581* There are some format units that are unsupported as "legacy converters",
582 because they require arguments, and the legacy converter syntax doesn't
583 support specifying arguments.
584* In the future we may have a new argument parsing library that isn't
Larry Hastings6d2ea212014-01-05 02:50:45 -0800585 restricted to what :c:func:`PyArg_ParseTuple` supports; this flexibility
586 won't be available to parameters using legacy converters.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800587
Larry Hastings6d2ea212014-01-05 02:50:45 -0800588Therefore, if you don't mind a little extra effort, you should consider
589using normal converters instead of legacy converters.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800590
591In a nutshell, the syntax for Argument Clinic (non-legacy) converters
592looks like a Python function call. However, if there are no explicit
593arguments to the function (all functions take their default values),
594you may omit the parentheses. Thus ``bool`` and ``bool()`` are exactly
Larry Hastings6d2ea212014-01-05 02:50:45 -0800595the same converters.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800596
Larry Hastings6d2ea212014-01-05 02:50:45 -0800597All arguments to Argument Clinic converters are keyword-only.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800598All Argument Clinic converters accept the following arguments:
599
600``doc_default``
601 If the parameter takes a default value, normally this value is also
602 provided in the ``inspect.Signature`` metadata, and displayed in the
603 docstring. ``doc_default`` lets you override the value used in these
604 two places: pass in a string representing the Python value you wish
605 to use in these two contexts.
606
607``required``
608 If a parameter takes a default value, Argument Clinic infers that the
609 parameter is optional. However, you may want a parameter to take a
610 default value in C, but not behave in Python as if the parameter is
611 optional. Passing in ``required=True`` to a converter tells Argument
612 Clinic that this parameter is not optional, even if it has a default
613 value.
614
615``annotation``
616 The annotation value for this parameter. Not currently supported,
617 because PEP 8 mandates that the Python library may not use
618 annotations.
619
620Below is a table showing the mapping of legacy converters into real
621Argument Clinic converters. On the left is the legacy converter,
622on the right is the text you'd replace it with.
623
624========= =================================================================================
625``'B'`` ``byte(bitwise=True)``
626``'b'`` ``byte``
627``'c'`` ``char``
628``'C'`` ``int(types='str')``
629``'d'`` ``double``
630``'D'`` ``Py_complex``
631``'es#'`` ``str(encoding='name_of_encoding', length=True, zeroes=True)``
632``'es'`` ``str(encoding='name_of_encoding')``
633``'et#'`` ``str(encoding='name_of_encoding', types='bytes bytearray str', length=True)``
634``'et'`` ``str(encoding='name_of_encoding', types='bytes bytearray str')``
635``'f'`` ``float``
636``'h'`` ``short``
637``'H'`` ``unsigned_short``
638``'i'`` ``int``
639``'I'`` ``unsigned_int``
640``'K'`` ``unsigned_PY_LONG_LONG``
641``'L'`` ``PY_LONG_LONG``
642``'n'`` ``Py_ssize_t``
Larry Hastings77561cc2014-01-07 12:13:13 -0800643``'O!'`` ``object(subclass_of='&PySomething_Type')``
Larry Hastings78cf85c2014-01-04 12:44:57 -0800644``'O&'`` ``object(converter='name_of_c_function')``
645``'O'`` ``object``
646``'p'`` ``bool``
647``'s#'`` ``str(length=True)``
648``'S'`` ``PyBytesObject``
649``'s'`` ``str``
650``'s*'`` ``Py_buffer(types='str bytes bytearray buffer')``
651``'u#'`` ``Py_UNICODE(length=True)``
652``'u'`` ``Py_UNICODE``
653``'U'`` ``unicode``
654``'w*'`` ``Py_buffer(types='bytearray rwbuffer')``
655``'y#'`` ``str(type='bytes', length=True)``
656``'Y'`` ``PyByteArrayObject``
657``'y'`` ``str(type='bytes')``
658``'y*'`` ``Py_buffer``
659``'Z#'`` ``Py_UNICODE(nullable=True, length=True)``
660``'z#'`` ``str(nullable=True, length=True)``
661``'Z'`` ``Py_UNICODE(nullable=True)``
662``'z'`` ``str(nullable=True)``
663``'z*'`` ``Py_buffer(types='str bytes bytearray buffer', nullable=True)``
664========= =================================================================================
665
666As an example, here's our sample ``pickle.Pickler.dump`` using the proper
667converter::
668
Larry Hastings61272b72014-01-07 12:41:53 -0800669 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800670 pickle.Pickler.dump
671
672 obj: object
673 The object to be pickled.
674 /
675
676 Write a pickled representation of obj to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -0800677 [clinic start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800678
679Argument Clinic will show you all the converters it has
680available. For each converter it'll show you all the parameters
681it accepts, along with the default value for each parameter.
682Just run ``Tools/clinic/clinic.py --converters`` to see the full list.
683
684
685Advanced converters
686-------------------
687
688Remeber those format units you skipped for your first
689time because they were advanced? Here's how to handle those too.
690
691The trick is, all those format units take arguments--either
692conversion functions, or types, or strings specifying an encoding.
693(But "legacy converters" don't support arguments. That's why we
694skipped them for your first function.) The argument you specified
695to the format unit is now an argument to the converter; this
Larry Hastings77561cc2014-01-07 12:13:13 -0800696argument is either ``converter`` (for ``O&``), ``subclass_of`` (for ``O!``),
Larry Hastings78cf85c2014-01-04 12:44:57 -0800697or ``encoding`` (for all the format units that start with ``e``).
698
Larry Hastings77561cc2014-01-07 12:13:13 -0800699When using ``subclass_of``, you may also want to use the other
700custom argument for ``object()``: ``type``, which lets you set the type
701actually used for the parameter. For example, if you want to ensure
702that the object is a subclass of ``PyUnicode_Type``, you probably want
703to use the converter ``object(type='PyUnicodeObject *', subclass_of='&PyUnicode_Type')``.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800704
Larry Hastings77561cc2014-01-07 12:13:13 -0800705One possible problem with using Argument Clinic: it takes away some possible
706flexibility for the format units starting with ``e``. When writing a
707``PyArg_Parse`` call by hand, you could theoretically decide at runtime what
Larry Hastings6d2ea212014-01-05 02:50:45 -0800708encoding string to pass in to :c:func:`PyArg_ParseTuple`. But now this string must
Larry Hastings77561cc2014-01-07 12:13:13 -0800709be hard-coded at Argument-Clinic-preprocessing-time. This limitation is deliberate;
710it made supporting this format unit much easier, and may allow for future optimizations.
711This restriction doesn't seem unreasonable; CPython itself always passes in static
Larry Hastings6d2ea212014-01-05 02:50:45 -0800712hard-coded encoding strings for parameters whose format units start with ``e``.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800713
714
715Using a return converter
716------------------------
717
718By default the impl function Argument Clinic generates for you returns ``PyObject *``.
719But your C function often computes some C type, then converts it into the ``PyObject *``
720at the last moment. Argument Clinic handles converting your inputs from Python types
721into native C types--why not have it convert your return value from a native C type
722into a Python type too?
723
724That's what a "return converter" does. It changes your impl function to return
725some C type, then adds code to the generated (non-impl) function to handle converting
726that value into the appropriate ``PyObject *``.
727
728The syntax for return converters is similar to that of parameter converters.
729You specify the return converter like it was a return annotation on the
730function itself. Return converters behave much the same as parameter converters;
731they take arguments, the arguments are all keyword-only, and if you're not changing
732any of the default arguments you can omit the parentheses.
733
734(If you use both ``"as"`` *and* a return converter for your function,
735the ``"as"`` should come before the return converter.)
736
737There's one additional complication when using return converters: how do you
738indicate an error has occured? Normally, a function returns a valid (non-``NULL``)
739pointer for success, and ``NULL`` for failure. But if you use an integer return converter,
740all integers are valid. How can Argument Clinic detect an error? Its solution: each return
741converter implicitly looks for a special value that indicates an error. If you return
742that value, and an error has been set (``PyErr_Occurred()`` returns a true
743value), then the generated code will propogate the error. Otherwise it will
744encode the value you return like normal.
745
746Currently Argument Clinic supports only a few return converters::
747
748 int
749 long
750 Py_ssize_t
751 DecodeFSDefault
752
753None of these take parameters. For the first three, return -1 to indicate
754error. For ``DecodeFSDefault``, the return type is ``char *``; return a NULL
755pointer to indicate an error.
756
Larry Hastings6d2ea212014-01-05 02:50:45 -0800757To see all the return converters Argument Clinic supports, along with
758their parameters (if any),
759just run ``Tools/clinic/clinic.py --converters`` for the full list.
760
761
Larry Hastings78cf85c2014-01-04 12:44:57 -0800762Calling Python code
763-------------------
764
765The rest of the advanced topics require you to write Python code
Larry Hastings6d2ea212014-01-05 02:50:45 -0800766which lives inside your C file and modifies Argument Clinic's
767runtime state. This is simple: you simply define a Python block.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800768
769A Python block uses different delimiter lines than an Argument
770Clinic function block. It looks like this::
771
Larry Hastings61272b72014-01-07 12:41:53 -0800772 /*[python input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800773 # python code goes here
Larry Hastings61272b72014-01-07 12:41:53 -0800774 [python start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800775
776All the code inside the Python block is executed at the
777time it's parsed. All text written to stdout inside the block
778is redirected into the "output" after the block.
779
780As an example, here's a Python block that adds a static integer
781variable to the C code::
782
Larry Hastings61272b72014-01-07 12:41:53 -0800783 /*[python input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800784 print('static int __ignored_unused_variable__ = 0;')
Larry Hastings61272b72014-01-07 12:41:53 -0800785 [python start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800786 static int __ignored_unused_variable__ = 0;
787 /*[python checksum:...]*/
788
789
790Using a "self converter"
791------------------------
792
793Argument Clinic automatically adds a "self" parameter for you
794using a default converter. However, you can override
795Argument Clinic's converter and specify one yourself.
796Just add your own ``self`` parameter as the first parameter in a
797block, and ensure that its converter is an instance of
798``self_converter`` or a subclass thereof.
799
800What's the point? This lets you automatically cast ``self``
Larry Hastings77561cc2014-01-07 12:13:13 -0800801from ``PyObject *`` to a custom type, just like ``object()``
802does with its ``type`` parameter.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800803
804How do you specify the custom type you want to cast ``self`` to?
805If you only have one or two functions with the same type for ``self``,
806you can directly use Argument Clinic's existing ``self`` converter,
807passing in the type you want to use as the ``type`` parameter::
808
Larry Hastings61272b72014-01-07 12:41:53 -0800809 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800810
811 _pickle.Pickler.dump
812
813 self: self(type="PicklerObject *")
814 obj: object
815 /
816
817 Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -0800818 [clinic start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800819
820On the other hand, if you have a lot of functions that will use the same
821type for ``self``, it's best to create your own converter, subclassing
822``self_converter`` but overwriting the ``type`` member::
823
Zachary Warec1cb2272014-01-09 21:41:23 -0600824 /*[python input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800825 class PicklerObject_converter(self_converter):
826 type = "PicklerObject *"
Zachary Warec1cb2272014-01-09 21:41:23 -0600827 [python start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800828
Larry Hastings61272b72014-01-07 12:41:53 -0800829 /*[clinic input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800830
831 _pickle.Pickler.dump
832
833 self: PicklerObject
834 obj: object
835 /
836
837 Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -0800838 [clinic start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800839
840
841
842Writing a custom converter
843--------------------------
844
845As we hinted at in the previous section... you can write your own converters!
846A converter is simply a Python class that inherits from ``CConverter``.
847The main purpose of a custom converter is if you have a parameter using
848the ``O&`` format unit--parsing this parameter means calling
Larry Hastings6d2ea212014-01-05 02:50:45 -0800849a :c:func:`PyArg_ParseTuple` "converter function".
Larry Hastings78cf85c2014-01-04 12:44:57 -0800850
851Your converter class should be named ``*something*_converter``.
852If the name follows this convention, then your converter class
853will be automatically registered with Argument Clinic; its name
854will be the name of your class with the ``_converter`` suffix
Larry Hastings6d2ea212014-01-05 02:50:45 -0800855stripped off. (This is accomplished with a metaclass.)
Larry Hastings78cf85c2014-01-04 12:44:57 -0800856
857You shouldn't subclass ``CConverter.__init__``. Instead, you should
858write a ``converter_init()`` function. ``converter_init()``
859always accepts a ``self`` parameter; after that, all additional
860parameters *must* be keyword-only. Any arguments passed in to
861the converter in Argument Clinic will be passed along to your
862``converter_init()``.
863
864There are some additional members of ``CConverter`` you may wish
865to specify in your subclass. Here's the current list:
866
867``type``
868 The C type to use for this variable.
869 ``type`` should be a Python string specifying the type, e.g. ``int``.
870 If this is a pointer type, the type string should end with ``' *'``.
871
872``default``
873 The Python default value for this parameter, as a Python value.
874 Or the magic value ``unspecified`` if there is no default.
875
876``doc_default``
877 ``default`` as it should appear in the documentation,
878 as a string.
879 Or ``None`` if there is no default.
880 This string, when run through ``eval()``, should produce
881 a Python value.
882
883``py_default``
884 ``default`` as it should appear in Python code,
885 as a string.
886 Or ``None`` if there is no default.
887
888``c_default``
889 ``default`` as it should appear in C code,
890 as a string.
891 Or ``None`` if there is no default.
892
893``c_ignored_default``
894 The default value used to initialize the C variable when
895 there is no default, but not specifying a default may
Larry Hastings6d2ea212014-01-05 02:50:45 -0800896 result in an "uninitialized variable" warning. This can
Larry Hastings78cf85c2014-01-04 12:44:57 -0800897 easily happen when using option groups--although
Larry Hastings6d2ea212014-01-05 02:50:45 -0800898 properly-written code will never actually use this value,
899 the variable does get passed in to the impl, and the
900 C compiler will complain about the "use" of the
901 uninitialized value. This value should always be a
902 non-empty string.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800903
904``converter``
905 The name of the C converter function, as a string.
906
907``impl_by_reference``
908 A boolean value. If true,
909 Argument Clinic will add a ``&`` in front of the name of
910 the variable when passing it into the impl function.
911
912``parse_by_reference``
913 A boolean value. If true,
914 Argument Clinic will add a ``&`` in front of the name of
Larry Hastings6d2ea212014-01-05 02:50:45 -0800915 the variable when passing it into :c:func:`PyArg_ParseTuple`.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800916
917
918Here's the simplest example of a custom converter, from ``Modules/zlibmodule.c``::
919
Larry Hastings61272b72014-01-07 12:41:53 -0800920 /*[python input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800921
922 class uint_converter(CConverter):
923 type = 'unsigned int'
924 converter = 'uint_converter'
925
Larry Hastings61272b72014-01-07 12:41:53 -0800926 [python start generated code]*/
927 /*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800928
Larry Hastings6d2ea212014-01-05 02:50:45 -0800929This block adds a converter to Argument Clinic named ``uint``. Parameters
Larry Hastings78cf85c2014-01-04 12:44:57 -0800930declared as ``uint`` will be declared as type ``unsigned int``, and will
Larry Hastings6d2ea212014-01-05 02:50:45 -0800931be parsed by the ``'O&'`` format unit, which will call the ``uint_converter``
932converter function.
Larry Hastings78cf85c2014-01-04 12:44:57 -0800933``uint`` variables automatically support default values.
934
935More sophisticated custom converters can insert custom C code to
936handle initialization and cleanup.
937You can see more examples of custom converters in the CPython
938source tree; grep the C files for the string ``CConverter``.
939
940Writing a custom return converter
941---------------------------------
942
943Writing a custom return converter is much like writing
Larry Hastings6d2ea212014-01-05 02:50:45 -0800944a custom converter. Except it's somewhat simpler, because return
Larry Hastings78cf85c2014-01-04 12:44:57 -0800945converters are themselves much simpler.
946
947Return converters must subclass ``CReturnConverter``.
948There are no examples yet of custom return converters,
949because they are not widely used yet. If you wish to
950write your own return converter, please read ``Tools/clinic/clinic.py``,
951specifically the implementation of ``CReturnConverter`` and
952all its subclasses.
953
954
955Using Argument Clinic in Python files
956-------------------------------------
957
958It's actually possible to use Argument Clinic to preprocess Python files.
959There's no point to using Argument Clinic blocks, of course, as the output
960wouldn't make any sense to the Python interpreter. But using Argument Clinic
961to run Python blocks lets you use Python as a Python preprocessor!
962
963Since Python comments are different from C comments, Argument Clinic
964blocks embedded in Python files look slightly different. They look like this::
965
Larry Hastings61272b72014-01-07 12:41:53 -0800966 #/*[python input]
Larry Hastings78cf85c2014-01-04 12:44:57 -0800967 #print("def foo(): pass")
Larry Hastings61272b72014-01-07 12:41:53 -0800968 #[python start generated code]*/
Larry Hastings78cf85c2014-01-04 12:44:57 -0800969 def foo(): pass
970 #/*[python checksum:...]*/