Skip to content

[3.12] Docs: Argument Clinic: Move the CConverter class to the reference (GH-107671) #107701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2023
Merged
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
133 changes: 69 additions & 64 deletions Doc/howto/clinic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,71 @@ The CLI supports the following options:
The list of files to process.


.. _clinic-classes:

Classes for extending Argument Clinic
-------------------------------------

.. module:: clinic

.. class:: CConverter

The base class for all converters.
See :ref:`clinic-howto-custom-converter` for how to subclass this class.

.. attribute:: type

The C type to use for this variable.
:attr:`!type` should be a Python string specifying the type,
e.g. ``'int'``.
If this is a pointer type, the type string should end with ``' *'``.

.. attribute:: default

The Python default value for this parameter, as a Python value.
Or the magic value ``unspecified`` if there is no default.

.. attribute:: py_default

:attr:`!default` as it should appear in Python code,
as a string.
Or ``None`` if there is no default.

.. attribute:: c_default

:attr:`!default` as it should appear in C code,
as a string.
Or ``None`` if there is no default.

.. attribute:: c_ignored_default

The default value used to initialize the C variable when
there is no default, but not specifying a default may
result in an "uninitialized variable" warning. This can
easily happen when using option groups—although
properly written code will never actually use this value,
the variable does get passed in to the impl, and the
C compiler will complain about the "use" of the
uninitialized value. This value should always be a
non-empty string.

.. attribute:: converter

The name of the C converter function, as a string.

.. attribute:: impl_by_reference

A boolean value. If true,
Argument Clinic will add a ``&`` in front of the name of
the variable when passing it into the impl function.

.. attribute:: parse_by_reference

A boolean value. If true,
Argument Clinic will add a ``&`` in front of the name of
the variable when passing it into :c:func:`PyArg_ParseTuple`.


.. _clinic-tutorial:

Tutorial
Expand Down Expand Up @@ -1348,7 +1413,7 @@ See also :pep:`573`.
How to write a custom converter
-------------------------------

A converter is a Python class that inherits from :py:class:`!CConverter`.
A converter is a Python class that inherits from :py:class:`CConverter`.
The main purpose of a custom converter, is for parameters parsed with
the ``O&`` format unit --- parsing such a parameter means calling
a :c:func:`PyArg_ParseTuple` "converter function".
Expand All @@ -1360,73 +1425,13 @@ your converter class with the ``_converter`` suffix stripped off.

Instead of subclassing :py:meth:`!CConverter.__init__`,
write a :py:meth:`!converter_init` method.
Apart for the *self* parameter, all additional :py:meth:`!converter_init`
parameters **must** be keyword-only.
:py:meth:`!converter_init` always accepts a *self* parameter.
After *self*, all additional parameters **must** be keyword-only.
Any arguments passed to the converter in Argument Clinic
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
See :py:class:`CConverter` for a list of members you may wish to specify in
your subclass.

.. module:: clinic

.. class:: CConverter

The base class for all converters.
See :ref:`clinic-howto-custom-converter` for how to subclass this class.

.. attribute:: type

The C type to use for this variable.
:attr:`!type` should be a Python string specifying the type,
e.g. ``'int'``.
If this is a pointer type, the type string should end with ``' *'``.

.. attribute:: default

The Python default value for this parameter, as a Python value.
Or the magic value ``unspecified`` if there is no default.

.. attribute:: py_default

:attr:`!default` as it should appear in Python code,
as a string.
Or ``None`` if there is no default.

.. attribute:: c_default

:attr:`!default` as it should appear in C code,
as a string.
Or ``None`` if there is no default.

.. attribute:: c_ignored_default

The default value used to initialize the C variable when
there is no default, but not specifying a default may
result in an "uninitialized variable" warning. This can
easily happen when using option groups—although
properly written code will never actually use this value,
the variable does get passed in to the impl, and the
C compiler will complain about the "use" of the
uninitialized value. This value should always be a
non-empty string.

.. attribute:: converter

The name of the C converter function, as a string.

.. attribute:: impl_by_reference

A boolean value. If true,
Argument Clinic will add a ``&`` in front of the name of
the variable when passing it into the impl function.

.. attribute:: parse_by_reference

A boolean value. If true,
Argument Clinic will add a ``&`` in front of the name of
the variable when passing it into :c:func:`PyArg_ParseTuple`.


Here's the simplest example of a custom converter, from :source:`Modules/zlibmodule.c`::

/*[python input]
Expand Down