Skip to content

Commit 4a5b422

Browse files
Docs: Argument Clinic: Improve 'How to write a custom converter' (#107328)
- Omit unneccesary wording and sentences - Don't mention implementation details (no digression, explanation) Co-authored-by: Ezio Melotti <[email protected]>
1 parent 9ebc6ec commit 4a5b422

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

Diff for: Doc/howto/clinic.rst

+26-24
Original file line numberDiff line numberDiff line change
@@ -1343,35 +1343,37 @@ state. Example from the ``setattro`` slot method in
13431343
See also :pep:`573`.
13441344

13451345

1346+
.. _clinic-howto-custom-converter:
1347+
13461348
How to write a custom converter
13471349
-------------------------------
13481350

1349-
As we hinted at in the previous section... you can write your own converters!
1350-
A converter is simply a Python class that inherits from :py:class:`!CConverter`.
1351-
The main purpose of a custom converter is if you have a parameter using
1352-
the ``O&`` format unit—parsing this parameter means calling
1351+
A converter is a Python class that inherits from :py:class:`!CConverter`.
1352+
The main purpose of a custom converter, is for parameters parsed with
1353+
the ``O&`` format unit --- parsing such a parameter means calling
13531354
a :c:func:`PyArg_ParseTuple` "converter function".
13541355

1355-
Your converter class should be named ``*something*_converter``.
1356-
If the name follows this convention, then your converter class
1357-
will be automatically registered with Argument Clinic; its name
1358-
will be the name of your class with the ``_converter`` suffix
1359-
stripped off. (This is accomplished with a metaclass.)
1360-
1361-
You shouldn't subclass :py:meth:`!CConverter.__init__`. Instead, you should
1362-
write a :py:meth:`!converter_init` function. :py:meth:`!converter_init`
1363-
always accepts a *self* parameter; after that, all additional
1364-
parameters *must* be keyword-only. Any arguments passed in to
1365-
the converter in Argument Clinic will be passed along to your
1366-
:py:meth:`!converter_init`.
1356+
Your converter class should be named :samp:`{ConverterName}_converter`.
1357+
By following this convention, your converter class will be automatically
1358+
registered with Argument Clinic, with its *converter name* being the name of
1359+
your converter class with the ``_converter`` suffix stripped off.
13671360

1368-
There are some additional members of :py:class:`!CConverter` you may wish
1369-
to specify in your subclass. Here's the current list:
1361+
Instead of subclassing :py:meth:`!CConverter.__init__`,
1362+
write a :py:meth:`!converter_init` method.
1363+
Apart for the *self* parameter, all additional :py:meth:`!converter_init`
1364+
parameters **must** be keyword-only.
1365+
Any arguments passed to the converter in Argument Clinic
1366+
will be passed along to your :py:meth:`!converter_init` method.
1367+
See :py:class:`!CConverter` for a list of members you may wish to specify in
1368+
your subclass.
13701369

13711370
.. module:: clinic
13721371

13731372
.. class:: CConverter
13741373

1374+
The base class for all converters.
1375+
See :ref:`clinic-howto-custom-converter` for how to subclass this class.
1376+
13751377
.. attribute:: type
13761378

13771379
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
14361438
[python start generated code]*/
14371439
/*[python end generated code: output=da39a3ee5e6b4b0d input=35521e4e733823c7]*/
14381440

1439-
This block adds a converter to Argument Clinic named ``ssize_t``. Parameters
1440-
declared as ``ssize_t`` will be declared as type :c:type:`Py_ssize_t`, and will
1441-
be parsed by the ``'O&'`` format unit, which will call the
1442-
``ssize_t_converter`` converter function. ``ssize_t`` variables
1443-
automatically support default values.
1441+
This block adds a converter named ``ssize_t`` to Argument Clinic.
1442+
Parameters declared as ``ssize_t`` will be declared with type :c:type:`Py_ssize_t`,
1443+
and will be parsed by the ``'O&'`` format unit,
1444+
which will call the :c:func:`!ssize_t_converter` converter C function.
1445+
``ssize_t`` variables automatically support default values.
14441446

14451447
More sophisticated custom converters can insert custom C code to
14461448
handle initialization and cleanup.
14471449
You can see more examples of custom converters in the CPython
1448-
source tree; grep the C files for the string :py:class:`!CConverter`.
1450+
source tree; grep the C files for the string ``CConverter``.
14491451

14501452

14511453
How to write a custom return converter

0 commit comments

Comments
 (0)