Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 61 additions & 28 deletions development-tools/clinic/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -272,43 +272,76 @@ typically right after the clinic module block::

#include "clinic/_pickle.c.h"

Double-check that the argument-parsing code Argument Clinic generated
looks basically the same as the existing code.

First, ensure both places use the same argument-parsing function.
The existing code must call either
:c:func:`PyArg_ParseTuple` or :c:func:`PyArg_ParseTupleAndKeywords`;
ensure that the code generated by Argument Clinic calls the
*exact* same function.

Second, the format string passed in to :c:func:`!PyArg_ParseTuple` or
:c:func:`!PyArg_ParseTupleAndKeywords` should be *exactly* the same
as the hand-written one in the existing function,
Double-check that the code Argument Clinic generated behaves like the code
you are replacing.
Argument Clinic does not copy your old argument-parsing code,
so the generated code does not have to look like it.
Check these things in the generated :file:`.c.h` file instead.

First, check the signature.
Argument Clinic writes it as the first line of the generated docstring,
for example::

"dump($self, obj, /)\n"

The parameter names, the ``/`` and ``*`` markers and the default values
should match what the old function accepted.
This is also the text that :py:func:`inspect.signature` reads.

Second, look at the macro that defines the :c:type:`PyMethodDef` entry for
your function::

#define _PICKLE_PICKLER_DUMP_METHODDEF \
{"dump", (PyCFunction)_pickle_Pickler_dump, METH_O, _pickle_Pickler_dump__doc__},

Argument Clinic picks the calling convention from your parameters.
A function with no parameters gets :c:macro:`METH_NOARGS`,
and a function with a single required positional-only parameter
gets :c:macro:`METH_O`.
Most other functions get :c:macro:`METH_FASTCALL`,
or ``METH_FASTCALL|METH_KEYWORDS`` if they accept keyword arguments.
The flags may be different from the old :c:type:`!PyMethodDef` entry,
because the old code often used :c:macro:`METH_VARARGS`.
This is expected.
What matters is that the function accepts the same arguments as before.

Third, check how the arguments are parsed.
For most converters, Argument Clinic generates the parsing code itself,
using helpers such as :c:func:`!_PyArg_CheckPositional`
and :c:func:`!_PyArg_UnpackKeywords`.
There is no :c:func:`!PyArg_Parse*` call to compare in that case.
Instead, compare the old code with the generated code and
make sure both of them:

* accept the same number of positional arguments,
including the optional ones and their default values;
* accept the same keyword arguments, and only those;
* convert each argument to the same C type.

Some converters have no generated parsing code.
Examples are the ``s#``, ``es`` and ``et`` legacy converters.
If your function uses one of them,
Argument Clinic falls back to a single call to a function from the
:c:func:`!PyArg_Parse*` family.
Which one depends on the calling convention it chose,
for example :c:func:`PyArg_ParseTuple`, :c:func:`!PyArg_ParseArray`
or :c:func:`!_PyArg_ParseStackAndKeywords`.
It may not be the function the old code called,
but the format string should be *exactly* the same as the hand-written one
up to the colon or semi-colon.

Argument Clinic always generates its format strings
with a ``:`` followed by the name of the function.
If the existing code's format string ends with ``;``,
to provide usage help, this change is harmless --- don't worry about it.

Third, for parameters whose format units require two arguments,
For parameters whose format units require two arguments,
like a length variable, an encoding string, or a pointer
to a conversion function, ensure that the second argument is
*exactly* the same between the two invocations.

Fourth, inside the output portion of the block,
you'll find a preprocessor macro defining the appropriate static
:c:type:`PyMethodDef` structure for this builtin::

#define __PICKLE_PICKLER_DUMP_METHODDEF \
{"dump", (PyCFunction)__pickle_Pickler_dump, METH_O, __pickle_Pickler_dump__doc__},

This static structure should be *exactly* the same as the existing static
:c:type:`!PyMethodDef` structure for this builtin.
*exactly* the same between the two calls.

If any of these items differ in *any way*,
If anything differs in a way that changes behavior,
adjust your Argument Clinic function specification and rerun
``Tools/clinic/clinic.py`` until they *are* the same.
``Tools/clinic/clinic.py`` until the generated code accepts
the same arguments as the old code.

Notice that the last line of its output is the declaration
of your "impl" function. This is where the builtin's implementation goes.
Expand Down