@@ -2030,52 +2030,76 @@ The generated glue code looks like this:
2030
2030
.. versionadded :: 3.13
2031
2031
2032
2032
2033
- .. _clinic-howto-getter :
2033
+ .. _clinic-howto-pygetsetdef :
2034
2034
2035
- How to generate a getter
2036
- ------------------------
2035
+ How to declare `` PyGetSetDef `` (" getter/setter") functions
2036
+ ----------------------------------------------------------
2037
2037
2038
- "Getters" are C functions that facilitate property-like access for a class.
2039
- See :c:type: ` getter <PyGetSetDef> ` for details .
2040
- You can use the ``@getter `` directive to generate an "impl" function for a
2041
- getter using Argument Clinic.
2038
+ "Getters" and "setters" are C functions defined in a :c:type: ` PyGetSetDef ` struct
2039
+ that facilitate :py:class: ` property `-like access for a class .
2040
+ You can use the ``@getter `` and `` @setter `` directives to generate
2041
+ "impl" functions using Argument Clinic.
2042
2042
2043
- This example -- taken from :cpy-file: `Modules/_io/bufferedio .c ` --
2044
- shows the use of ``@getter `` in combination with
2043
+ This example --- taken from :cpy-file: `Modules/_io/textio .c ` - --
2044
+ shows the use of ``@getter `` and `` @setter `` in combination with
2045
2045
the :ref: `@critical_section <clinic-howto-critical-sections >` directive
2046
2046
(which achieves thread safety without causing deadlocks between threads)::
2047
2047
2048
2048
/*[clinic input]
2049
2049
@critical_section
2050
2050
@getter
2051
- _io._Buffered.closed
2051
+ _io.TextIOWrapper._CHUNK_SIZE
2052
+ [clinic start generated code]*/
2053
+
2054
+ /*[clinic input]
2055
+ @critical_section
2056
+ @setter
2057
+ _io.TextIOWrapper._CHUNK_SIZE
2052
2058
[clinic start generated code]*/
2053
2059
2054
2060
The generated glue code looks like this:
2055
2061
2056
2062
.. code-block :: c
2057
2063
2058
2064
static PyObject *
2059
- _io__Buffered_closed_get(buffered *self, void *context)
2065
+ _io_TextIOWrapper__CHUNK_SIZE_get(textio *self, void *Py_UNUSED( context) )
2060
2066
{
2061
2067
PyObject *return_value = NULL;
2062
2068
2063
2069
Py_BEGIN_CRITICAL_SECTION(self);
2064
- return_value = _io__Buffered_closed_get_impl (self);
2070
+ return_value = _io_TextIOWrapper__CHUNK_SIZE_get_impl (self);
2065
2071
Py_END_CRITICAL_SECTION();
2066
2072
2067
2073
return return_value;
2068
2074
}
2069
2075
2076
+ static int
2077
+ _io_TextIOWrapper__CHUNK_SIZE_set(textio *self, PyObject *value, void *Py_UNUSED(context))
2078
+ {
2079
+ int return_value;
2080
+ Py_BEGIN_CRITICAL_SECTION(self);
2081
+ return_value = _io_TextIOWrapper__CHUNK_SIZE_set_impl(self, value);
2082
+ Py_END_CRITICAL_SECTION();
2083
+ return return_value;
2084
+ }
2085
+
2086
+ .. note ::
2087
+
2088
+ Getters and setters must be declared as separate functions.
2089
+ The *value * parameter for a "setter" is added implicitly by Argument Clinic.
2090
+
2070
2091
And then the implementation will work the same as a Python method which is
2071
- decorated by :py:class: `property `.
2092
+ decorated by :py:class: `property `:
2072
2093
2073
2094
.. code-block :: pycon
2074
2095
2075
- >>> import _io
2076
- >>> a = _io._BufferedIOBase()
2077
- >>> a.closed
2078
- False
2096
+ >>> import sys, _io
2097
+ >>> a = _io.TextIOWrapper(sys.stdout)
2098
+ >>> a._CHUNK_SIZE
2099
+ 8192
2100
+ >>> a._CHUNK_SIZE = 30
2101
+ >>> a._CHUNK_SIZE
2102
+ 30
2079
2103
2080
2104
.. versionadded :: 3.13
2081
2105
0 commit comments