@@ -2001,6 +2001,56 @@ The generated glue code looks like this:
2001
2001
.. versionadded :: 3.13
2002
2002
2003
2003
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
+
2004
2054
.. _clinic-howto-deprecate-positional :
2005
2055
.. _clinic-howto-deprecate-keyword :
2006
2056
0 commit comments