Skip to content

Docs: Argument Clinic: Move the CConverter class to the reference #107671

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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an actual class that I can import and use? If not, should it get a :noindex:?

I was going through the howto and saw an example that points to Modules/zlibmodule.c, but in the source there's no mention of CConverter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You must subclass the CConverter class when you write custom converters. Perhaps we can point to Modules/posixmodule.c instead; there's a lot of custom converters there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answering your first question; you cannot import it, but you can use it. It is "magically" there when you write a [python input] block.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the :noindex:, though.


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