-
-
Notifications
You must be signed in to change notification settings - Fork 882
Add "how to" for the setter Argument Clinic directive #1245
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
Changes from 15 commits
85a1ea8
e278757
3a4661d
017fdc6
17d0f4d
f019535
e53ba5d
a648144
bc015ec
1ce8b16
031c5eb
85242c5
5f99196
0eaa5d7
6fce77e
c7c2f0b
ee3b355
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2000,52 +2000,73 @@ The generated glue code looks like this: | |
.. versionadded:: 3.13 | ||
|
||
|
||
.. _clinic-howto-getter: | ||
.. _clinic-howto-pygetsetdef: | ||
|
||
How to generate a getter | ||
------------------------ | ||
How to declare ``PyGetSetDef`` ("getter/setter") functions | ||
---------------------------------------------------------- | ||
|
||
"Getters" are C functions that facilitate property-like access for a class. | ||
See :c:type:`getter <PyGetSetDef>` for details. | ||
You can use the ``@getter`` directive to generate an "impl" function for a | ||
getter using Argument Clinic. | ||
"Getters" and "setters" are C functions defined in a :c:type:`PyGetSetDef` struct | ||
that facilitate :py:class:`property`-like access for a class. | ||
You can use the ``@getter`` and ``@setter`` directives to generate | ||
"impl" functions for using Argument Clinic. | ||
|
||
This example -- taken from :cpy-file:`Modules/_io/bufferedio.c` -- | ||
shows the use of ``@getter`` in combination with | ||
This example --- taken from :cpy-file:`Modules/_io/textio.c` --- | ||
shows the use of ``@getter`` and ``@setter`` in combination with | ||
the :ref:`@critical_section <clinic-howto-critical-sections>` directive | ||
(which achieves thread safety without causing deadlocks between threads):: | ||
|
||
/*[clinic input] | ||
@critical_section | ||
@getter | ||
_io._Buffered.closed | ||
_io.TextIOWrapper._CHUNK_SIZE | ||
[clinic start generated code]*/ | ||
|
||
/*[clinic input] | ||
@critical_section | ||
@setter | ||
_io.TextIOWrapper._CHUNK_SIZE | ||
[clinic start generated code]*/ | ||
|
||
The generated glue code looks like this: | ||
|
||
.. code-block:: c | ||
|
||
static PyObject * | ||
_io__Buffered_closed_get(buffered *self, void *context) | ||
_io_TextIOWrapper__CHUNK_SIZE_get(textio *self, void *Py_UNUSED(context)) | ||
{ | ||
PyObject *return_value = NULL; | ||
|
||
Py_BEGIN_CRITICAL_SECTION(self); | ||
return_value = _io__Buffered_closed_get_impl(self); | ||
return_value = _io_TextIOWrapper__CHUNK_SIZE_get_impl(self); | ||
Py_END_CRITICAL_SECTION(); | ||
|
||
return return_value; | ||
} | ||
|
||
And then the implementation will work the same as a Python method which is | ||
decorated by :py:class:`property`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure this should be deleted; the Python REPL example at the bottom now has no introduction explaining what it's illustrating There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I will supplement to the above descriptions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm.. I think that |
||
static int | ||
_io_TextIOWrapper__CHUNK_SIZE_set(textio *self, PyObject *value, void *Py_UNUSED(context)) | ||
{ | ||
int return_value; | ||
Py_BEGIN_CRITICAL_SECTION(self); | ||
return_value = _io_TextIOWrapper__CHUNK_SIZE_set_impl(self, value); | ||
Py_END_CRITICAL_SECTION(); | ||
return return_value; | ||
} | ||
|
||
.. note:: | ||
|
||
Getters and setters must be declared as separate functions. | ||
The *value* parameter for a "setter" is added implicitly by Argument Clinic. | ||
|
||
corona10 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.. code-block:: pycon | ||
|
||
>>> import _io | ||
>>> a = _io._BufferedIOBase() | ||
>>> a.closed | ||
False | ||
>>> import sys, _io | ||
>>> a = _io.TextIOWrapper(sys.stdout) | ||
>>> a._CHUNK_SIZE | ||
8192 | ||
>>> a._CHUNK_SIZE = 30 | ||
>>> a._CHUNK_SIZE | ||
30 | ||
|
||
.. versionadded:: 3.13 | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.