Skip to content

Commit e4b5ec7

Browse files
miss-islingtonerlend-aaslandezio-melotti
authored
[3.11] Docs: Argument Clinic: Improve 'How to write a custom converter' (GH-107328) (#107670)
- Omit unneccesary wording and sentences - Don't mention implementation details (no digression, explanation) (cherry picked from commit 4a5b422) Co-authored-by: Erlend E. Aasland <[email protected]> Co-authored-by: Ezio Melotti <[email protected]>
1 parent b89feac commit e4b5ec7

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

Doc/howto/clinic.rst

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,35 +1338,37 @@ state. Example from the ``setattro`` slot method in
13381338
See also :pep:`573`.
13391339

13401340

1341+
.. _clinic-howto-custom-converter:
1342+
13411343
How to write a custom converter
13421344
-------------------------------
13431345

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

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

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

13661365
.. module:: clinic
13671366

13681367
.. class:: CConverter
13691368

1369+
The base class for all converters.
1370+
See :ref:`clinic-howto-custom-converter` for how to subclass this class.
1371+
13701372
.. attribute:: type
13711373

13721374
The C type to use for this variable.
@@ -1431,16 +1433,16 @@ Here's the simplest example of a custom converter, from :source:`Modules/zlibmod
14311433
[python start generated code]*/
14321434
/*[python end generated code: output=da39a3ee5e6b4b0d input=35521e4e733823c7]*/
14331435

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

14401442
More sophisticated custom converters can insert custom C code to
14411443
handle initialization and cleanup.
14421444
You can see more examples of custom converters in the CPython
1443-
source tree; grep the C files for the string :py:class:`!CConverter`.
1445+
source tree; grep the C files for the string ``CConverter``.
14441446

14451447

14461448
How to write a custom return converter

0 commit comments

Comments
 (0)