mirror of https://github.com/python/cpython
Issue #28755: Merge Arg Clinic howto from 3.6
This commit is contained in:
commit
e5a0946aa5
|
@ -1,3 +1,5 @@
|
||||||
|
.. highlightlang:: c
|
||||||
|
|
||||||
**********************
|
**********************
|
||||||
Argument Clinic How-To
|
Argument Clinic How-To
|
||||||
**********************
|
**********************
|
||||||
|
@ -78,17 +80,23 @@ Basic Concepts And Usage
|
||||||
========================
|
========================
|
||||||
|
|
||||||
Argument Clinic ships with CPython; you'll find it in ``Tools/clinic/clinic.py``.
|
Argument Clinic ships with CPython; you'll find it in ``Tools/clinic/clinic.py``.
|
||||||
If you run that script, specifying a C file as an argument::
|
If you run that script, specifying a C file as an argument:
|
||||||
|
|
||||||
% python3 Tools/clinic/clinic.py foo.c
|
.. code-block:: shell-session
|
||||||
|
|
||||||
|
$ python3 Tools/clinic/clinic.py foo.c
|
||||||
|
|
||||||
Argument Clinic will scan over the file looking for lines that
|
Argument Clinic will scan over the file looking for lines that
|
||||||
look exactly like this::
|
look exactly like this:
|
||||||
|
|
||||||
|
.. code-block:: none
|
||||||
|
|
||||||
/*[clinic input]
|
/*[clinic input]
|
||||||
|
|
||||||
When it finds one, it reads everything up to a line that looks
|
When it finds one, it reads everything up to a line that looks
|
||||||
exactly like this::
|
exactly like this:
|
||||||
|
|
||||||
|
.. code-block:: none
|
||||||
|
|
||||||
[clinic start generated code]*/
|
[clinic start generated code]*/
|
||||||
|
|
||||||
|
@ -99,7 +107,9 @@ lines, are collectively called an Argument Clinic "block".
|
||||||
When Argument Clinic parses one of these blocks, it
|
When Argument Clinic parses one of these blocks, it
|
||||||
generates output. This output is rewritten into the C file
|
generates output. This output is rewritten into the C file
|
||||||
immediately after the block, followed by a comment containing a checksum.
|
immediately after the block, followed by a comment containing a checksum.
|
||||||
The Argument Clinic block now looks like this::
|
The Argument Clinic block now looks like this:
|
||||||
|
|
||||||
|
.. code-block:: none
|
||||||
|
|
||||||
/*[clinic input]
|
/*[clinic input]
|
||||||
... clinic input goes here ...
|
... clinic input goes here ...
|
||||||
|
@ -378,9 +388,7 @@ Let's dive in!
|
||||||
12. Save and close the file, then run ``Tools/clinic/clinic.py`` on
|
12. Save and close the file, then run ``Tools/clinic/clinic.py`` on
|
||||||
it. With luck everything worked---your block now has output, and
|
it. With luck everything worked---your block now has output, and
|
||||||
a ``.c.h`` file has been generated! Reopen the file in your
|
a ``.c.h`` file has been generated! Reopen the file in your
|
||||||
text editor to see:
|
text editor to see::
|
||||||
|
|
||||||
.. code-block:: c
|
|
||||||
|
|
||||||
/*[clinic input]
|
/*[clinic input]
|
||||||
_pickle.Pickler.dump
|
_pickle.Pickler.dump
|
||||||
|
@ -402,9 +410,7 @@ Let's dive in!
|
||||||
|
|
||||||
For readability, most of the glue code has been generated to a ``.c.h``
|
For readability, most of the glue code has been generated to a ``.c.h``
|
||||||
file. You'll need to include that in your original ``.c`` file,
|
file. You'll need to include that in your original ``.c`` file,
|
||||||
typically right after the clinic module block:
|
typically right after the clinic module block::
|
||||||
|
|
||||||
.. code-block:: c
|
|
||||||
|
|
||||||
#include "clinic/_pickle.c.h"
|
#include "clinic/_pickle.c.h"
|
||||||
|
|
||||||
|
@ -1028,7 +1034,9 @@ that value, and an error has been set (``PyErr_Occurred()`` returns a true
|
||||||
value), then the generated code will propagate the error. Otherwise it will
|
value), then the generated code will propagate the error. Otherwise it will
|
||||||
encode the value you return like normal.
|
encode the value you return like normal.
|
||||||
|
|
||||||
Currently Argument Clinic supports only a few return converters::
|
Currently Argument Clinic supports only a few return converters:
|
||||||
|
|
||||||
|
.. code-block:: none
|
||||||
|
|
||||||
bool
|
bool
|
||||||
int
|
int
|
||||||
|
@ -1607,7 +1615,9 @@ code probably looks like this::
|
||||||
#endif /* HAVE_FUNCTIONNAME */
|
#endif /* HAVE_FUNCTIONNAME */
|
||||||
|
|
||||||
And then in the ``PyMethodDef`` structure at the bottom the existing code
|
And then in the ``PyMethodDef`` structure at the bottom the existing code
|
||||||
will have::
|
will have:
|
||||||
|
|
||||||
|
.. code-block:: none
|
||||||
|
|
||||||
#ifdef HAVE_FUNCTIONNAME
|
#ifdef HAVE_FUNCTIONNAME
|
||||||
{'functionname', ... },
|
{'functionname', ... },
|
||||||
|
@ -1657,7 +1667,9 @@ extra code when using the "block" output preset? It can't go in the output bloc
|
||||||
because that could be deactivated by the ``#ifdef``. (That's the whole point!)
|
because that could be deactivated by the ``#ifdef``. (That's the whole point!)
|
||||||
|
|
||||||
In this situation, Argument Clinic writes the extra code to the "buffer" destination.
|
In this situation, Argument Clinic writes the extra code to the "buffer" destination.
|
||||||
This may mean that you get a complaint from Argument Clinic::
|
This may mean that you get a complaint from Argument Clinic:
|
||||||
|
|
||||||
|
.. code-block:: none
|
||||||
|
|
||||||
Warning in file "Modules/posixmodule.c" on line 12357:
|
Warning in file "Modules/posixmodule.c" on line 12357:
|
||||||
Destination buffer 'buffer' not empty at end of file, emptying.
|
Destination buffer 'buffer' not empty at end of file, emptying.
|
||||||
|
@ -1677,7 +1689,9 @@ wouldn't make any sense to the Python interpreter. But using Argument Clinic
|
||||||
to run Python blocks lets you use Python as a Python preprocessor!
|
to run Python blocks lets you use Python as a Python preprocessor!
|
||||||
|
|
||||||
Since Python comments are different from C comments, Argument Clinic
|
Since Python comments are different from C comments, Argument Clinic
|
||||||
blocks embedded in Python files look slightly different. They look like this::
|
blocks embedded in Python files look slightly different. They look like this:
|
||||||
|
|
||||||
|
.. code-block:: python3
|
||||||
|
|
||||||
#/*[python input]
|
#/*[python input]
|
||||||
#print("def foo(): pass")
|
#print("def foo(): pass")
|
||||||
|
|
Loading…
Reference in New Issue