Larry Hastings
Source code:Tools/clinic/clinic.py.
Argument Clinic is a preprocessor for CPython C files.It was introduced in Python 3.4 withPEP 436,in order to provide introspection signatures,and to generate performant and tailor-made boilerplate codefor argument parsing in CPython builtins, module level functions, and class methods.This document is divided in four major sections:
Background talks about the basic concepts and goals of Argument Clinic.
Reference describes the command-line interface and Argument Clinic terminology.
Tutorial guides you through all the steps required to adapt an existing C function to Argument Clinic.
How-to guides details how to handle specific tasks.
Note
Argument Clinic is considered internal-onlyfor CPython. Its use is not supported for files outsideCPython, and no guarantees are made regarding backwardscompatibility for future versions. In other words: if youmaintain an external C extension for CPython, you’re welcometo experiment with Argument Clinic in your own code. But theversion of Argument Clinic that ships with the next versionof CPythoncould be totally incompatible and break all your code.
When Argument Clinic is run on a file, either via theCommand-line interfaceor viamakeclinic, it will scan over the input files looking forstart lines:
/*[clinic input]
When it finds one, it reads everything up to theend line:
[clinic start generated code]*/
Everything in between these two lines is Argument Clinicinput.When Argument Clinic parses input, it generatesoutput.The output is rewritten into the C file immediately after the input,followed by achecksum line.All of these lines, including thestart line andchecksum line,are collectively called an Argument Clinicblock:
/*[clinic input]... clinic input goes here ...[clinic start generated code]*/... clinic output goes here .../*[clinic end generated code: ...]*/
If you run Argument Clinic on the same file a second time, Argument Clinicwill discard the oldoutput and write out the new output with a freshchecksum line.If theinput hasn’t changed, the output won’t change either.
Note
You should never modify the output of an Argument Clinic block,as any change will be lost in future Argument Clinic runs;Argument Clinic will detect an output checksum mismatch and regenerate thecorrect output.If you are not happy with the generated output,you should instead change the input until it produces the output you want.
The line/*[clinicinput].This line marks the beginning of Argument Clinic input.Note that thestart line opens a C block comment.
The line[clinicstartgeneratedcode]*/.Theend line marks theend of Argument Clinicinput,but at the same time marks thestart of Argument Clinicoutput,thus the text“clinic start start generated code”Note that theend line closes the C block comment openedby thestart line.
A line that looks like/*[clinicendgeneratedcode:...]*/.The three dots will be replaced by achecksum generated from theinput, and achecksum generated from theoutput.The checksum line marks the end of Argument Clinic generated code,and is used by Argument Clinic to determine if it needs to regenerateoutput.
The text between thestart line and theend line.Note that the start and end lines open and close a C block comment;theinput is thus a part of that same C block comment.
The text between theend line and thechecksum line.
All text from thestart line to thechecksum line inclusively.
The Argument ClinicCLI is typically used toprocess a single source file, like this:
$python3./Tools/clinic/clinic.pyfoo.cThe CLI supports the following options:
Print CLI usage.
Force output regeneration.
Redirect file output to OUTPUT
Enable verbose mode.
Print a list of all supported converters and return converters.
Use theLimited API to parse arguments in the generated C code.SeeHow to use the Limited C API.
The list of files to process.
The base class for all converters.SeeHow to write a custom converter for how to subclass this class.
The C type to use for this variable.type should be a Python string specifying the type,for example,'int'.If this is a pointer type, the type string should end with'*'.
The Python default value for this parameter, as a Python value.Or the magic valueunspecified if there is no default.
default as it should appear in Python code,as a string.OrNone if there is no default.
default as it should appear in C code,as a string.OrNone if there is no default.
The default value used to initialize the C variable whenthere is no default, but not specifying a default mayresult in an “uninitialized variable” warning. This caneasily happen when using option groups—althoughproperly written code will never actually use this value,the variable does get passed in to the impl, and theC compiler will complain about the “use” of theuninitialized value. This value should always be anon-empty string.
The name of the C converter function, as a string.
A boolean value. If true,Argument Clinic will add a& in front of the name ofthe variable when passing it into the impl function.
A boolean value. If true,Argument Clinic will add a& in front of the name ofthe variable when passing it intoPyArg_ParseTuple().