Skip to content

Commit 2ad6990

Browse files
erlend-aaslandAA-TurnerAlexWaygoodezio-melotti
authored
Docs: Argument Clinic: Restructure "Basic concepts and usage" (#106981)
Split "Basic concepts and usage" into: - Reference - Terminology - CLI reference - Background - Basic concepts Co-authored-by: Adam Turner <[email protected]> Co-authored-by: Alex Waygood <[email protected]> Co-authored-by: Ezio Melotti <[email protected]>
1 parent 5923955 commit 2ad6990

File tree

2 files changed

+142
-50
lines changed

2 files changed

+142
-50
lines changed

Doc/howto/clinic.rst

+129-43
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ Argument Clinic How-To
88

99
:author: Larry Hastings
1010

11+
**Source code:** :source:`Tools/clinic/clinic.py`.
1112

1213
.. topic:: Abstract
1314

1415
Argument Clinic is a preprocessor for CPython C files.
1516
Its purpose is to automate all the boilerplate involved
1617
with writing argument parsing code for "builtins",
1718
module level functions, and class methods.
18-
This document is divided in three major sections:
19+
This document is divided in four major sections:
1920

2021
* :ref:`clinic-background` talks about the basic concepts and goals of
2122
Argument Clinic.
23+
* :ref:`clinic-reference` describes the command-line interface and Argument
24+
Clinic terminology.
2225
* :ref:`clinic-tutorial` guides you through all the steps required to
2326
adapt an existing C function to Argument Clinic.
2427
* :ref:`clinic-howtos` details how to handle specific tasks.
@@ -93,68 +96,151 @@ and it should be able to do many interesting and smart
9396
things with all the information you give it.
9497

9598

96-
Basic concepts and usage
97-
------------------------
99+
Basic concepts
100+
--------------
98101

99-
Argument Clinic ships with CPython; you'll find it in
100-
:source:`Tools/clinic/clinic.py`.
101-
If you run that script, specifying a C file as an argument:
102-
103-
.. code-block:: shell-session
104-
105-
$ python Tools/clinic/clinic.py foo.c
106-
107-
Argument Clinic will scan over the file looking for lines that
108-
look exactly like this:
102+
When Argument Clinic is run on a file, either via the :ref:`clinic-cli`
103+
or via ``make clinic``, it will scan over the input files looking for
104+
:term:`start lines <start line>`:
109105

110106
.. code-block:: none
111107
112108
/*[clinic input]
113109
114-
When it finds one, it reads everything up to a line that looks
115-
exactly like this:
110+
When it finds one, it reads everything up to the :term:`end line`:
116111

117112
.. code-block:: none
118113
119114
[clinic start generated code]*/
120115
121-
Everything in between these two lines is input for Argument Clinic.
122-
All of these lines, including the beginning and ending comment
123-
lines, are collectively called an Argument Clinic "block".
124-
125-
When Argument Clinic parses one of these blocks, it
126-
generates output. This output is rewritten into the C file
127-
immediately after the block, followed by a comment containing a checksum.
128-
The Argument Clinic block now looks like this:
116+
Everything in between these two lines is Argument Clinic :term:`input`.
117+
When Argument Clinic parses input, it generates :term:`output`.
118+
The output is rewritten into the C file immediately after the input,
119+
followed by a :term:`checksum line`.
120+
All of these lines, including the :term:`start line` and :term:`checksum line`,
121+
are collectively called an Argument Clinic :term:`block`:
129122

130123
.. code-block:: none
131124
132125
/*[clinic input]
133126
... clinic input goes here ...
134127
[clinic start generated code]*/
135128
... clinic output goes here ...
136-
/*[clinic end generated code: checksum=...]*/
129+
/*[clinic end generated code: ...]*/
137130
138131
If you run Argument Clinic on the same file a second time, Argument Clinic
139-
will discard the old output and write out the new output with a fresh checksum
140-
line. However, if the input hasn't changed, the output won't change either.
141-
142-
You should never modify the output portion of an Argument Clinic block. Instead,
143-
change the input until it produces the output you want. (That's the purpose of the
144-
checksum—to detect if someone changed the output, as these edits would be lost
145-
the next time Argument Clinic writes out fresh output.)
146-
147-
For the sake of clarity, here's the terminology we'll use with Argument Clinic:
148-
149-
* The first line of the comment (``/*[clinic input]``) is the *start line*.
150-
* The last line of the initial comment (``[clinic start generated code]*/``) is the *end line*.
151-
* The last line (``/*[clinic end generated code: checksum=...]*/``) is the *checksum line*.
152-
* In between the start line and the end line is the *input*.
153-
* In between the end line and the checksum line is the *output*.
154-
* All the text collectively, from the start line to the checksum line inclusively,
155-
is the *block*. (A block that hasn't been successfully processed by Argument
156-
Clinic yet doesn't have output or a checksum line, but it's still considered
157-
a block.)
132+
will discard the old :term:`output` and write out the new output with a fresh
133+
:term:`checksum line`.
134+
If the :term:`input` hasn't changed, the output won't change either.
135+
136+
.. note::
137+
138+
You should never modify the output of an Argument Clinic block,
139+
as any change will be lost in future Argument Clinic runs;
140+
Argument Clinic will detect an output checksum mismatch and regenerate the
141+
correct output.
142+
If you are not happy with the generated output,
143+
you should instead change the input until it produces the output you want.
144+
145+
146+
.. _clinic-reference:
147+
148+
Reference
149+
=========
150+
151+
152+
.. _clinic-terminology:
153+
154+
Terminology
155+
-----------
156+
157+
.. glossary::
158+
159+
start line
160+
The line ``/*[clinic input]``.
161+
This line marks the beginning of Argument Clinic input.
162+
Note that the *start line* opens a C block comment.
163+
164+
end line
165+
The line ``[clinic start generated code]*/``.
166+
The *end line* marks the _end_ of Argument Clinic :term:`input`,
167+
but at the same time marks the _start_ of Argument Clinic :term:`output`,
168+
thus the text *"clinic start start generated code"*
169+
Note that the *end line* closes the C block comment opened
170+
by the *start line*.
171+
172+
checksum
173+
A hash to distinguish unique :term:`inputs <input>`
174+
and :term:`outputs <output>`.
175+
176+
checksum line
177+
A line that looks like ``/*[clinic end generated code: ...]*/``.
178+
The three dots will be replaced by a :term:`checksum` generated from the
179+
:term:`input`, and a :term:`checksum` generated from the :term:`output`.
180+
The checksum line marks the end of Argument Clinic generated code,
181+
and is used by Argument Clinic to determine if it needs to regenerate
182+
output.
183+
184+
input
185+
The text between the :term:`start line` and the :term:`end line`.
186+
Note that the start and end lines open and close a C block comment;
187+
the *input* is thus a part of that same C block comment.
188+
189+
output
190+
The text between the :term:`end line` and the :term:`checksum line`.
191+
192+
block
193+
All text from the :term:`start line` to the :term:`checksum line` inclusively.
194+
195+
196+
.. _clinic-cli:
197+
198+
Command-line interface
199+
----------------------
200+
201+
The Argument Clinic :abbr:`CLI (Command-Line Interface)` is typically used to
202+
process a single source file, like this:
203+
204+
.. code-block:: shell-session
205+
206+
$ python3 ./Tools/clinic/clinic.py foo.c
207+
208+
The CLI supports the following options:
209+
210+
.. program:: ./Tools/clinic/clinic.py [-h] [-f] [-o OUTPUT] [-v] \
211+
[--converters] [--make] [--srcdir SRCDIR] [FILE ...]
212+
213+
.. option:: -h, --help
214+
215+
Print CLI usage.
216+
217+
.. option:: -f, --force
218+
219+
Force output regeneration.
220+
221+
.. option:: -o, --output OUTPUT
222+
223+
Redirect file output to OUTPUT
224+
225+
.. option:: -v, --verbose
226+
227+
Enable verbose mode.
228+
229+
.. option:: --converters
230+
231+
Print a list of all supported converters and return converters.
232+
233+
.. option:: --make
234+
235+
Walk :option:`--srcdir` to run over all relevant files.
236+
237+
.. option:: --srcdir SRCDIR
238+
239+
The directory tree to walk in :option:`--make` mode.
240+
241+
.. option:: FILE ...
242+
243+
The list of files to process.
158244

159245

160246
.. _clinic-tutorial:

Tools/clinic/clinic.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -5631,15 +5631,21 @@ def main(argv: list[str]) -> None:
56315631
signatures ("docstrings") for CPython builtins.
56325632
56335633
For more information see https://docs.python.org/3/howto/clinic.html""")
5634-
cmdline.add_argument("-f", "--force", action='store_true')
5635-
cmdline.add_argument("-o", "--output", type=str)
5636-
cmdline.add_argument("-v", "--verbose", action='store_true')
5637-
cmdline.add_argument("--converters", action='store_true')
5634+
cmdline.add_argument("-f", "--force", action='store_true',
5635+
help="force output regeneration")
5636+
cmdline.add_argument("-o", "--output", type=str,
5637+
help="redirect file output to OUTPUT")
5638+
cmdline.add_argument("-v", "--verbose", action='store_true',
5639+
help="enable verbose mode")
5640+
cmdline.add_argument("--converters", action='store_true',
5641+
help=("print a list of all supported converters "
5642+
"and return converters"))
56385643
cmdline.add_argument("--make", action='store_true',
5639-
help="Walk --srcdir to run over all relevant files.")
5644+
help="walk --srcdir to run over all relevant files")
56405645
cmdline.add_argument("--srcdir", type=str, default=os.curdir,
5641-
help="The directory tree to walk in --make mode.")
5642-
cmdline.add_argument("filename", type=str, nargs="*")
5646+
help="the directory tree to walk in --make mode")
5647+
cmdline.add_argument("filename", metavar="FILE", type=str, nargs="*",
5648+
help="the list of files to process")
56435649
ns = cmdline.parse_args(argv)
56445650

56455651
if ns.converters:

0 commit comments

Comments
 (0)