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