@@ -560,15 +560,12 @@ Let's dive in!
560
560
Congratulations, you've ported your first function to work with Argument Clinic!
561
561
562
562
563
- Advanced topics
564
- ===============
563
+ How-to guides
564
+ =============
565
565
566
- Now that you've had some experience working with Argument Clinic, it's time
567
- for some advanced topics.
568
566
569
-
570
- Symbolic default values
571
- -----------------------
567
+ How to use symbolic default values
568
+ ----------------------------------
572
569
573
570
The default value you provide for a parameter can't be any arbitrary
574
571
expression. Currently the following are explicitly supported:
@@ -583,8 +580,8 @@ expression. Currently the following are explicitly supported:
583
580
to allow full expressions like ``CONSTANT - 1 ``.)
584
581
585
582
586
- Renaming the C functions and variables generated by Argument Clinic
587
- -------------------------------------------------------------------
583
+ How to to rename C functions and variables generated by Argument Clinic
584
+ -----------------------------------------------------------------------
588
585
589
586
Argument Clinic automatically names the functions it generates for you.
590
587
Occasionally this may cause a problem, if the generated name collides with
@@ -626,8 +623,8 @@ array) would be ``file``, but the C variable would be named ``file_obj``.
626
623
You can use this to rename the ``self `` parameter too!
627
624
628
625
629
- Converting functions using PyArg_UnpackTuple
630
- --------------------------------------------
626
+ How to convert functions using `` PyArg_UnpackTuple ``
627
+ ----------------------------------------------------
631
628
632
629
To convert a function parsing its arguments with :c:func: `PyArg_UnpackTuple `,
633
630
simply write out all the arguments, specifying each as an ``object ``. You
@@ -639,8 +636,8 @@ Currently the generated code will use :c:func:`PyArg_ParseTuple`, but this
639
636
will change soon.
640
637
641
638
642
- Optional groups
643
- ---------------
639
+ How to use optional groups
640
+ --------------------------
644
641
645
642
Some legacy functions have a tricky approach to parsing their arguments:
646
643
they count the number of positional arguments, then use a ``switch `` statement
@@ -732,8 +729,8 @@ Notes:
732
729
use optional groups for new code.
733
730
734
731
735
- Using real Argument Clinic converters, instead of "legacy converters"
736
- ---------------------------------------------------------------------
732
+ How to use real Argument Clinic converters, instead of "legacy converters"
733
+ --------------------------------------------------------------------------
737
734
738
735
To save time, and to minimize how much you need to learn
739
736
to achieve your first port to Argument Clinic, the walkthrough above tells
@@ -903,17 +900,17 @@ it accepts, along with the default value for each parameter.
903
900
Just run ``Tools/clinic/clinic.py --converters `` to see the full list.
904
901
905
902
906
- Py_buffer
907
- ---------
903
+ How to use the `` Py_buffer `` converter
904
+ --------------------------------------
908
905
909
906
When using the ``Py_buffer `` converter
910
907
(or the ``'s*' ``, ``'w*' ``, ``'*y' ``, or ``'z*' `` legacy converters),
911
908
you *must * not call :c:func: `PyBuffer_Release ` on the provided buffer.
912
909
Argument Clinic generates code that does it for you (in the parsing function).
913
910
914
911
915
- Advanced converters
916
- -------------------
912
+ How to use advanced converters
913
+ ------------------------------
917
914
918
915
Remember those format units you skipped for your first
919
916
time because they were advanced? Here's how to handle those too.
@@ -944,8 +941,8 @@ hard-coded encoding strings for parameters whose format units start with ``e``.
944
941
945
942
.. _default_values :
946
943
947
- Parameter default values
948
- ------------------------
944
+ How to assign default values to parameter
945
+ -----------------------------------------
949
946
950
947
Default values for parameters can be any of a number of values.
951
948
At their simplest, they can be string, int, or float literals:
@@ -968,8 +965,8 @@ There's also special support for a default value of ``NULL``, and
968
965
for simple expressions, documented in the following sections.
969
966
970
967
971
- The ``NULL `` default value
972
- --------------------------
968
+ How to use the ``NULL `` default value
969
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
973
970
974
971
For string and object parameters, you can set them to ``None `` to indicate
975
972
that there's no default. However, that means the C variable will be
@@ -979,8 +976,8 @@ behaves like a default value of ``None``, but the C variable is initialized
979
976
with ``NULL ``.
980
977
981
978
982
- Expressions specified as default values
983
- ---------------------------------------
979
+ How to use expressions as default values
980
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
984
981
985
982
The default value for a parameter can be more than just a literal value.
986
983
It can be an entire expression, using math operators and looking up attributes
@@ -1036,8 +1033,8 @@ you're not permitted to use:
1036
1033
* Tuple/list/set/dict literals.
1037
1034
1038
1035
1039
- Using a return converter
1040
- ------------------------
1036
+ How to use return converters
1037
+ ----------------------------
1041
1038
1042
1039
By default, the impl function Argument Clinic generates for you returns
1043
1040
:c:type: `PyObject * <PyObject> `.
@@ -1106,8 +1103,8 @@ their parameters (if any),
1106
1103
just run ``Tools/clinic/clinic.py --converters `` for the full list.
1107
1104
1108
1105
1109
- Cloning existing functions
1110
- --------------------------
1106
+ How to clone existing functions
1107
+ -------------------------------
1111
1108
1112
1109
If you have a number of functions that look similar, you may be able to
1113
1110
use Clinic's "clone" feature. When you clone an existing function,
@@ -1150,8 +1147,8 @@ Also, the function you are cloning from must have been previously defined
1150
1147
in the current file.
1151
1148
1152
1149
1153
- Calling Python code
1154
- -------------------
1150
+ How to call Python code
1151
+ -----------------------
1155
1152
1156
1153
The rest of the advanced topics require you to write Python code
1157
1154
which lives inside your C file and modifies Argument Clinic's
@@ -1178,8 +1175,8 @@ variable to the C code::
1178
1175
/*[python checksum:...]*/
1179
1176
1180
1177
1181
- Using a "self converter"
1182
- ------------------------
1178
+ How to use the "self converter"
1179
+ -------------------------------
1183
1180
1184
1181
Argument Clinic automatically adds a "self" parameter for you
1185
1182
using a default converter. It automatically sets the ``type ``
@@ -1230,8 +1227,8 @@ type for ``self``, it's best to create your own converter, subclassing
1230
1227
[clinic start generated code]*/
1231
1228
1232
1229
1233
- Using a "defining class" converter
1234
- ----------------------------------
1230
+ How to use the "defining class" converter
1231
+ -----------------------------------------
1235
1232
1236
1233
Argument Clinic facilitates gaining access to the defining class of a method.
1237
1234
This is useful for :ref: `heap type <heap-types >` methods that need to fetch
@@ -1293,8 +1290,8 @@ state. Example from the ``setattro`` slot method in
1293
1290
See also :pep: `573 `.
1294
1291
1295
1292
1296
- Writing a custom converter
1297
- --------------------------
1293
+ How to write a custom converter
1294
+ -------------------------------
1298
1295
1299
1296
As we hinted at in the previous section... you can write your own converters!
1300
1297
A converter is simply a Python class that inherits from ``CConverter ``.
@@ -1385,8 +1382,8 @@ You can see more examples of custom converters in the CPython
1385
1382
source tree; grep the C files for the string ``CConverter ``.
1386
1383
1387
1384
1388
- Writing a custom return converter
1389
- ---------------------------------
1385
+ How to write a custom return converter
1386
+ --------------------------------------
1390
1387
1391
1388
Writing a custom return converter is much like writing
1392
1389
a custom converter. Except it's somewhat simpler, because return
@@ -1400,8 +1397,8 @@ specifically the implementation of ``CReturnConverter`` and
1400
1397
all its subclasses.
1401
1398
1402
1399
1403
- METH_O and METH_NOARGS
1404
- ----------------------
1400
+ How to convert `` METH_O `` and `` METH_NOARGS `` functions
1401
+ -------------------------------------------------------
1405
1402
1406
1403
To convert a function using ``METH_O ``, make sure the function's
1407
1404
single argument is using the ``object `` converter, and mark the
@@ -1422,8 +1419,8 @@ You can still use a self converter, a return converter, and specify
1422
1419
a ``type `` argument to the object converter for ``METH_O ``.
1423
1420
1424
1421
1425
- tp_new and tp_init functions
1426
- ----------------------------
1422
+ How to convert `` tp_new `` and `` tp_init `` functions
1423
+ ---------------------------------------------------
1427
1424
1428
1425
You can convert ``tp_new `` and ``tp_init `` functions. Just name
1429
1426
them ``__new__ `` or ``__init__ `` as appropriate. Notes:
@@ -1445,8 +1442,8 @@ them ``__new__`` or ``__init__`` as appropriate. Notes:
1445
1442
generated will throw an exception if it receives any.)
1446
1443
1447
1444
1448
- Changing and redirecting Clinic's output
1449
- ----------------------------------------
1445
+ How to change and redirect Clinic's output
1446
+ ------------------------------------------
1450
1447
1451
1448
It can be inconvenient to have Clinic's output interspersed with
1452
1449
your conventional hand-edited C code. Luckily, Clinic is configurable:
@@ -1728,8 +1725,8 @@ it in a Clinic block lets Clinic use its existing checksum functionality to ensu
1728
1725
the file was not modified by hand before it gets overwritten.
1729
1726
1730
1727
1731
- The #ifdef trick
1732
- ----------------
1728
+ How to use the `` #ifdef `` trick
1729
+ -------------------------------
1733
1730
1734
1731
If you're converting a function that isn't available on all platforms,
1735
1732
there's a trick you can use to make life a little easier. The existing
@@ -1809,8 +1806,8 @@ Argument Clinic added to your file (it'll be at the very bottom), then
1809
1806
move it above the ``PyMethodDef `` structure where that macro is used.
1810
1807
1811
1808
1812
- Using Argument Clinic in Python files
1813
- -------------------------------------
1809
+ How to use Argument Clinic in Python files
1810
+ ------------------------------------------
1814
1811
1815
1812
It's actually possible to use Argument Clinic to preprocess Python files.
1816
1813
There's no point to using Argument Clinic blocks, of course, as the output
0 commit comments