Skip to content
Merged
48 changes: 48 additions & 0 deletions development-tools/clinic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2001,6 +2001,54 @@ The generated glue code looks like this:
.. versionadded:: 3.13


.. _clinic-howto-getter:

How to generate a getter
------------------------

You can use the ``@getter`` directive to generate an "impl" function
that defines a :c:type:`getter <PyGetSetDef>`.
Comment thread
corona10 marked this conversation as resolved.
Outdated

This example --- taken from :cpy-file:`Modules/_io/bufferedio.c` ---
Comment thread
corona10 marked this conversation as resolved.
Outdated
shows the use of ``@getter`` in combination with
the :ref:`@critical_section <clinic-howto-critical-sections>` directive
(which achieves thread safety without causing deadlocks between threads)::
Comment thread
AlexWaygood marked this conversation as resolved.
Outdated

/*[clinic input]
@critical_section
@getter
_io._Buffered.closed
[clinic start generated code]*/

The generated glue code looks like this:

.. code-block:: c

static PyObject *
_io__Buffered_closed_get(buffered *self, void *context)
{
PyObject *return_value = NULL;

Py_BEGIN_CRITICAL_SECTION(self);
return_value = _io__Buffered_closed_get_impl(self);
Comment thread
corona10 marked this conversation as resolved.
Py_END_CRITICAL_SECTION();

return return_value;
}

And then the implementation will work the same as the Python method which is
Comment thread
corona10 marked this conversation as resolved.
Outdated
decorated by `property <https://docs.python.org/3/library/functions.html#property>`_.
Comment thread
corona10 marked this conversation as resolved.
Outdated

.. code-block:: python
Comment thread
corona10 marked this conversation as resolved.
Outdated

>>> import _io
>>> a = _io._BufferedIOBase()
>>> a.closed
False

.. versionadded:: 3.13


.. _clinic-howto-deprecate-positional:
.. _clinic-howto-deprecate-keyword:

Expand Down