Skip to content

Commit 44905cb

Browse files
Added reference for argument clinic directives
1 parent 1ec5840 commit 44905cb

File tree

2 files changed

+304
-0
lines changed

2 files changed

+304
-0
lines changed
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
Argument Clinic References
2+
---------------------------
3+
4+
@getter Directive
5+
-----------------
6+
7+
**Description**:
8+
The ``@getter`` directive generates a C function that provides read-only access to an attribute, mimicking Python’s property. It is used to create an "impl" function that retrieves the value of an attribute.
9+
10+
**Parameters**:
11+
- None
12+
13+
**Usage Example**:
14+
The use of ``@getter`` in combination with the :ref:`@critical_section <clinic-howto-critical-sections>` directive::
15+
16+
/*[clinic input]
17+
@critical_section
18+
@getter
19+
_io.TextIOWrapper._CHUNK_SIZE
20+
[clinic start generated code]*/
21+
22+
23+
The generated glue code looks like this:
24+
25+
.. code-block:: c
26+
27+
static PyObject *
28+
_io_TextIOWrapper__CHUNK_SIZE_get(textio *self, void *Py_UNUSED(context))
29+
{
30+
PyObject *return_value = NULL;
31+
Py_BEGIN_CRITICAL_SECTION(self);
32+
return_value = _io_TextIOWrapper__CHUNK_SIZE_get_impl(self);
33+
Py_END_CRITICAL_SECTION();
34+
return return_value
35+
}
36+
37+
**Explanation**:
38+
- The generated C function, ``get()``, retrieves the ``_CHUNK_SIZE value`` from the ``TextIOWrapper`` class.
39+
- A critical section ensures thread safety when accessing the attribute.
40+
41+
Refer to the ``getter`` function section in :ref:`clinic-howto-getset <clinic-howto-pygetsetdef>` for more details.
42+
43+
@setter Directive
44+
-----------------
45+
46+
**Description**:
47+
The ``@setter`` directive generates a C function that sets a value for an attribute, providing property-like write access. It works alongside the ``@getter`` directive to create read-write properties for Python objects in C extensions.
48+
49+
**Parameters**:
50+
- value (automatically passed): The new value to be set for the attribute.
51+
52+
**Usage Example**:
53+
The use of ``@setter`` in combination with the :ref:`@critical_section <clinic-howto-critical-sections>` directive::
54+
55+
/*[clinic input]
56+
@critical_section
57+
@setter
58+
_io.TextIOWrapper._CHUNK_SIZE
59+
[clinic start generated code]*/
60+
61+
The generated glue code looks like this:
62+
63+
.. code-block:: c
64+
65+
static int
66+
_io_TextIOWrapper__CHUNK_SIZE_set(textio *self, PyObject *value, void *Py_UNUSED(context))
67+
{
68+
int return_value;
69+
Py_BEGIN_CRITICAL_SECTION(self);
70+
return_value = _io_TextIOWrapper__CHUNK_SIZE_set_impl(self, value);
71+
Py_END_CRITICAL_SECTION();
72+
return return_value;
73+
}
74+
75+
**Explanation**:
76+
- The setter function assigns a new value to ``_CHUNK_SIZE``.
77+
- A ``critical section`` ensures thread safety during the update.
78+
- The ``value`` parameter is added implicitly by Argument Clinic and represents the new data passed by the user.
79+
80+
Refer to the ``setter`` function section in :ref:`clinic-howto-getset <clinic-howto-pygetsetdef>` for more details.
81+
82+
@critical_section Directive
83+
----------------------------
84+
85+
**Description**:
86+
- The ``@critical_section`` directive in Argument Clinic ensures thread safety by wrapping a function call within a critical section. This section locks the first argument's associated object, preventing concurrent access during execution. It is particularly useful in GIL-free builds of CPython to avoid deadlocks across threads.
87+
88+
**Usage**:
89+
- Without additional arguments: Locks only the first argument’s object.
90+
- With additional arguments: Allows locking more objects by passing their C variable names as parameters.
91+
92+
Example Usage::
93+
94+
/*[clinic input]
95+
@critical_section
96+
_io._Buffered.close
97+
[clinic start generated code]*/
98+
99+
The generated glue code looks like this:
100+
101+
.. code-block:: c
102+
103+
static PyObject *
104+
_io__Buffered_close(buffered *self, PyObject *Py_UNUSED(ignored))
105+
{
106+
PyObject *return_value = NULL;
107+
108+
Py_BEGIN_CRITICAL_SECTION(self);
109+
return_value = _io__Buffered_close_impl(self);
110+
Py_END_CRITICAL_SECTION();
111+
112+
return return_value;
113+
}
114+
115+
Example with additional arguments::
116+
117+
/*[clinic input]
118+
@critical_section object
119+
_weakref.getweakrefcount -> Py_ssize_t
120+
121+
object: object
122+
/
123+
Return the number of weak references to 'object'.
124+
[clinic start generated code]*/
125+
126+
The generated glue code looks like this:
127+
128+
.. code-block:: c
129+
130+
static PyObject *
131+
_weakref_getweakrefs(PyObject *module, PyObject *object)
132+
{
133+
PyObject *return_value = NULL;
134+
135+
Py_BEGIN_CRITICAL_SECTION(object);
136+
return_value = _weakref_getweakrefs_impl(module, object);
137+
Py_END_CRITICAL_SECTION();
138+
139+
return return_value;
140+
}
141+
142+
**Explanation**:
143+
- ``Critical Section Behavior``: The critical section acquires a lock on entry and releases it on exit.
144+
- ``GIL Impact``: In CPython builds with the GIL, these sections are no-ops, as the GIL already ensures thread safety.
145+
146+
Refer to the ``@critical_section`` directive in the section :ref:`@critical_section <clinic-howto-critical-sections>` for details.
147+
148+
@text_signature Directive
149+
-------------------------
150+
151+
**Description**:
152+
The ``@text_signiture`` directive is used to override the default generated signature in the docstring.
153+
154+
**Parameters**:
155+
- The ``@text_signiture`` directive takes on argument: ``( @text_signiture [arg1] )``
156+
157+
Example from :cpy-file:`Objects/codeobject.c`::
158+
159+
160+
/*[clinic input]
161+
@text_signature "($self, /, **changes)"
162+
code.replace
163+
*
164+
co_argcount: int(c_default="self->co_argcount") = unchanged
165+
co_posonlyargcount: int(c_default="self->co_posonlyargcount") = unchanged
166+
# etc ...
167+
168+
Return a copy of the code object with new values for the specified fields.
169+
[clinic start generated output]*/
170+
171+
The generated docstring ends up looking like this:
172+
173+
.. code-block:: none
174+
175+
replace($self, /, **changes)
176+
--
177+
178+
Return a copy of the code object with new values for the specified fields.
179+
180+
dump Directive
181+
---------------
182+
183+
**Description**:
184+
Dumps the contents of the specified destination buffer into the output of the current block and empties it. This is useful in configuring how and where Argument Clinic outputs generated code, particularly in multi-pass processing.
185+
186+
**Parameters**:
187+
- The ``dump`` directive takes in one argument ``(dump [arg1])``
188+
- Where ``arg1`` corresponds to the name of the ``destination`` of the buffer to dump, valid options being ``buffer`` or ``two-pass`` destinations.
189+
190+
.. code-block:: none
191+
192+
dump <destination>
193+
194+
195+
output Directive
196+
----------------
197+
198+
**Description**:
199+
The ``output`` directive specifies where Argument Clinic should output specific fields, supporting custom and preset configurations.
200+
201+
**Parameters**:
202+
- The ``output`` directive takes in two arguments ``(output [arg1] [arg2])``
203+
- Where ``arg1`` is the ``field`` to output and ``arg2`` is the ``destination`` for the output.
204+
205+
.. code-block:: none
206+
207+
output <field> <destination>
208+
209+
Outputs the specified ``<field>`` to ``<destination>``.
210+
211+
.. note::
212+
213+
For all fields, use ``everything`` as the ``<field>``.
214+
215+
**Configuration Commands**:
216+
- ``output push``: Pushes the current configuration to the stack for temporary changes.
217+
- ``output pop``: Restores the last configuration from the stack.
218+
- ``preset <preset>``: Sets output configuration to a preset.
219+
220+
**Preset Options**:
221+
- ``block``: Outputs most fields immediately after input block.
222+
- ``file``: Outputs fields to a separate file for inclusion.
223+
- ``buffer``: Saves output to dump later, reducing file edits.
224+
- ``two-pass``: Uses two buffers to handle forward declarations and definitions separately.
225+
- ``partial-buffer``: Outputs smaller code chunks to ``block``, larger ones to ``buffer``.
226+
227+
destination Directive
228+
---------------------
229+
230+
**Description**:
231+
The ``destination`` directive allows for operations on output destinations.
232+
233+
**Parameters**:
234+
- The ``destination`` directive takes in two arguments ``(destination [arg1] [arg2])``.
235+
- Where ``arg1`` is the ``name`` of the output destination and ``arg2`` is the ``command`` on the output destination.
236+
237+
.. code-block:: none
238+
239+
destination <name> <command> [...]
240+
241+
There two defined subcommands: ``new`` and ``clear``
242+
243+
The ``new`` subcommand works like this:
244+
245+
.. code-block:: none
246+
247+
destination <name> new <type>
248+
249+
This creates a new destination with name ``<name>`` and type ``<type>``.
250+
251+
There are five destination types:
252+
253+
- ``suppress``: Discards the output.
254+
- ``block``: Writes to the current block.
255+
- ``buffer``: A simple text buffer.
256+
- ``file``: A text file (requires a filename template).
257+
- ``two-pass``: A two-pass buffer.
258+
259+
260+
The ``clear`` subcommand works like this:
261+
262+
.. code-block:: none
263+
264+
destination <name> clear
265+
266+
This removes all the accumulated text in the specified destination.
267+
268+
set Directive
269+
-------------
270+
The ``set`` directive allows you to configure internal variables in Argument Clinic.
271+
272+
**Syntax**:
273+
274+
.. code-block:: none
275+
276+
set line_prefix "string"
277+
set line_suffix "string"
278+
279+
**Description**:
280+
- ``line_prefix``: Specifies a string to prepend to each line of Clinic's output.
281+
- ``line_suffix``: Specifies a string to append to each line of Clinic's output.
282+
283+
**Format Strings**:
284+
285+
Both ``line_prefix`` and ``line_suffix`` support the following format strings:
286+
287+
``{block comment start}``
288+
Turns into the string ``/*``, the start-comment text sequence for C files.
289+
290+
``{block comment end}``
291+
Turns into the string ``*/``, the end-comment text sequence for C files.
292+
293+
preserve Directive
294+
------------------
295+
296+
The ``preserver`` directive tells Clinic that the current contents of the output should be kept, unmodified.
297+
This is used internally by Clinic when dumping output into ``file`` files; wrapping
298+
it in a Clinic block lets Clinic use its existing checksum functionality to ensure
299+
the file was not modified by hand before it gets overwritten.
300+
301+
.. code-block:: none
302+
303+
preserve

development-tools/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ Development tools
1111
gdb
1212
clang
1313
warnings
14+
clinic-directive-reference

0 commit comments

Comments
 (0)