@@ -8,17 +8,20 @@ Argument Clinic How-To
8
8
9
9
:author: Larry Hastings
10
10
11
+ **Source code: ** :source: `Tools/clinic/clinic.py `.
11
12
12
13
.. topic :: Abstract
13
14
14
15
Argument Clinic is a preprocessor for CPython C files.
15
16
Its purpose is to automate all the boilerplate involved
16
17
with writing argument parsing code for "builtins",
17
18
module level functions, and class methods.
18
- This document is divided in three major sections:
19
+ This document is divided in four major sections:
19
20
20
21
* :ref: `clinic-background ` talks about the basic concepts and goals of
21
22
Argument Clinic.
23
+ * :ref: `clinic-reference ` describes the command-line interface and Argument
24
+ Clinic terminology.
22
25
* :ref: `clinic-tutorial ` guides you through all the steps required to
23
26
adapt an existing C function to Argument Clinic.
24
27
* :ref: `clinic-howtos ` details how to handle specific tasks.
@@ -93,68 +96,151 @@ and it should be able to do many interesting and smart
93
96
things with all the information you give it.
94
97
95
98
96
- Basic concepts and usage
97
- ------------------------
99
+ Basic concepts
100
+ --------------
98
101
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> `:
109
105
110
106
.. code-block :: none
111
107
112
108
/*[clinic input]
113
109
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 `:
116
111
117
112
.. code-block :: none
118
113
119
114
[clinic start generated code]*/
120
115
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 `:
129
122
130
123
.. code-block :: none
131
124
132
125
/*[clinic input]
133
126
... clinic input goes here ...
134
127
[clinic start generated code]*/
135
128
... clinic output goes here ...
136
- /*[clinic end generated code: checksum= ...]*/
129
+ /*[clinic end generated code: ...]*/
137
130
138
131
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.
158
244
159
245
160
246
.. _clinic-tutorial :
0 commit comments