[3.12] Docs: Argument Clinic: Improve 'How to write a custom converter' (GH-107328) (#107669)

Docs: Argument Clinic: Improve 'How to write a custom converter' (GH-107328)

- Omit unneccesary wording and sentences
- Don't mention implementation details (no digression, explanation)

(cherry picked from commit 4a5b4221e3)

Co-authored-by: Erlend E. Aasland <erlend@python.org>
Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com>
This commit is contained in:
Miss Islington (bot) 2023-08-06 13:09:00 -07:00 committed by GitHub
parent e950d70451
commit e94548479a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1343,35 +1343,37 @@ state. Example from the ``setattro`` slot method in
See also :pep:`573`. See also :pep:`573`.
.. _clinic-howto-custom-converter:
How to write a custom converter How to write a custom converter
------------------------------- -------------------------------
As we hinted at in the previous section... you can write your own converters! A converter is a Python class that inherits from :py:class:`!CConverter`.
A converter is simply a Python class that inherits from :py:class:`!CConverter`. The main purpose of a custom converter, is for parameters parsed with
The main purpose of a custom converter is if you have a parameter using the ``O&`` format unit --- parsing such a parameter means calling
the ``O&`` format unit—parsing this parameter means calling
a :c:func:`PyArg_ParseTuple` "converter function". a :c:func:`PyArg_ParseTuple` "converter function".
Your converter class should be named ``*something*_converter``. Your converter class should be named :samp:`{ConverterName}_converter`.
If the name follows this convention, then your converter class By following this convention, your converter class will be automatically
will be automatically registered with Argument Clinic; its name registered with Argument Clinic, with its *converter name* being the name of
will be the name of your class with the ``_converter`` suffix your converter class with the ``_converter`` suffix stripped off.
stripped off. (This is accomplished with a metaclass.)
You shouldn't subclass :py:meth:`!CConverter.__init__`. Instead, you should Instead of subclassing :py:meth:`!CConverter.__init__`,
write a :py:meth:`!converter_init` function. :py:meth:`!converter_init` write a :py:meth:`!converter_init` method.
always accepts a *self* parameter; after that, all additional Apart for the *self* parameter, all additional :py:meth:`!converter_init`
parameters *must* be keyword-only. Any arguments passed in to parameters **must** be keyword-only.
the converter in Argument Clinic will be passed along to your Any arguments passed to the converter in Argument Clinic
:py:meth:`!converter_init`. will be passed along to your :py:meth:`!converter_init` method.
See :py:class:`!CConverter` for a list of members you may wish to specify in
There are some additional members of :py:class:`!CConverter` you may wish your subclass.
to specify in your subclass. Here's the current list:
.. module:: clinic .. module:: clinic
.. class:: CConverter .. class:: CConverter
The base class for all converters.
See :ref:`clinic-howto-custom-converter` for how to subclass this class.
.. attribute:: type .. attribute:: type
The C type to use for this variable. The C type to use for this variable.
@ -1436,16 +1438,16 @@ Here's the simplest example of a custom converter, from :source:`Modules/zlibmod
[python start generated code]*/ [python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=35521e4e733823c7]*/ /*[python end generated code: output=da39a3ee5e6b4b0d input=35521e4e733823c7]*/
This block adds a converter to Argument Clinic named ``ssize_t``. Parameters This block adds a converter named ``ssize_t`` to Argument Clinic.
declared as ``ssize_t`` will be declared as type :c:type:`Py_ssize_t`, and will Parameters declared as ``ssize_t`` will be declared with type :c:type:`Py_ssize_t`,
be parsed by the ``'O&'`` format unit, which will call the and will be parsed by the ``'O&'`` format unit,
``ssize_t_converter`` converter function. ``ssize_t`` variables which will call the :c:func:`!ssize_t_converter` converter C function.
automatically support default values. ``ssize_t`` variables automatically support default values.
More sophisticated custom converters can insert custom C code to More sophisticated custom converters can insert custom C code to
handle initialization and cleanup. handle initialization and cleanup.
You can see more examples of custom converters in the CPython You can see more examples of custom converters in the CPython
source tree; grep the C files for the string :py:class:`!CConverter`. source tree; grep the C files for the string ``CConverter``.
How to write a custom return converter How to write a custom return converter