mirror of
https://github.com/python/cpython.git
synced 2025-10-29 09:29:26 +00:00
gh-64658: Expand Argument Clinic return converter docs (#104175)
This commit is contained in:
parent
2318bedb36
commit
721a78395d
1 changed files with 30 additions and 15 deletions
|
|
@ -1033,19 +1033,36 @@ you're not permitted to use:
|
||||||
Using a return converter
|
Using a return converter
|
||||||
------------------------
|
------------------------
|
||||||
|
|
||||||
By default the impl function Argument Clinic generates for you returns ``PyObject *``.
|
By default, the impl function Argument Clinic generates for you returns
|
||||||
But your C function often computes some C type, then converts it into the ``PyObject *``
|
:c:type:`PyObject * <PyObject>`.
|
||||||
|
But your C function often computes some C type,
|
||||||
|
then converts it into the :c:type:`!PyObject *`
|
||||||
at the last moment. Argument Clinic handles converting your inputs from Python types
|
at the last moment. Argument Clinic handles converting your inputs from Python types
|
||||||
into native C types—why not have it convert your return value from a native C type
|
into native C types—why not have it convert your return value from a native C type
|
||||||
into a Python type too?
|
into a Python type too?
|
||||||
|
|
||||||
That's what a "return converter" does. It changes your impl function to return
|
That's what a "return converter" does. It changes your impl function to return
|
||||||
some C type, then adds code to the generated (non-impl) function to handle converting
|
some C type, then adds code to the generated (non-impl) function to handle converting
|
||||||
that value into the appropriate ``PyObject *``.
|
that value into the appropriate :c:type:`!PyObject *`.
|
||||||
|
|
||||||
The syntax for return converters is similar to that of parameter converters.
|
The syntax for return converters is similar to that of parameter converters.
|
||||||
You specify the return converter like it was a return annotation on the
|
You specify the return converter like it was a return annotation on the
|
||||||
function itself. Return converters behave much the same as parameter converters;
|
function itself, using ``->`` notation.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
.. code-block:: c
|
||||||
|
|
||||||
|
/*[clinic input]
|
||||||
|
add -> int
|
||||||
|
|
||||||
|
a: int
|
||||||
|
b: int
|
||||||
|
/
|
||||||
|
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
|
Return converters behave much the same as parameter converters;
|
||||||
they take arguments, the arguments are all keyword-only, and if you're not changing
|
they take arguments, the arguments are all keyword-only, and if you're not changing
|
||||||
any of the default arguments you can omit the parentheses.
|
any of the default arguments you can omit the parentheses.
|
||||||
|
|
||||||
|
|
@ -1066,19 +1083,17 @@ Currently Argument Clinic supports only a few return converters:
|
||||||
.. code-block:: none
|
.. code-block:: none
|
||||||
|
|
||||||
bool
|
bool
|
||||||
int
|
|
||||||
unsigned int
|
|
||||||
long
|
|
||||||
unsigned int
|
|
||||||
size_t
|
|
||||||
Py_ssize_t
|
|
||||||
float
|
|
||||||
double
|
double
|
||||||
DecodeFSDefault
|
float
|
||||||
|
int
|
||||||
|
long
|
||||||
|
Py_ssize_t
|
||||||
|
size_t
|
||||||
|
unsigned int
|
||||||
|
unsigned long
|
||||||
|
|
||||||
None of these take parameters. For the first three, return -1 to indicate
|
None of these take parameters.
|
||||||
error. For ``DecodeFSDefault``, the return type is ``const char *``; return a ``NULL``
|
For all of these, return ``-1`` to indicate error.
|
||||||
pointer to indicate an error.
|
|
||||||
|
|
||||||
To see all the return converters Argument Clinic supports, along with
|
To see all the return converters Argument Clinic supports, along with
|
||||||
their parameters (if any),
|
their parameters (if any),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue