Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4a55fc5

Browse files
committed
Issue #20214: Fixed a number of small issues and documentation errors in
Argument Clinic (see issue for details).
1 parent 583baa8 commit 4a55fc5

4 files changed

Lines changed: 223 additions & 50 deletions

File tree

Doc/howto/clinic.rst

Lines changed: 120 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ convert a function to work with it. Let's dive in!
109109
support all of these scenarios. But these are advanced
110110
topics--let's do something simpler for your first function.
111111

112+
Also, if the function has multiple calls to :c:func:`PyArg_ParseTuple`
113+
or :c:func:`PyArg_ParseTupleAndKeywords` where it supports different
114+
types for the same argument, or if the function uses something besides
115+
PyArg_Parse functions to parse its arguments, it probably
116+
isn't suitable for conversion to Argument Clinic. Argument Clinic
117+
doesn't support generic functions or polymorphic parameters.
118+
112119
3. Add the following boilerplate above the function, creating our block::
113120

114121
/*[clinic input]
@@ -170,6 +177,11 @@ convert a function to work with it. Let's dive in!
170177
Write a pickled representation of obj to the open file.
171178
[clinic start generated code]*/
172179

180+
The name of the class and module should be the same as the one
181+
seen by Python. Check the name defined in the :c:type:`PyModuleDef`
182+
or :c:type:`PyTypeObject` as appropriate.
183+
184+
173185

174186
8. Declare each of the parameters to the function. Each parameter
175187
should get its own line. All the parameter lines should be
@@ -455,6 +467,28 @@ convert a function to work with it. Let's dive in!
455467
Advanced Topics
456468
===============
457469

470+
Now that you've had some experience working with Argument Clinic, it's time
471+
for some advanced topics.
472+
473+
474+
Symbolic default values
475+
-----------------------
476+
477+
The default value you provide for a parameter can't be any arbitrary
478+
expression. Currently the following are explicitly supported:
479+
480+
* Numeric constants (integer and float)
481+
* String constants
482+
* ``True``, ``False``, and ``None``
483+
* Simple symbolic constants like ``sys.maxsize``, which must
484+
start with the name of the module
485+
486+
In case you're curious, this is implemented in ``from_builtin()``
487+
in ``Lib/inspect.py``.
488+
489+
(In the future, this may need to get even more elaborate,
490+
to allow full expressions like ``CONSTANT - 1``.)
491+
458492

459493
Renaming the C functions generated by Argument Clinic
460494
-----------------------------------------------------
@@ -479,6 +513,29 @@ The base function would now be named ``pickler_dumper()``,
479513
and the impl function would now be named ``pickler_dumper_impl()``.
480514

481515

516+
The NULL default value
517+
----------------------
518+
519+
For string and object parameters, you can set them to ``None`` to indicate
520+
that there is no default. However, that means the C variable will be
521+
initialized to ``Py_None``. For convenience's sakes, there's a special
522+
value called ``NULL`` for just this case: from Python's perspective it
523+
behaves like a default value of ``None``, but the C variable is initialized
524+
with ``NULL``.
525+
526+
527+
Converting functions using PyArg_UnpackTuple
528+
--------------------------------------------
529+
530+
To convert a function parsing its arguments with :c:func:`PyArg_UnpackTuple`,
531+
simply write out all the arguments, specifying each as an ``object``. You
532+
may specify the ``type`` argument to cast the type as appropriate. All
533+
arguments should be marked positional-only (add a ``/`` on a line by itself
534+
after the last argument).
535+
536+
Currently the generated code will use :c:func:`PyArg_ParseTuple`, but this
537+
will change soon.
538+
482539
Optional Groups
483540
---------------
484541

@@ -570,8 +627,8 @@ To save time, and to minimize how much you need to learn
570627
to achieve your first port to Argument Clinic, the walkthrough above tells
571628
you to use "legacy converters". "Legacy converters" are a convenience,
572629
designed explicitly to make porting existing code to Argument Clinic
573-
easier. And to be clear, their use is entirely acceptable when porting
574-
code for Python 3.4.
630+
easier. And to be clear, their use is acceptable when porting code for
631+
Python 3.4.
575632

576633
However, in the long term we probably want all our blocks to
577634
use Argument Clinic's real syntax for converters. Why? A couple
@@ -585,8 +642,8 @@ reasons:
585642
restricted to what :c:func:`PyArg_ParseTuple` supports; this flexibility
586643
won't be available to parameters using legacy converters.
587644

588-
Therefore, if you don't mind a little extra effort, you should consider
589-
using normal converters instead of legacy converters.
645+
Therefore, if you don't mind a little extra effort, please use the normal
646+
converters instead of legacy converters.
590647

591648
In a nutshell, the syntax for Argument Clinic (non-legacy) converters
592649
looks like a Python function call. However, if there are no explicit
@@ -597,12 +654,19 @@ the same converters.
597654
All arguments to Argument Clinic converters are keyword-only.
598655
All Argument Clinic converters accept the following arguments:
599656

600-
``doc_default``
601-
If the parameter takes a default value, normally this value is also
602-
provided in the ``inspect.Signature`` metadata, and displayed in the
603-
docstring. ``doc_default`` lets you override the value used in these
604-
two places: pass in a string representing the Python value you wish
605-
to use in these two contexts.
657+
``py_default``
658+
The default value for this parameter when defined in Python.
659+
Specifically, the value that will be used in the ``inspect.Signature``
660+
string.
661+
If a default value is specified for the parameter, defaults to
662+
``repr(default)``, else defaults to ``None``.
663+
Specified as a string.
664+
665+
``c_default``
666+
The default value for this parameter when defined in C.
667+
Specifically, this will be the initializer for the variable declared
668+
in the "parse function".
669+
Specified as a string.
606670

607671
``required``
608672
If a parameter takes a default value, Argument Clinic infers that the
@@ -612,6 +676,9 @@ All Argument Clinic converters accept the following arguments:
612676
Clinic that this parameter is not optional, even if it has a default
613677
value.
614678

679+
(The need for ``required`` may be obviated by ``c_default``, which is
680+
newer but arguably a better solution.)
681+
615682
``annotation``
616683
The annotation value for this parameter. Not currently supported,
617684
because PEP 8 mandates that the Python library may not use
@@ -634,10 +701,11 @@ on the right is the text you'd replace it with.
634701
``'et'`` ``str(encoding='name_of_encoding', types='bytes bytearray str')``
635702
``'f'`` ``float``
636703
``'h'`` ``short``
637-
``'H'`` ``unsigned_short``
704+
``'H'`` ``unsigned_short(bitwise=True)``
638705
``'i'`` ``int``
639-
``'I'`` ``unsigned_int``
640-
``'K'`` ``unsigned_PY_LONG_LONG``
706+
``'I'`` ``unsigned_int(bitwise=True)``
707+
``'k'`` ``unsigned_long(bitwise=True)``
708+
``'K'`` ``unsigned_PY_LONG_LONG(bitwise=True)``
641709
``'L'`` ``PY_LONG_LONG``
642710
``'n'`` ``Py_ssize_t``
643711
``'O!'`` ``object(subclass_of='&PySomething_Type')``
@@ -681,6 +749,14 @@ available. For each converter it'll show you all the parameters
681749
it accepts, along with the default value for each parameter.
682750
Just run ``Tools/clinic/clinic.py --converters`` to see the full list.
683751

752+
Py_buffer
753+
---------
754+
755+
When using the ``Py_buffer`` converter
756+
(or the ``'s*'``, ``'w*'``, ``'*y'``, or ``'z*'`` legacy converters)
757+
note that the code Argument Clinic generates for you will automatically
758+
call :c:func:`PyBuffer_Release` on the buffer for you.
759+
684760

685761
Advanced converters
686762
-------------------
@@ -745,15 +821,26 @@ encode the value you return like normal.
745821

746822
Currently Argument Clinic supports only a few return converters::
747823

824+
bool
748825
int
826+
unsigned int
749827
long
828+
unsigned int
829+
size_t
750830
Py_ssize_t
831+
float
832+
double
751833
DecodeFSDefault
752834

753835
None of these take parameters. For the first three, return -1 to indicate
754836
error. For ``DecodeFSDefault``, the return type is ``char *``; return a NULL
755837
pointer to indicate an error.
756838

839+
(There's also an experimental ``NoneType`` converter, which lets you
840+
return ``Py_None`` on success or ``NULL`` on failure, without having
841+
to increment the reference count on ``Py_None``. I'm not sure it adds
842+
enough clarity to be worth using.)
843+
757844
To see all the return converters Argument Clinic supports, along with
758845
their parameters (if any),
759846
just run ``Tools/clinic/clinic.py --converters`` for the full list.
@@ -873,13 +960,6 @@ to specify in your subclass. Here's the current list:
873960
The Python default value for this parameter, as a Python value.
874961
Or the magic value ``unspecified`` if there is no default.
875962

876-
``doc_default``
877-
``default`` as it should appear in the documentation,
878-
as a string.
879-
Or ``None`` if there is no default.
880-
This string, when run through ``eval()``, should produce
881-
a Python value.
882-
883963
``py_default``
884964
``default`` as it should appear in Python code,
885965
as a string.
@@ -951,6 +1031,26 @@ write your own return converter, please read ``Tools/clinic/clinic.py``,
9511031
specifically the implementation of ``CReturnConverter`` and
9521032
all its subclasses.
9531033

1034+
METH_O and METH_NOARGS
1035+
----------------------------------------------
1036+
1037+
To convert a function using ``METH_O``, make sure the function's
1038+
single argument is using the ``object`` converter, and mark the
1039+
arguments as positional-only::
1040+
1041+
/*[clinic input]
1042+
meth_o_sample
1043+
1044+
argument: object
1045+
/
1046+
[clinic start generated code]*/
1047+
1048+
1049+
To convert a function using ``METH_NOARGS``, just don't specify
1050+
any arguments.
1051+
1052+
You can still use a self converter, a return converter, and specify
1053+
a ``type`` argument to the object converter for ``METH_O``.
9541054

9551055
Using Argument Clinic in Python files
9561056
-------------------------------------

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ Tests
7272
Tools/Demos
7373
-----------
7474

75+
- Issue #20214: Fixed a number of small issues and documentation errors in
76+
Argument Clinic (see issue for details).
77+
7578
- Issue #20196: Fixed a bug where Argument Clinic did not generate correct
7679
parsing code for functions with positional-only parameters where all arguments
7780
are optional.

Modules/zlibmodule.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ static PyObject *
198198
zlib_compress(PyModuleDef *module, PyObject *args)
199199
{
200200
PyObject *return_value = NULL;
201-
Py_buffer bytes = {NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL};
201+
Py_buffer bytes = {NULL, NULL};
202202
int group_right_1 = 0;
203203
int level = 0;
204204

@@ -219,15 +219,15 @@ zlib_compress(PyModuleDef *module, PyObject *args)
219219
return_value = zlib_compress_impl(module, &bytes, group_right_1, level);
220220

221221
/* Cleanup for bytes */
222-
if (bytes.buf)
222+
if (bytes.obj)
223223
PyBuffer_Release(&bytes);
224224

225225
return return_value;
226226
}
227227

228228
static PyObject *
229229
zlib_compress_impl(PyModuleDef *module, Py_buffer *bytes, int group_right_1, int level)
230-
/*[clinic end generated code: checksum=9f055a396620bc1a8a13d74c3496249528b32b0d]*/
230+
/*[clinic end generated code: checksum=2c59af563a4595c5ecea4011701f482ae350aa5f]*/
231231
{
232232
PyObject *ReturnVal = NULL;
233233
Byte *input, *output = NULL;
@@ -789,7 +789,7 @@ static PyObject *
789789
zlib_Decompress_decompress(PyObject *self, PyObject *args)
790790
{
791791
PyObject *return_value = NULL;
792-
Py_buffer data = {NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL};
792+
Py_buffer data = {NULL, NULL};
793793
unsigned int max_length = 0;
794794

795795
if (!PyArg_ParseTuple(args,
@@ -800,15 +800,15 @@ zlib_Decompress_decompress(PyObject *self, PyObject *args)
800800

801801
exit:
802802
/* Cleanup for data */
803-
if (data.buf)
803+
if (data.obj)
804804
PyBuffer_Release(&data);
805805

806806
return return_value;
807807
}
808808

809809
static PyObject *
810810
zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data, unsigned int max_length)
811-
/*[clinic end generated code: checksum=5b1e4f9f1ef8eca55fff78356f9df0c81232ed3b]*/
811+
/*[clinic end generated code: checksum=e0058024c4a97b411d2e2197791b89fde175f76f]*/
812812
{
813813
int err;
814814
unsigned int old_length, length = DEFAULTALLOC;

0 commit comments

Comments
 (0)