Skip to content

Commit e8a6577

Browse files
corona10hugovkerlend-aaslandezio-melottiAlexWaygood
authored
Add "how to" for the getter Argument Clinic directive. (#1232)
--------- Co-authored-by: Hugo van Kemenade <[email protected]> Co-authored-by: Erlend E. Aasland <[email protected]> Co-authored-by: Ezio Melotti <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
1 parent 01adb83 commit e8a6577

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

development-tools/clinic.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,6 +2001,56 @@ The generated glue code looks like this:
20012001
.. versionadded:: 3.13
20022002

20032003

2004+
.. _clinic-howto-getter:
2005+
2006+
How to generate a getter
2007+
------------------------
2008+
2009+
"Getters" are C functions that facilitate property-like access for a class.
2010+
See :c:type:`getter <PyGetSetDef>` for details.
2011+
You can use the ``@getter`` directive to generate an "impl" function for a
2012+
getter using Argument Clinic.
2013+
2014+
This example -- taken from :cpy-file:`Modules/_io/bufferedio.c` --
2015+
shows the use of ``@getter`` in combination with
2016+
the :ref:`@critical_section <clinic-howto-critical-sections>` directive
2017+
(which achieves thread safety without causing deadlocks between threads)::
2018+
2019+
/*[clinic input]
2020+
@critical_section
2021+
@getter
2022+
_io._Buffered.closed
2023+
[clinic start generated code]*/
2024+
2025+
The generated glue code looks like this:
2026+
2027+
.. code-block:: c
2028+
2029+
static PyObject *
2030+
_io__Buffered_closed_get(buffered *self, void *context)
2031+
{
2032+
PyObject *return_value = NULL;
2033+
2034+
Py_BEGIN_CRITICAL_SECTION(self);
2035+
return_value = _io__Buffered_closed_get_impl(self);
2036+
Py_END_CRITICAL_SECTION();
2037+
2038+
return return_value;
2039+
}
2040+
2041+
And then the implementation will work the same as a Python method which is
2042+
decorated by :py:class:`property`.
2043+
2044+
.. code-block:: pycon
2045+
2046+
>>> import _io
2047+
>>> a = _io._BufferedIOBase()
2048+
>>> a.closed
2049+
False
2050+
2051+
.. versionadded:: 3.13
2052+
2053+
20042054
.. _clinic-howto-deprecate-positional:
20052055
.. _clinic-howto-deprecate-keyword:
20062056

0 commit comments

Comments
 (0)