-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclinic.html
More file actions
1809 lines (1747 loc) · 129 KB
/
clinic.html
File metadata and controls
1809 lines (1747 loc) · 129 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_TW">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Argument Clinic How-To — Python 3.7.0 說明文件</title>
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/translations.js"></script>
<script type="text/javascript" src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.7.0 說明文件 中搜尋"
href="../_static/opensearch.xml"/>
<link rel="author" title="關於這些文件" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜尋" href="../search.html" />
<link rel="copyright" title="Copyright" href="../copyright.html" />
<link rel="next" title="Instrumenting CPython with DTrace and SystemTap" href="instrumentation.html" />
<link rel="prev" title="ipaddress模組介紹" href="ipaddress.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
<link rel="canonical" href="https://docs.python.org/3/howto/clinic.html" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/switchers.js"></script>
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>瀏覽</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模組索引"
>模組</a> |</li>
<li class="right" >
<a href="instrumentation.html" title="Instrumenting CPython with DTrace and SystemTap"
accesskey="N">下一頁</a> |</li>
<li class="right" >
<a href="ipaddress.html" title="ipaddress模組介紹"
accesskey="P">上一頁</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<span class="language_switcher_placeholder">zh_TW</span>
<span class="version_switcher_placeholder">3.7.0</span>
<a href="../index.html">Documentation </a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python HOWTOs</a> »</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="argument-clinic-how-to">
<h1>Argument Clinic How-To<a class="headerlink" href="#argument-clinic-how-to" title="本標題的永久連結">¶</a></h1>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">author:</th><td class="field-body">Larry Hastings</td>
</tr>
</tbody>
</table>
<div class="topic">
<p class="topic-title first">Abstract</p>
<p>Argument Clinic is a preprocessor for CPython C files.
Its purpose is to automate all the boilerplate involved
with writing argument parsing code for 「builtins」.
This document shows you how to convert your first C
function to work with Argument Clinic, and then introduces
some advanced topics on Argument Clinic usage.</p>
<p>Currently Argument Clinic is considered internal-only
for CPython. Its use is not supported for files outside
CPython, and no guarantees are made regarding backwards
compatibility for future versions. In other words: if you
maintain an external C extension for CPython, you’re welcome
to experiment with Argument Clinic in your own code. But the
version of Argument Clinic that ships with the next version
of CPython <em>could</em> be totally incompatible and break all your code.</p>
</div>
<div class="section" id="the-goals-of-argument-clinic">
<h2>The Goals Of Argument Clinic<a class="headerlink" href="#the-goals-of-argument-clinic" title="本標題的永久連結">¶</a></h2>
<p>Argument Clinic’s primary goal
is to take over responsibility for all argument parsing code
inside CPython. This means that, when you convert a function
to work with Argument Clinic, that function should no longer
do any of its own argument parsing—the code generated by
Argument Clinic should be a 「black box」 to you, where CPython
calls in at the top, and your code gets called at the bottom,
with <code class="docutils literal notranslate"><span class="pre">PyObject</span> <span class="pre">*args</span></code> (and maybe <code class="docutils literal notranslate"><span class="pre">PyObject</span> <span class="pre">*kwargs</span></code>)
magically converted into the C variables and types you need.</p>
<p>In order for Argument Clinic to accomplish its primary goal,
it must be easy to use. Currently, working with CPython’s
argument parsing library is a chore, requiring maintaining
redundant information in a surprising number of places.
When you use Argument Clinic, you don’t have to repeat yourself.</p>
<p>Obviously, no one would want to use Argument Clinic unless
it’s solving their problem—and without creating new problems of
its own.
So it’s paramount that Argument Clinic generate correct code.
It’d be nice if the code was faster, too, but at the very least
it should not introduce a major speed regression. (Eventually Argument
Clinic <em>should</em> make a major speedup possible—we could
rewrite its code generator to produce tailor-made argument
parsing code, rather than calling the general-purpose CPython
argument parsing library. That would make for the fastest
argument parsing possible!)</p>
<p>Additionally, Argument Clinic must be flexible enough to
work with any approach to argument parsing. Python has
some functions with some very strange parsing behaviors;
Argument Clinic’s goal is to support all of them.</p>
<p>Finally, the original motivation for Argument Clinic was
to provide introspection 「signatures」 for CPython builtins.
It used to be, the introspection query functions would throw
an exception if you passed in a builtin. With Argument
Clinic, that’s a thing of the past!</p>
<p>One idea you should keep in mind, as you work with
Argument Clinic: the more information you give it, the
better job it’ll be able to do.
Argument Clinic is admittedly relatively simple right
now. But as it evolves it will get more sophisticated,
and it should be able to do many interesting and smart
things with all the information you give it.</p>
</div>
<div class="section" id="basic-concepts-and-usage">
<h2>Basic Concepts And Usage<a class="headerlink" href="#basic-concepts-and-usage" title="本標題的永久連結">¶</a></h2>
<p>Argument Clinic ships with CPython; you’ll find it in <code class="docutils literal notranslate"><span class="pre">Tools/clinic/clinic.py</span></code>.
If you run that script, specifying a C file as an argument:</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> python3 Tools/clinic/clinic.py foo.c
</pre></div>
</div>
<p>Argument Clinic will scan over the file looking for lines that
look exactly like this:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>/*[clinic input]
</pre></div>
</div>
<p>When it finds one, it reads everything up to a line that looks
exactly like this:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>[clinic start generated code]*/
</pre></div>
</div>
<p>Everything in between these two lines is input for Argument Clinic.
All of these lines, including the beginning and ending comment
lines, are collectively called an Argument Clinic 「block」.</p>
<p>When Argument Clinic parses one of these blocks, it
generates output. This output is rewritten into the C file
immediately after the block, followed by a comment containing a checksum.
The Argument Clinic block now looks like this:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>/*[clinic input]
... clinic input goes here ...
[clinic start generated code]*/
... clinic output goes here ...
/*[clinic end generated code: checksum=...]*/
</pre></div>
</div>
<p>If you run Argument Clinic on the same file a second time, Argument Clinic
will discard the old output and write out the new output with a fresh checksum
line. However, if the input hasn’t changed, the output won’t change either.</p>
<p>You should never modify the output portion of an Argument Clinic block. Instead,
change the input until it produces the output you want. (That’s the purpose of the
checksum—to detect if someone changed the output, as these edits would be lost
the next time Argument Clinic writes out fresh output.)</p>
<p>For the sake of clarity, here’s the terminology we’ll use with Argument Clinic:</p>
<ul class="simple">
<li>The first line of the comment (<code class="docutils literal notranslate"><span class="pre">/*[clinic</span> <span class="pre">input]</span></code>) is the <em>start line</em>.</li>
<li>The last line of the initial comment (<code class="docutils literal notranslate"><span class="pre">[clinic</span> <span class="pre">start</span> <span class="pre">generated</span> <span class="pre">code]*/</span></code>) is the <em>end line</em>.</li>
<li>The last line (<code class="docutils literal notranslate"><span class="pre">/*[clinic</span> <span class="pre">end</span> <span class="pre">generated</span> <span class="pre">code:</span> <span class="pre">checksum=...]*/</span></code>) is the <em>checksum line</em>.</li>
<li>In between the start line and the end line is the <em>input</em>.</li>
<li>In between the end line and the checksum line is the <em>output</em>.</li>
<li>All the text collectively, from the start line to the checksum line inclusively,
is the <em>block</em>. (A block that hasn’t been successfully processed by Argument
Clinic yet doesn’t have output or a checksum line, but it’s still considered
a block.)</li>
</ul>
</div>
<div class="section" id="converting-your-first-function">
<h2>Converting Your First Function<a class="headerlink" href="#converting-your-first-function" title="本標題的永久連結">¶</a></h2>
<p>The best way to get a sense of how Argument Clinic works is to
convert a function to work with it. Here, then, are the bare
minimum steps you’d need to follow to convert a function to
work with Argument Clinic. Note that for code you plan to
check in to CPython, you really should take the conversion farther,
using some of the advanced concepts you’ll see later on in
the document (like 「return converters」 and 「self converters」).
But we’ll keep it simple for this walkthrough so you can learn.</p>
<p>Let’s dive in!</p>
<ol class="arabic" start="0">
<li><p class="first">Make sure you’re working with a freshly updated checkout
of the CPython trunk.</p>
</li>
<li><p class="first">Find a Python builtin that calls either <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a>
or <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTupleAndKeywords" title="PyArg_ParseTupleAndKeywords"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code></a>, and hasn’t been converted
to work with Argument Clinic yet.
For my example I’m using <code class="docutils literal notranslate"><span class="pre">_pickle.Pickler.dump()</span></code>.</p>
</li>
<li><p class="first">If the call to the <code class="docutils literal notranslate"><span class="pre">PyArg_Parse</span></code> function uses any of the
following format units:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>O&
O!
es
es#
et
et#
</pre></div>
</div>
<p>or if it has multiple calls to <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a>,
you should choose a different function. Argument Clinic <em>does</em>
support all of these scenarios. But these are advanced
topics—let’s do something simpler for your first function.</p>
<p>Also, if the function has multiple calls to <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a>
or <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTupleAndKeywords" title="PyArg_ParseTupleAndKeywords"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code></a> 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.</p>
</li>
<li><p class="first">Add the following boilerplate above the function, creating our block:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
<span class="cm">[clinic start generated code]*/</span>
</pre></div>
</div>
</li>
<li><p class="first">Cut the docstring and paste it in between the <code class="docutils literal notranslate"><span class="pre">[clinic]</span></code> lines,
removing all the junk that makes it a properly quoted C string.
When you’re done you should have just the text, based at the left
margin, with no line wider than 80 characters.
(Argument Clinic will preserve indents inside the docstring.)</p>
<p>If the old docstring had a first line that looked like a function
signature, throw that line away. (The docstring doesn’t need it
anymore—when you use <code class="docutils literal notranslate"><span class="pre">help()</span></code> on your builtin in the future,
the first line will be built automatically based on the function’s
signature.)</p>
<p>Sample:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
<span class="cm">Write a pickled representation of obj to the open file.</span>
<span class="cm">[clinic start generated code]*/</span>
</pre></div>
</div>
</li>
<li><p class="first">If your docstring doesn’t have a 「summary」 line, Argument Clinic will
complain. So let’s make sure it has one. The 「summary」 line should
be a paragraph consisting of a single 80-column line
at the beginning of the docstring.</p>
<p>(Our example docstring consists solely of a summary line, so the sample
code doesn’t have to change for this step.)</p>
</li>
<li><p class="first">Above the docstring, enter the name of the function, followed
by a blank line. This should be the Python name of the function,
and should be the full dotted path
to the function—it should start with the name of the module,
include any sub-modules, and if the function is a method on
a class it should include the class name too.</p>
<p>Sample:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
<span class="cm">_pickle.Pickler.dump</span>
<span class="cm">Write a pickled representation of obj to the open file.</span>
<span class="cm">[clinic start generated code]*/</span>
</pre></div>
</div>
</li>
<li><p class="first">If this is the first time that module or class has been used with Argument
Clinic in this C file,
you must declare the module and/or class. Proper Argument Clinic hygiene
prefers declaring these in a separate block somewhere near the
top of the C file, in the same way that include files and statics go at
the top. (In our sample code we’ll just show the two blocks next to
each other.)</p>
<p>The name of the class and module should be the same as the one
seen by Python. Check the name defined in the <a class="reference internal" href="../c-api/module.html#c.PyModuleDef" title="PyModuleDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyModuleDef</span></code></a>
or <a class="reference internal" href="../c-api/type.html#c.PyTypeObject" title="PyTypeObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyTypeObject</span></code></a> as appropriate.</p>
<p>When you declare a class, you must also specify two aspects of its type
in C: the type declaration you’d use for a pointer to an instance of
this class, and a pointer to the <a class="reference internal" href="../c-api/type.html#c.PyTypeObject" title="PyTypeObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyTypeObject</span></code></a> for this class.</p>
<p>Sample:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
<span class="cm">module _pickle</span>
<span class="cm">class _pickle.Pickler "PicklerObject *" "&Pickler_Type"</span>
<span class="cm">[clinic start generated code]*/</span>
<span class="cm">/*[clinic input]</span>
<span class="cm">_pickle.Pickler.dump</span>
<span class="cm">Write a pickled representation of obj to the open file.</span>
<span class="cm">[clinic start generated code]*/</span>
</pre></div>
</div>
</li>
<li><p class="first">Declare each of the parameters to the function. Each parameter
should get its own line. All the parameter lines should be
indented from the function name and the docstring.</p>
<p>The general form of these parameter lines is as follows:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>name_of_parameter: converter
</pre></div>
</div>
<p>If the parameter has a default value, add that after the
converter:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>name_of_parameter: converter = default_value
</pre></div>
</div>
<p>Argument Clinic’s support for 「default values」 is quite sophisticated;
please see <a class="reference internal" href="#default-values"><span class="std std-ref">the section below on default values</span></a>
for more information.</p>
<p>Add a blank line below the parameters.</p>
<p>What’s a 「converter」? It establishes both the type
of the variable used in C, and the method to convert the Python
value into a C value at runtime.
For now you’re going to use what’s called a 「legacy converter」—a
convenience syntax intended to make porting old code into Argument
Clinic easier.</p>
<p>For each parameter, copy the 「format unit」 for that
parameter from the <code class="docutils literal notranslate"><span class="pre">PyArg_Parse()</span></code> format argument and
specify <em>that</em> as its converter, as a quoted
string. (「format unit」 is the formal name for the one-to-three
character substring of the <code class="docutils literal notranslate"><span class="pre">format</span></code> parameter that tells
the argument parsing function what the type of the variable
is and how to convert it. For more on format units please
see <a class="reference internal" href="../c-api/arg.html#arg-parsing"><span class="std std-ref">Parsing arguments and building values</span></a>.)</p>
<p>For multicharacter format units like <code class="docutils literal notranslate"><span class="pre">z#</span></code>, use the
entire two-or-three character string.</p>
<p>Sample:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span> <span class="cm">/*[clinic input]</span>
<span class="cm"> module _pickle</span>
<span class="cm"> class _pickle.Pickler "PicklerObject *" "&Pickler_Type"</span>
<span class="cm"> [clinic start generated code]*/</span>
<span class="cm">/*[clinic input]</span>
<span class="cm"> _pickle.Pickler.dump</span>
<span class="cm"> obj: 'O'</span>
<span class="cm">Write a pickled representation of obj to the open file.</span>
<span class="cm">[clinic start generated code]*/</span>
</pre></div>
</div>
</li>
<li><p class="first">If your function has <code class="docutils literal notranslate"><span class="pre">|</span></code> in the format string, meaning some
parameters have default values, you can ignore it. Argument
Clinic infers which parameters are optional based on whether
or not they have default values.</p>
<p>If your function has <code class="docutils literal notranslate"><span class="pre">$</span></code> in the format string, meaning it
takes keyword-only arguments, specify <code class="docutils literal notranslate"><span class="pre">*</span></code> on a line by
itself before the first keyword-only argument, indented the
same as the parameter lines.</p>
<p>(<code class="docutils literal notranslate"><span class="pre">_pickle.Pickler.dump</span></code> has neither, so our sample is unchanged.)</p>
</li>
<li><p class="first">If the existing C function calls <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a>
(as opposed to <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTupleAndKeywords" title="PyArg_ParseTupleAndKeywords"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code></a>), then all its
arguments are positional-only.</p>
<p>To mark all parameters as positional-only in Argument Clinic,
add a <code class="docutils literal notranslate"><span class="pre">/</span></code> on a line by itself after the last parameter,
indented the same as the parameter lines.</p>
<p>Currently this is all-or-nothing; either all parameters are
positional-only, or none of them are. (In the future Argument
Clinic may relax this restriction.)</p>
<p>Sample:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
<span class="cm">module _pickle</span>
<span class="cm">class _pickle.Pickler "PicklerObject *" "&Pickler_Type"</span>
<span class="cm">[clinic start generated code]*/</span>
<span class="cm">/*[clinic input]</span>
<span class="cm">_pickle.Pickler.dump</span>
<span class="cm"> obj: 'O'</span>
<span class="cm"> /</span>
<span class="cm">Write a pickled representation of obj to the open file.</span>
<span class="cm">[clinic start generated code]*/</span>
</pre></div>
</div>
</li>
<li><p class="first">It’s helpful to write a per-parameter docstring for each parameter.
But per-parameter docstrings are optional; you can skip this step
if you prefer.</p>
<p>Here’s how to add a per-parameter docstring. The first line
of the per-parameter docstring must be indented further than the
parameter definition. The left margin of this first line establishes
the left margin for the whole per-parameter docstring; all the text
you write will be outdented by this amount. You can write as much
text as you like, across multiple lines if you wish.</p>
<p>Sample:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
<span class="cm">module _pickle</span>
<span class="cm">class _pickle.Pickler "PicklerObject *" "&Pickler_Type"</span>
<span class="cm">[clinic start generated code]*/</span>
<span class="cm">/*[clinic input]</span>
<span class="cm">_pickle.Pickler.dump</span>
<span class="cm"> obj: 'O'</span>
<span class="cm"> The object to be pickled.</span>
<span class="cm"> /</span>
<span class="cm">Write a pickled representation of obj to the open file.</span>
<span class="cm">[clinic start generated code]*/</span>
</pre></div>
</div>
</li>
<li><p class="first">Save and close the file, then run <code class="docutils literal notranslate"><span class="pre">Tools/clinic/clinic.py</span></code> on
it. With luck everything worked—your block now has output, and
a <code class="docutils literal notranslate"><span class="pre">.c.h</span></code> file has been generated! Reopen the file in your
text editor to see:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
<span class="cm">_pickle.Pickler.dump</span>
<span class="cm"> obj: 'O'</span>
<span class="cm"> The object to be pickled.</span>
<span class="cm"> /</span>
<span class="cm">Write a pickled representation of obj to the open file.</span>
<span class="cm">[clinic start generated code]*/</span>
<span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span>
<span class="n">_pickle_Pickler_dump</span><span class="p">(</span><span class="n">PicklerObject</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">obj</span><span class="p">)</span>
<span class="cm">/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/</span>
</pre></div>
</div>
<p>Obviously, if Argument Clinic didn’t produce any output, it’s because
it found an error in your input. Keep fixing your errors and retrying
until Argument Clinic processes your file without complaint.</p>
<p>For readability, most of the glue code has been generated to a <code class="docutils literal notranslate"><span class="pre">.c.h</span></code>
file. You’ll need to include that in your original <code class="docutils literal notranslate"><span class="pre">.c</span></code> file,
typically right after the clinic module block:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">"clinic/_pickle.c.h"</span><span class="cp"></span>
</pre></div>
</div>
</li>
<li><p class="first">Double-check that the argument-parsing code Argument Clinic generated
looks basically the same as the existing code.</p>
<p>First, ensure both places use the same argument-parsing function.
The existing code must call either
<a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a> or <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTupleAndKeywords" title="PyArg_ParseTupleAndKeywords"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code></a>;
ensure that the code generated by Argument Clinic calls the
<em>exact</em> same function.</p>
<p>Second, the format string passed in to <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a> or
<a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTupleAndKeywords" title="PyArg_ParseTupleAndKeywords"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code></a> should be <em>exactly</em> the same
as the hand-written one in the existing function, up to the colon
or semi-colon.</p>
<p>(Argument Clinic always generates its format strings
with a <code class="docutils literal notranslate"><span class="pre">:</span></code> followed by the name of the function. If the
existing code’s format string ends with <code class="docutils literal notranslate"><span class="pre">;</span></code>, to provide
usage help, this change is harmless—don’t worry about it.)</p>
<p>Third, for parameters whose format units require two arguments
(like a length variable, or an encoding string, or a pointer
to a conversion function), ensure that the second argument is
<em>exactly</em> the same between the two invocations.</p>
<p>Fourth, inside the output portion of the block you’ll find a preprocessor
macro defining the appropriate static <a class="reference internal" href="../c-api/structures.html#c.PyMethodDef" title="PyMethodDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMethodDef</span></code></a> structure for
this builtin:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#define __PICKLE_PICKLER_DUMP_METHODDEF \</span>
<span class="cp">{"dump", (PyCFunction)__pickle_Pickler_dump, METH_O, __pickle_Pickler_dump__doc__},</span>
</pre></div>
</div>
<p>This static structure should be <em>exactly</em> the same as the existing static
<a class="reference internal" href="../c-api/structures.html#c.PyMethodDef" title="PyMethodDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMethodDef</span></code></a> structure for this builtin.</p>
<p>If any of these items differ in <em>any way</em>,
adjust your Argument Clinic function specification and rerun
<code class="docutils literal notranslate"><span class="pre">Tools/clinic/clinic.py</span></code> until they <em>are</em> the same.</p>
</li>
<li><p class="first">Notice that the last line of its output is the declaration
of your 「impl」 function. This is where the builtin’s implementation goes.
Delete the existing prototype of the function you’re modifying, but leave
the opening curly brace. Now delete its argument parsing code and the
declarations of all the variables it dumps the arguments into.
Notice how the Python arguments are now arguments to this impl function;
if the implementation used different names for these variables, fix it.</p>
<p>Let’s reiterate, just because it’s kind of weird. Your code should now
look like this:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span> <span class="n">return_type</span>
<span class="nf">your_function_impl</span><span class="p">(...)</span>
<span class="cm">/*[clinic end generated code: checksum=...]*/</span>
<span class="p">{</span>
<span class="p">...</span>
</pre></div>
</div>
<p>Argument Clinic generated the checksum line and the function prototype just
above it. You should write the opening (and closing) curly braces for the
function, and the implementation inside.</p>
<p>Sample:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
<span class="cm">module _pickle</span>
<span class="cm">class _pickle.Pickler "PicklerObject *" "&Pickler_Type"</span>
<span class="cm">[clinic start generated code]*/</span>
<span class="cm">/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/</span>
<span class="cm">/*[clinic input]</span>
<span class="cm">_pickle.Pickler.dump</span>
<span class="cm"> obj: 'O'</span>
<span class="cm"> The object to be pickled.</span>
<span class="cm"> /</span>
<span class="cm">Write a pickled representation of obj to the open file.</span>
<span class="cm">[clinic start generated code]*/</span>
<span class="n">PyDoc_STRVAR</span><span class="p">(</span><span class="n">__pickle_Pickler_dump__doc__</span><span class="p">,</span>
<span class="s">"Write a pickled representation of obj to the open file.</span><span class="se">\n</span><span class="s">"</span>
<span class="s">"</span><span class="se">\n</span><span class="s">"</span>
<span class="p">...</span>
<span class="k">static</span> <span class="n">PyObject</span> <span class="o">*</span>
<span class="n">_pickle_Pickler_dump_impl</span><span class="p">(</span><span class="n">PicklerObject</span> <span class="o">*</span><span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span> <span class="o">*</span><span class="n">obj</span><span class="p">)</span>
<span class="cm">/*[clinic end generated code: checksum=3bd30745bf206a48f8b576a1da3d90f55a0a4187]*/</span>
<span class="p">{</span>
<span class="cm">/* Check whether the Pickler was initialized correctly (issue3664).</span>
<span class="cm"> Developers often forget to call __init__() in their subclasses, which</span>
<span class="cm"> would trigger a segfault without this check. */</span>
<span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="o">-></span><span class="n">write</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">PyErr_Format</span><span class="p">(</span><span class="n">PicklingError</span><span class="p">,</span>
<span class="s">"Pickler.__init__() was not called by %s.__init__()"</span><span class="p">,</span>
<span class="n">Py_TYPE</span><span class="p">(</span><span class="n">self</span><span class="p">)</span><span class="o">-></span><span class="n">tp_name</span><span class="p">);</span>
<span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">_Pickler_ClearBuffer</span><span class="p">(</span><span class="n">self</span><span class="p">)</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span>
<span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="p">...</span>
</pre></div>
</div>
</li>
<li><p class="first">Remember the macro with the <a class="reference internal" href="../c-api/structures.html#c.PyMethodDef" title="PyMethodDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMethodDef</span></code></a> structure for this
function? Find the existing <a class="reference internal" href="../c-api/structures.html#c.PyMethodDef" title="PyMethodDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMethodDef</span></code></a> structure for this
function and replace it with a reference to the macro. (If the builtin
is at module scope, this will probably be very near the end of the file;
if the builtin is a class method, this will probably be below but relatively
near to the implementation.)</p>
<p>Note that the body of the macro contains a trailing comma. So when you
replace the existing static <a class="reference internal" href="../c-api/structures.html#c.PyMethodDef" title="PyMethodDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMethodDef</span></code></a> structure with the macro,
<em>don’t</em> add a comma to the end.</p>
<p>Sample:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span> <span class="k">struct</span> <span class="n">PyMethodDef</span> <span class="n">Pickler_methods</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span>
<span class="n">__PICKLE_PICKLER_DUMP_METHODDEF</span>
<span class="n">__PICKLE_PICKLER_CLEAR_MEMO_METHODDEF</span>
<span class="p">{</span><span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">}</span> <span class="cm">/* sentinel */</span>
<span class="p">};</span>
</pre></div>
</div>
</li>
<li><p class="first">Compile, then run the relevant portions of the regression-test suite.
This change should not introduce any new compile-time warnings or errors,
and there should be no externally-visible change to Python’s behavior.</p>
<p>Well, except for one difference: <code class="docutils literal notranslate"><span class="pre">inspect.signature()</span></code> run on your function
should now provide a valid signature!</p>
<p>Congratulations, you’ve ported your first function to work with Argument Clinic!</p>
</li>
</ol>
</div>
<div class="section" id="advanced-topics">
<h2>Advanced Topics<a class="headerlink" href="#advanced-topics" title="本標題的永久連結">¶</a></h2>
<p>Now that you’ve had some experience working with Argument Clinic, it’s time
for some advanced topics.</p>
<div class="section" id="symbolic-default-values">
<h3>Symbolic default values<a class="headerlink" href="#symbolic-default-values" title="本標題的永久連結">¶</a></h3>
<p>The default value you provide for a parameter can’t be any arbitrary
expression. Currently the following are explicitly supported:</p>
<ul class="simple">
<li>Numeric constants (integer and float)</li>
<li>String constants</li>
<li><code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="docutils literal notranslate"><span class="pre">False</span></code>, and <code class="docutils literal notranslate"><span class="pre">None</span></code></li>
<li>Simple symbolic constants like <code class="docutils literal notranslate"><span class="pre">sys.maxsize</span></code>, which must
start with the name of the module</li>
</ul>
<p>In case you’re curious, this is implemented in <code class="docutils literal notranslate"><span class="pre">from_builtin()</span></code>
in <code class="docutils literal notranslate"><span class="pre">Lib/inspect.py</span></code>.</p>
<p>(In the future, this may need to get even more elaborate,
to allow full expressions like <code class="docutils literal notranslate"><span class="pre">CONSTANT</span> <span class="pre">-</span> <span class="pre">1</span></code>.)</p>
</div>
<div class="section" id="renaming-the-c-functions-and-variables-generated-by-argument-clinic">
<h3>Renaming the C functions and variables generated by Argument Clinic<a class="headerlink" href="#renaming-the-c-functions-and-variables-generated-by-argument-clinic" title="本標題的永久連結">¶</a></h3>
<p>Argument Clinic automatically names the functions it generates for you.
Occasionally this may cause a problem, if the generated name collides with
the name of an existing C function. There’s an easy solution: override the names
used for the C functions. Just add the keyword <code class="docutils literal notranslate"><span class="pre">"as"</span></code>
to your function declaration line, followed by the function name you wish to use.
Argument Clinic will use that function name for the base (generated) function,
then add <code class="docutils literal notranslate"><span class="pre">"_impl"</span></code> to the end and use that for the name of the impl function.</p>
<p>For example, if we wanted to rename the C function names generated for
<code class="docutils literal notranslate"><span class="pre">pickle.Pickler.dump</span></code>, it’d look like this:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
<span class="cm">pickle.Pickler.dump as pickler_dumper</span>
<span class="cm">...</span>
</pre></div>
</div>
<p>The base function would now be named <code class="docutils literal notranslate"><span class="pre">pickler_dumper()</span></code>,
and the impl function would now be named <code class="docutils literal notranslate"><span class="pre">pickler_dumper_impl()</span></code>.</p>
<p>Similarly, you may have a problem where you want to give a parameter
a specific Python name, but that name may be inconvenient in C. Argument
Clinic allows you to give a parameter different names in Python and in C,
using the same <code class="docutils literal notranslate"><span class="pre">"as"</span></code> syntax:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
<span class="cm">pickle.Pickler.dump</span>
<span class="cm"> obj: object</span>
<span class="cm"> file as file_obj: object</span>
<span class="cm"> protocol: object = NULL</span>
<span class="cm"> *</span>
<span class="cm"> fix_imports: bool = True</span>
</pre></div>
</div>
<p>Here, the name used in Python (in the signature and the <code class="docutils literal notranslate"><span class="pre">keywords</span></code>
array) would be <code class="docutils literal notranslate"><span class="pre">file</span></code>, but the C variable would be named <code class="docutils literal notranslate"><span class="pre">file_obj</span></code>.</p>
<p>You can use this to rename the <code class="docutils literal notranslate"><span class="pre">self</span></code> parameter too!</p>
</div>
<div class="section" id="converting-functions-using-pyarg-unpacktuple">
<h3>Converting functions using PyArg_UnpackTuple<a class="headerlink" href="#converting-functions-using-pyarg-unpacktuple" title="本標題的永久連結">¶</a></h3>
<p>To convert a function parsing its arguments with <a class="reference internal" href="../c-api/arg.html#c.PyArg_UnpackTuple" title="PyArg_UnpackTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_UnpackTuple()</span></code></a>,
simply write out all the arguments, specifying each as an <code class="docutils literal notranslate"><span class="pre">object</span></code>. You
may specify the <code class="docutils literal notranslate"><span class="pre">type</span></code> argument to cast the type as appropriate. All
arguments should be marked positional-only (add a <code class="docutils literal notranslate"><span class="pre">/</span></code> on a line by itself
after the last argument).</p>
<p>Currently the generated code will use <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a>, but this
will change soon.</p>
</div>
<div class="section" id="optional-groups">
<h3>Optional Groups<a class="headerlink" href="#optional-groups" title="本標題的永久連結">¶</a></h3>
<p>Some legacy functions have a tricky approach to parsing their arguments:
they count the number of positional arguments, then use a <code class="docutils literal notranslate"><span class="pre">switch</span></code> statement
to call one of several different <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a> calls depending on
how many positional arguments there are. (These functions cannot accept
keyword-only arguments.) This approach was used to simulate optional
arguments back before <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTupleAndKeywords" title="PyArg_ParseTupleAndKeywords"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code></a> was created.</p>
<p>While functions using this approach can often be converted to
use <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTupleAndKeywords" title="PyArg_ParseTupleAndKeywords"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code></a>, optional arguments, and default values,
it’s not always possible. Some of these legacy functions have
behaviors <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTupleAndKeywords" title="PyArg_ParseTupleAndKeywords"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code></a> doesn’t directly support.
The most obvious example is the builtin function <code class="docutils literal notranslate"><span class="pre">range()</span></code>, which has
an optional argument on the <em>left</em> side of its required argument!
Another example is <code class="docutils literal notranslate"><span class="pre">curses.window.addch()</span></code>, which has a group of two
arguments that must always be specified together. (The arguments are
called <code class="docutils literal notranslate"><span class="pre">x</span></code> and <code class="docutils literal notranslate"><span class="pre">y</span></code>; if you call the function passing in <code class="docutils literal notranslate"><span class="pre">x</span></code>,
you must also pass in <code class="docutils literal notranslate"><span class="pre">y</span></code>—and if you don’t pass in <code class="docutils literal notranslate"><span class="pre">x</span></code> you may not
pass in <code class="docutils literal notranslate"><span class="pre">y</span></code> either.)</p>
<p>In any case, the goal of Argument Clinic is to support argument parsing
for all existing CPython builtins without changing their semantics.
Therefore Argument Clinic supports
this alternate approach to parsing, using what are called <em>optional groups</em>.
Optional groups are groups of arguments that must all be passed in together.
They can be to the left or the right of the required arguments. They
can <em>only</em> be used with positional-only parameters.</p>
<div class="admonition note">
<p class="first admonition-title">備註</p>
<p class="last">Optional groups are <em>only</em> intended for use when converting
functions that make multiple calls to <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a>!
Functions that use <em>any</em> other approach for parsing arguments
should <em>almost never</em> be converted to Argument Clinic using
optional groups. Functions using optional groups currently
cannot have accurate signatures in Python, because Python just
doesn’t understand the concept. Please avoid using optional
groups wherever possible.</p>
</div>
<p>To specify an optional group, add a <code class="docutils literal notranslate"><span class="pre">[</span></code> on a line by itself before
the parameters you wish to group together, and a <code class="docutils literal notranslate"><span class="pre">]</span></code> on a line by itself
after these parameters. As an example, here’s how <code class="docutils literal notranslate"><span class="pre">curses.window.addch</span></code>
uses optional groups to make the first two parameters and the last
parameter optional:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
<span class="cm">curses.window.addch</span>
<span class="cm"> [</span>
<span class="cm"> x: int</span>
<span class="cm"> X-coordinate.</span>
<span class="cm"> y: int</span>
<span class="cm"> Y-coordinate.</span>
<span class="cm"> ]</span>
<span class="cm"> ch: object</span>
<span class="cm"> Character to add.</span>
<span class="cm"> [</span>
<span class="cm"> attr: long</span>
<span class="cm"> Attributes for the character.</span>
<span class="cm"> ]</span>
<span class="cm"> /</span>
<span class="cm">...</span>
</pre></div>
</div>
<p>註解:</p>
<ul class="simple">
<li>For every optional group, one additional parameter will be passed into the
impl function representing the group. The parameter will be an int named
<code class="docutils literal notranslate"><span class="pre">group_{direction}_{number}</span></code>,
where <code class="docutils literal notranslate"><span class="pre">{direction}</span></code> is either <code class="docutils literal notranslate"><span class="pre">right</span></code> or <code class="docutils literal notranslate"><span class="pre">left</span></code> depending on whether the group
is before or after the required parameters, and <code class="docutils literal notranslate"><span class="pre">{number}</span></code> is a monotonically
increasing number (starting at 1) indicating how far away the group is from
the required parameters. When the impl is called, this parameter will be set
to zero if this group was unused, and set to non-zero if this group was used.
(By used or unused, I mean whether or not the parameters received arguments
in this invocation.)</li>
<li>If there are no required arguments, the optional groups will behave
as if they’re to the right of the required arguments.</li>
<li>In the case of ambiguity, the argument parsing code
favors parameters on the left (before the required parameters).</li>
<li>Optional groups can only contain positional-only parameters.</li>
<li>Optional groups are <em>only</em> intended for legacy code. Please do not
use optional groups for new code.</li>
</ul>
</div>
<div class="section" id="using-real-argument-clinic-converters-instead-of-legacy-converters">
<h3>Using real Argument Clinic converters, instead of 「legacy converters」<a class="headerlink" href="#using-real-argument-clinic-converters-instead-of-legacy-converters" title="本標題的永久連結">¶</a></h3>
<p>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 acceptable when porting code for
Python 3.4.</p>
<p>However, in the long term we probably want all our blocks to
use Argument Clinic’s real syntax for converters. Why? A couple
reasons:</p>
<ul class="simple">
<li>The proper converters are far easier to read and clearer in their intent.</li>
<li>There are some format units that are unsupported as 「legacy converters」,
because they require arguments, and the legacy converter syntax doesn’t
support specifying arguments.</li>
<li>In the future we may have a new argument parsing library that isn’t
restricted to what <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a> supports; this flexibility
won’t be available to parameters using legacy converters.</li>
</ul>
<p>Therefore, if you don’t mind a little extra effort, please use the normal
converters instead of legacy converters.</p>
<p>In a nutshell, the syntax for Argument Clinic (non-legacy) converters
looks like a Python function call. However, if there are no explicit
arguments to the function (all functions take their default values),
you may omit the parentheses. Thus <code class="docutils literal notranslate"><span class="pre">bool</span></code> and <code class="docutils literal notranslate"><span class="pre">bool()</span></code> are exactly
the same converters.</p>
<p>All arguments to Argument Clinic converters are keyword-only.
All Argument Clinic converters accept the following arguments:</p>
<blockquote>
<div><dl class="docutils">
<dt><code class="docutils literal notranslate"><span class="pre">c_default</span></code></dt>
<dd>The default value for this parameter when defined in C.
Specifically, this will be the initializer for the variable declared
in the 「parse function」. See <a class="reference internal" href="#default-values"><span class="std std-ref">the section on default values</span></a>
for how to use this.
Specified as a string.</dd>
<dt><code class="docutils literal notranslate"><span class="pre">annotation</span></code></dt>
<dd>The annotation value for this parameter. Not currently supported,
because PEP 8 mandates that the Python library may not use
annotations.</dd>
</dl>
</div></blockquote>
<p>In addition, some converters accept additional arguments. Here is a list
of these arguments, along with their meanings:</p>
<blockquote>
<div><dl class="docutils">
<dt><code class="docutils literal notranslate"><span class="pre">accept</span></code></dt>
<dd><p class="first">A set of Python types (and possibly pseudo-types);
this restricts the allowable Python argument to values of these types.
(This is not a general-purpose facility; as a rule it only supports
specific lists of types as shown in the legacy converter table.)</p>
<p class="last">To accept <code class="docutils literal notranslate"><span class="pre">None</span></code>, add <code class="docutils literal notranslate"><span class="pre">NoneType</span></code> to this set.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">bitwise</span></code></dt>
<dd>Only supported for unsigned integers. The native integer value of this
Python argument will be written to the parameter without any range checking,
even for negative values.</dd>
<dt><code class="docutils literal notranslate"><span class="pre">converter</span></code></dt>
<dd>Only supported by the <code class="docutils literal notranslate"><span class="pre">object</span></code> converter. Specifies the name of a
<a class="reference internal" href="../c-api/arg.html#o-ampersand"><span class="std std-ref">C 「converter function」</span></a>
to use to convert this object to a native type.</dd>
<dt><code class="docutils literal notranslate"><span class="pre">encoding</span></code></dt>
<dd>Only supported for strings. Specifies the encoding to use when converting
this string from a Python str (Unicode) value into a C <code class="docutils literal notranslate"><span class="pre">char</span> <span class="pre">*</span></code> value.</dd>
<dt><code class="docutils literal notranslate"><span class="pre">subclass_of</span></code></dt>
<dd>Only supported for the <code class="docutils literal notranslate"><span class="pre">object</span></code> converter. Requires that the Python
value be a subclass of a Python type, as expressed in C.</dd>
<dt><code class="docutils literal notranslate"><span class="pre">type</span></code></dt>
<dd>Only supported for the <code class="docutils literal notranslate"><span class="pre">object</span></code> and <code class="docutils literal notranslate"><span class="pre">self</span></code> converters. Specifies
the C type that will be used to declare the variable. Default value is
<code class="docutils literal notranslate"><span class="pre">"PyObject</span> <span class="pre">*"</span></code>.</dd>
<dt><code class="docutils literal notranslate"><span class="pre">zeroes</span></code></dt>
<dd>Only supported for strings. If true, embedded NUL bytes (<code class="docutils literal notranslate"><span class="pre">'\\0'</span></code>) are
permitted inside the value. The length of the string will be passed in
to the impl function, just after the string parameter, as a parameter named
<code class="docutils literal notranslate"><span class="pre"><parameter_name>_length</span></code>.</dd>
</dl>
</div></blockquote>
<p>Please note, not every possible combination of arguments will work.
Usually these arguments are implemented by specific <code class="docutils literal notranslate"><span class="pre">PyArg_ParseTuple</span></code>
<em>format units</em>, with specific behavior. For example, currently you cannot
call <code class="docutils literal notranslate"><span class="pre">unsigned_short</span></code> without also specifying <code class="docutils literal notranslate"><span class="pre">bitwise=True</span></code>.
Although it’s perfectly reasonable to think this would work, these semantics don’t
map to any existing format unit. So Argument Clinic doesn’t support it. (Or, at
least, not yet.)</p>
<p>Below is a table showing the mapping of legacy converters into real
Argument Clinic converters. On the left is the legacy converter,
on the right is the text you’d replace it with.</p>
<table border="1" class="docutils">
<colgroup>
<col width="10%" />
<col width="90%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'B'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">unsigned_char(bitwise=True)</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'b'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">unsigned_char</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'c'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">char</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'C'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">int(accept={str})</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'d'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">double</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'D'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">Py_complex</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'es'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">str(encoding='name_of_encoding')</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'es#'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">str(encoding='name_of_encoding',</span> <span class="pre">zeroes=True)</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'et'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">str(encoding='name_of_encoding',</span> <span class="pre">accept={bytes,</span> <span class="pre">bytearray,</span> <span class="pre">str})</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'et#'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">str(encoding='name_of_encoding',</span> <span class="pre">accept={bytes,</span> <span class="pre">bytearray,</span> <span class="pre">str},</span> <span class="pre">zeroes=True)</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'f'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">float</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'h'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">short</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'H'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">unsigned_short(bitwise=True)</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'i'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">int</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'I'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">unsigned_int(bitwise=True)</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'k'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">unsigned_long(bitwise=True)</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'K'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">unsigned_long_long(bitwise=True)</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'l'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">long</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'L'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">long</span> <span class="pre">long</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'n'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">Py_ssize_t</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'O'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">object</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'O!'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">object(subclass_of='&PySomething_Type')</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'O&'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">object(converter='name_of_c_function')</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'p'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">bool</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'S'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">PyBytesObject</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'s'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">str</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'s#'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">str(zeroes=True)</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'s*'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">Py_buffer(accept={buffer,</span> <span class="pre">str})</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'U'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">unicode</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'u'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">Py_UNICODE</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'u#'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">Py_UNICODE(zeroes=True)</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'w*'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">Py_buffer(accept={rwbuffer})</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'Y'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">PyByteArrayObject</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'y'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">str(accept={bytes})</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'y#'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">str(accept={robuffer},</span> <span class="pre">zeroes=True)</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'y*'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">Py_buffer</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'Z'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">Py_UNICODE(accept={str,</span> <span class="pre">NoneType})</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'Z#'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">Py_UNICODE(accept={str,</span> <span class="pre">NoneType},</span> <span class="pre">zeroes=True)</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'z'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">str(accept={str,</span> <span class="pre">NoneType})</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'z#'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">str(accept={str,</span> <span class="pre">NoneType},</span> <span class="pre">zeroes=True)</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'z*'</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">Py_buffer(accept={buffer,</span> <span class="pre">str,</span> <span class="pre">NoneType})</span></code></td>
</tr>
</tbody>
</table>
<p>As an example, here’s our sample <code class="docutils literal notranslate"><span class="pre">pickle.Pickler.dump</span></code> using the proper
converter:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
<span class="cm">pickle.Pickler.dump</span>
<span class="cm"> obj: object</span>
<span class="cm"> The object to be pickled.</span>
<span class="cm"> /</span>
<span class="cm">Write a pickled representation of obj to the open file.</span>
<span class="cm">[clinic start generated code]*/</span>
</pre></div>
</div>
<p>Argument Clinic will show you all the converters it has
available. For each converter it’ll show you all the parameters
it accepts, along with the default value for each parameter.
Just run <code class="docutils literal notranslate"><span class="pre">Tools/clinic/clinic.py</span> <span class="pre">--converters</span></code> to see the full list.</p>
</div>
<div class="section" id="py-buffer">
<h3>Py_buffer<a class="headerlink" href="#py-buffer" title="本標題的永久連結">¶</a></h3>
<p>When using the <code class="docutils literal notranslate"><span class="pre">Py_buffer</span></code> converter
(or the <code class="docutils literal notranslate"><span class="pre">'s*'</span></code>, <code class="docutils literal notranslate"><span class="pre">'w*'</span></code>, <code class="docutils literal notranslate"><span class="pre">'*y'</span></code>, or <code class="docutils literal notranslate"><span class="pre">'z*'</span></code> legacy converters),
you <em>must</em> not call <a class="reference internal" href="../c-api/buffer.html#c.PyBuffer_Release" title="PyBuffer_Release"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyBuffer_Release()</span></code></a> on the provided buffer.
Argument Clinic generates code that does it for you (in the parsing function).</p>
</div>
<div class="section" id="advanced-converters">
<h3>Advanced converters<a class="headerlink" href="#advanced-converters" title="本標題的永久連結">¶</a></h3>
<p>Remember those format units you skipped for your first
time because they were advanced? Here’s how to handle those too.</p>
<p>The trick is, all those format units take arguments—either
conversion functions, or types, or strings specifying an encoding.
(But 「legacy converters」 don’t support arguments. That’s why we
skipped them for your first function.) The argument you specified
to the format unit is now an argument to the converter; this
argument is either <code class="docutils literal notranslate"><span class="pre">converter</span></code> (for <code class="docutils literal notranslate"><span class="pre">O&</span></code>), <code class="docutils literal notranslate"><span class="pre">subclass_of</span></code> (for <code class="docutils literal notranslate"><span class="pre">O!</span></code>),
or <code class="docutils literal notranslate"><span class="pre">encoding</span></code> (for all the format units that start with <code class="docutils literal notranslate"><span class="pre">e</span></code>).</p>
<p>When using <code class="docutils literal notranslate"><span class="pre">subclass_of</span></code>, you may also want to use the other
custom argument for <code class="docutils literal notranslate"><span class="pre">object()</span></code>: <code class="docutils literal notranslate"><span class="pre">type</span></code>, which lets you set the type
actually used for the parameter. For example, if you want to ensure
that the object is a subclass of <code class="docutils literal notranslate"><span class="pre">PyUnicode_Type</span></code>, you probably want
to use the converter <code class="docutils literal notranslate"><span class="pre">object(type='PyUnicodeObject</span> <span class="pre">*',</span> <span class="pre">subclass_of='&PyUnicode_Type')</span></code>.</p>
<p>One possible problem with using Argument Clinic: it takes away some possible
flexibility for the format units starting with <code class="docutils literal notranslate"><span class="pre">e</span></code>. When writing a
<code class="docutils literal notranslate"><span class="pre">PyArg_Parse</span></code> call by hand, you could theoretically decide at runtime what
encoding string to pass in to <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a>. But now this string must
be hard-coded at Argument-Clinic-preprocessing-time. This limitation is deliberate;
it made supporting this format unit much easier, and may allow for future optimizations.
This restriction doesn’t seem unreasonable; CPython itself always passes in static