Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
376caf0
Issue #19659: Added documentation for Argument Clinic.
larryhastings Jan 4, 2014
5ca5d3a
Argument Clinic: fixed test suite, improved howto.
larryhastings Jan 5, 2014
9e47d75
Issue #20141: Improved Argument Clinic's support for the PyArg_Parse …
larryhastings Jan 7, 2014
01ae6c7
Issue #19273: The marker comments Argument Clinic uses have been changed
larryhastings Jan 7, 2014
1f988b1
Closes #20200: Argument Clinic custom converter example should be in a
zware Jan 10, 2014
5c53fd9
Issue #20214: Fixed a number of small issues and documentation errors in
larryhastings Jan 12, 2014
8d13753
Minor doc fix in Clinic howto.
larryhastings Jan 12, 2014
ef8fde7
Issue #20268: Argument Clinic now supports cloning the parameters
larryhastings Jan 15, 2014
ecea194
Issue #20226: Major improvements to Argument Clinic.
larryhastings Jan 16, 2014
71ff306
Issue #20287: Argument Clinic's output is now configurable, allowing
larryhastings Jan 18, 2014
8224acf
Doc improvements for Clinic howto "Goals" section.
larryhastings Jan 18, 2014
3619f5f
Issue #20294: Argument Clinic now supports argument parsing for __new…
larryhastings Jan 19, 2014
745625b
Doc fixes for Argument Clinic.
larryhastings Jan 22, 2014
4259315
GitHub-Issue-Link: https://github.com/python/cpython/issues/64547
ezio-melotti Jan 25, 2014
86a65e4
Documentation fixes, including fixing "suspicious" problems.
larryhastings Jan 26, 2014
416ba5f
including correctly generating code for Clinic blocks inside C
larryhastings Feb 1, 2014
6341d75
Fix Issue #21528 - Fix documentation typos
dstufft May 20, 2014
e3c5656
Issue #24000: Improved Argument Clinic's mapping of converters to legacy
larryhastings May 8, 2015
2914da8
Argument Clinic: added missing bit of info in howto
taleinat May 16, 2015
1b9d562
Issue #24232: Fix typos. Patch by Ville Skyttä.
berkerpeksag May 18, 2015
be8c565
Issue #24232: Fix typos. Patch by Ville Skyttä.
berkerpeksag May 18, 2015
f3fcaa4
Back porting changeset db302b88fdb6 to 3.4 branch, which fixed multip…
orsenthil Jun 15, 2015
62bda94
null merge with 3.4
orsenthil Jun 15, 2015
05046f9
null merge 3.4 to 3.5 (9a0c5ffe7420 merged 3.4 to default, bypassing …
ned-deily Jun 15, 2015
70bfe58
null merge 3.5 to default (9a0c5ffe7420 merged 3.4 to default, bypass…
ned-deily Jun 15, 2015
0ca4094
Issue #25626: Change zlib to accept Py_ssize_t and cap to UINT_MAX
vadmium Nov 20, 2015
c7b612a
Issue #25626: Merge zlib fix from 3.5
vadmium Nov 21, 2015
cfb1132
Issue #27130: Fix handling of buffers exceeding UINT_MAX in “zlib” mo…
vadmium Jul 23, 2016
4d20304
Issue #27626: Spelling fixes in docs, comments and internal names
vadmium Jul 28, 2016
3c7af20
Issue #26462: Doc: reduce literal_block warnings, fix syntax highligh…
vadmium Jul 26, 2016
2138267
Issue #27745: Fix some typos in Argument Clinic howto, by Lele Gaifax
vadmium Aug 12, 2016
83f8383
clinic: PY_LONG_LONG -> long long
benjaminp Sep 8, 2016
0147885
Issue 28753: Argument Clinic howto docfix, courtesy Julien Palard.
larryhastings Nov 20, 2016
2ff77e2
Change double hyphens (en dashes) to em (longer) dashes
vadmium Nov 21, 2016
a0b8e05
Merge doc fixups from 3.5
vadmium Nov 21, 2016
e066b36
Merge doc fixups from 3.6
vadmium Nov 21, 2016
d29e1f8
Change an en-dash to an em-dash.
serhiy-storchaka Nov 21, 2016
36e7d29
Issue 28753: Argument Clinic howto docfix, courtesy Julien Palard.
vadmium Dec 10, 2016
85fea54
Issue #28755: Improve syntax highlighting in Arg Clinic howto
vadmium Dec 10, 2016
8764b20
Issues #28755, #28753: Merge Arg Clinic howto from 3.5
vadmium Dec 10, 2016
4ad9136
Issue #28755: Merge Arg Clinic howto from 3.6
vadmium Dec 10, 2016
9a35ab4
bpo-29918: Add missed "const" modifiers in C API documentation. (#846)
serhiy-storchaka Mar 30, 2017
3fae62e
bpo-29596: Improve clinic howto documentation (GH-1710)
gfyoung Jun 6, 2017
3f5fc8f
Improve highlighting of some code blocks. (python/cpython#6401)
serhiy-storchaka Apr 8, 2018
40bb2c7
Docs: be less specific about python versions (python/cpython#6985)
grimreaper May 20, 2018
357a8f2
bpo-20260: Implement non-bitwise unsigned int converters for Argument…
serhiy-storchaka Jul 26, 2018
f9b4857
bpo-35042: Use the :pep: role where a PEP is specified (python/cpytho…
matrixise Oct 26, 2018
8c3d865
Doc: Replace the deprecated highlightlang directive by highlight. (py…
matrixise May 17, 2019
50f60a4
bpo-38600: NULL -> ``NULL``. (python/cpython#17001)
serhiy-storchaka Oct 30, 2019
403488f
bpo-42048: Clinic Howto: Document AC's defining_class converter (pyth…
Jan 20, 2021
4cd6fc6
bpo-45320: Remove long-deprecated inspect methods (python/cpython#28618)
hugovk Oct 20, 2021
9d1a7ad
bpo-46613: Add PyType_GetModuleByDef to the public API (python/cpytho…
encukou Feb 11, 2022
03d39ac
python/cpython#92536: PEP 623: Remove wstr and legacy APIs from Unico…
methane May 12, 2022
7980ac5
Document Py_ssize_t. (python/cpython#92512)
JulienPalard May 13, 2022
d2514e5
Docs: remove redundant "adverb-adjective" hyphens from compound modif…
nedbat Jul 5, 2022
d6ff211
python/cpython#95007: Remove the NoneType return converter from Argum…
noamcohen97 Aug 1, 2022
5f6ac54
python/cpython#97956: Mention `generate_global_objects.py` in `AC How…
sobolevn Oct 7, 2022
1196c45
docs: Change links to label refs (python/cpython#98454)
slateny Oct 26, 2022
9243c7a
python/cpython#98763: Prefer "python" over "python3" for command line…
felixxm Jan 11, 2023
59eef01
python/cpython#64658: Expand Argument Clinic return converter docs (p…
erlend-aasland May 5, 2023
d8a7663
python/cpython#104389: Add 'unused' keyword to Argument Clinic C conv…
erlend-aasland May 12, 2023
b247a69
Docs: Normalize Argument Clinic How-To section capitalization (python…
erlend-aasland Jul 15, 2023
9ca360c
Docs: Normalise Argument Clinic advanced topics headings (python/cpyt…
erlend-aasland Jul 18, 2023
4c56015
Docs: Argument Clinic: Group guides about default values (python/cpyt…
erlend-aasland Jul 18, 2023
60a674b
Docs: Argument Clinic: Add Background and Tutorial top-level sections…
erlend-aasland Jul 21, 2023
f73589c
Docs: Remove duplicate word in Argument Clinic howto heading (python/…
hakancelikdev Jul 24, 2023
4eda298
Docs: Add missing markup to Argument Clinic docs (python/cpython#106876)
erlend-aasland Jul 24, 2023
f4d2138
Docs: Remove the numbered steps from the Argument Clinic tutorial (py…
erlend-aasland Jul 26, 2023
8405f89
Docs: Argument Clinic: Restructure "Basic concepts and usage" (python…
erlend-aasland Jul 26, 2023
64b07f5
python/cpython#107507: Replace 'The goals of Argument Clinic' with a …
erlend-aasland Jul 31, 2023
1c3fb9b
Docs: Argument Clinic: Improve 'How to write a custom converter' (pyt…
erlend-aasland Aug 5, 2023
ce3b69d
Docs: Argument Clinic: Move the CConverter class to the reference (py…
erlend-aasland Aug 7, 2023
eec5f46
python/cpython#95065: Add Argument Clinic support for deprecating pos…
erlend-aasland Aug 7, 2023
6a0051f
python/cpython#86457: Add docs for Argument Clinic @text_signature di…
erlend-aasland Aug 8, 2023
8dd6936
python/cpython#104683: Add --exclude option to Argument Clinic CLI (p…
erlend-aasland Aug 8, 2023
067ee64
Docs: clean up Argument Clinic howto's (python/cpython#107797)
erlend-aasland Aug 9, 2023
f62db14
Add clinic.rst with history
erlend-aasland Aug 9, 2023
b091bbd
Add clinic.rst to Development Tools
erlend-aasland Aug 9, 2023
7e4977f
Adapt clinic.rst
erlend-aasland Aug 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Issue #20214: Fixed a number of small issues and documentation errors in
Argument Clinic (see issue for details).

GitHub-Issue-Link: python/cpython#64413
  • Loading branch information
larryhastings committed Jan 12, 2014
commit 5c53fd963b84ea38608a59656675c189f6206a35
140 changes: 120 additions & 20 deletions Doc/howto/clinic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ convert a function to work with it. Let's dive in!
support all of these scenarios. But these are advanced
topics--let's do something simpler for your first function.

Also, if the function has multiple calls to :c:func:`PyArg_ParseTuple`
or :c:func:`PyArg_ParseTupleAndKeywords` where it supports different
types for the same argument, or if the function uses something besides
PyArg_Parse functions to parse its arguments, it probably
isn't suitable for conversion to Argument Clinic. Argument Clinic
doesn't support generic functions or polymorphic parameters.

3. Add the following boilerplate above the function, creating our block::

/*[clinic input]
Expand Down Expand Up @@ -170,6 +177,11 @@ convert a function to work with it. Let's dive in!
Write a pickled representation of obj to the open file.
[clinic start generated code]*/

The name of the class and module should be the same as the one
seen by Python. Check the name defined in the :c:type:`PyModuleDef`
or :c:type:`PyTypeObject` as appropriate.



8. Declare each of the parameters to the function. Each parameter
should get its own line. All the parameter lines should be
Expand Down Expand Up @@ -455,6 +467,28 @@ convert a function to work with it. Let's dive in!
Advanced Topics
===============

Now that you've had some experience working with Argument Clinic, it's time
for some advanced topics.


Symbolic default values
-----------------------

The default value you provide for a parameter can't be any arbitrary
expression. Currently the following are explicitly supported:

* Numeric constants (integer and float)
* String constants
* ``True``, ``False``, and ``None``
* Simple symbolic constants like ``sys.maxsize``, which must
start with the name of the module

In case you're curious, this is implemented in ``from_builtin()``
in ``Lib/inspect.py``.

(In the future, this may need to get even more elaborate,
to allow full expressions like ``CONSTANT - 1``.)


Renaming the C functions generated by Argument Clinic
-----------------------------------------------------
Expand All @@ -479,6 +513,29 @@ The base function would now be named ``pickler_dumper()``,
and the impl function would now be named ``pickler_dumper_impl()``.


The NULL default value
----------------------

For string and object parameters, you can set them to ``None`` to indicate
that there is no default. However, that means the C variable will be
initialized to ``Py_None``. For convenience's sakes, there's a special
value called ``NULL`` for just this case: from Python's perspective it
behaves like a default value of ``None``, but the C variable is initialized
with ``NULL``.


Converting functions using PyArg_UnpackTuple
--------------------------------------------

To convert a function parsing its arguments with :c:func:`PyArg_UnpackTuple`,
simply write out all the arguments, specifying each as an ``object``. You
may specify the ``type`` argument to cast the type as appropriate. All
arguments should be marked positional-only (add a ``/`` on a line by itself
after the last argument).

Currently the generated code will use :c:func:`PyArg_ParseTuple`, but this
will change soon.

Optional Groups
---------------

Expand Down Expand Up @@ -570,8 +627,8 @@ To save time, and to minimize how much you need to learn
to achieve your first port to Argument Clinic, the walkthrough above tells
you to use "legacy converters". "Legacy converters" are a convenience,
designed explicitly to make porting existing code to Argument Clinic
easier. And to be clear, their use is entirely acceptable when porting
code for Python 3.4.
easier. And to be clear, their use is acceptable when porting code for
Python 3.4.

However, in the long term we probably want all our blocks to
use Argument Clinic's real syntax for converters. Why? A couple
Expand All @@ -585,8 +642,8 @@ reasons:
restricted to what :c:func:`PyArg_ParseTuple` supports; this flexibility
won't be available to parameters using legacy converters.

Therefore, if you don't mind a little extra effort, you should consider
using normal converters instead of legacy converters.
Therefore, if you don't mind a little extra effort, please use the normal
converters instead of legacy converters.

In a nutshell, the syntax for Argument Clinic (non-legacy) converters
looks like a Python function call. However, if there are no explicit
Expand All @@ -597,12 +654,19 @@ the same converters.
All arguments to Argument Clinic converters are keyword-only.
All Argument Clinic converters accept the following arguments:

``doc_default``
If the parameter takes a default value, normally this value is also
provided in the ``inspect.Signature`` metadata, and displayed in the
docstring. ``doc_default`` lets you override the value used in these
two places: pass in a string representing the Python value you wish
to use in these two contexts.
``py_default``
The default value for this parameter when defined in Python.
Specifically, the value that will be used in the ``inspect.Signature``
string.
If a default value is specified for the parameter, defaults to
``repr(default)``, else defaults to ``None``.
Specified as a string.

``c_default``
The default value for this parameter when defined in C.
Specifically, this will be the initializer for the variable declared
in the "parse function".
Specified as a string.

``required``
If a parameter takes a default value, Argument Clinic infers that the
Expand All @@ -612,6 +676,9 @@ All Argument Clinic converters accept the following arguments:
Clinic that this parameter is not optional, even if it has a default
value.

(The need for ``required`` may be obviated by ``c_default``, which is
newer but arguably a better solution.)

``annotation``
The annotation value for this parameter. Not currently supported,
because PEP 8 mandates that the Python library may not use
Expand All @@ -634,10 +701,11 @@ on the right is the text you'd replace it with.
``'et'`` ``str(encoding='name_of_encoding', types='bytes bytearray str')``
``'f'`` ``float``
``'h'`` ``short``
``'H'`` ``unsigned_short``
``'H'`` ``unsigned_short(bitwise=True)``
``'i'`` ``int``
``'I'`` ``unsigned_int``
``'K'`` ``unsigned_PY_LONG_LONG``
``'I'`` ``unsigned_int(bitwise=True)``
``'k'`` ``unsigned_long(bitwise=True)``
``'K'`` ``unsigned_PY_LONG_LONG(bitwise=True)``
``'L'`` ``PY_LONG_LONG``
``'n'`` ``Py_ssize_t``
``'O!'`` ``object(subclass_of='&PySomething_Type')``
Expand Down Expand Up @@ -681,6 +749,14 @@ available. For each converter it'll show you all the parameters
it accepts, along with the default value for each parameter.
Just run ``Tools/clinic/clinic.py --converters`` to see the full list.

Py_buffer
---------

When using the ``Py_buffer`` converter
(or the ``'s*'``, ``'w*'``, ``'*y'``, or ``'z*'`` legacy converters)
note that the code Argument Clinic generates for you will automatically
call :c:func:`PyBuffer_Release` on the buffer for you.


Advanced converters
-------------------
Expand Down Expand Up @@ -745,15 +821,26 @@ encode the value you return like normal.

Currently Argument Clinic supports only a few return converters::

bool
int
unsigned int
long
unsigned int
size_t
Py_ssize_t
float
double
DecodeFSDefault

None of these take parameters. For the first three, return -1 to indicate
error. For ``DecodeFSDefault``, the return type is ``char *``; return a NULL
pointer to indicate an error.

(There's also an experimental ``NoneType`` converter, which lets you
return ``Py_None`` on success or ``NULL`` on failure, without having
to increment the reference count on ``Py_None``. I'm not sure it adds
enough clarity to be worth using.)

To see all the return converters Argument Clinic supports, along with
their parameters (if any),
just run ``Tools/clinic/clinic.py --converters`` for the full list.
Expand Down Expand Up @@ -873,13 +960,6 @@ to specify in your subclass. Here's the current list:
The Python default value for this parameter, as a Python value.
Or the magic value ``unspecified`` if there is no default.

``doc_default``
``default`` as it should appear in the documentation,
as a string.
Or ``None`` if there is no default.
This string, when run through ``eval()``, should produce
a Python value.

``py_default``
``default`` as it should appear in Python code,
as a string.
Expand Down Expand Up @@ -951,6 +1031,26 @@ write your own return converter, please read ``Tools/clinic/clinic.py``,
specifically the implementation of ``CReturnConverter`` and
all its subclasses.

METH_O and METH_NOARGS
----------------------------------------------

To convert a function using ``METH_O``, make sure the function's
single argument is using the ``object`` converter, and mark the
arguments as positional-only::

/*[clinic input]
meth_o_sample

argument: object
/
[clinic start generated code]*/


To convert a function using ``METH_NOARGS``, just don't specify
any arguments.

You can still use a self converter, a return converter, and specify
a ``type`` argument to the object converter for ``METH_O``.

Using Argument Clinic in Python files
-------------------------------------
Expand Down