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

Skip to content

Commit 2a72791

Browse files
committed
Issue #20226: Major improvements to Argument Clinic.
* You may now specify an expression as the default value for a parameter! Example: "sys.maxsize - 1". This support is intentionally quite limited; you may only use values that can be represented as static C values. * Removed "doc_default", simplified support for "c_default" and "py_default". (I'm not sure we still even need "py_default", but I'm leaving it in for now in case a use presents itself.) * Parameter lines support a trailing '\\' as a line continuation character, allowing you to break up long lines. * The argument parsing code generated when supporting optional groups now uses PyTuple_GET_SIZE instead of PyTuple_GetSize, leading to a 850% speedup in parsing. (Just kidding, this is an unmeasurable difference.) * A bugfix for the recent regression where the generated prototype from pydoc for builtins would be littered with unreadable "=<object ...>"" default values for parameters that had no default value. * Converted some asserts into proper failure messages. * Many doc improvements and fixes.
1 parent e1f5544 commit 2a72791

13 files changed

Lines changed: 478 additions & 193 deletions

File tree

Doc/c-api/arg.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ Other objects
294294
the object pointer is stored. If the Python object does not have the required
295295
type, :exc:`TypeError` is raised.
296296

297+
.. _o_ampersand:
298+
297299
``O&`` (object) [*converter*, *anything*]
298300
Convert a Python object to a C variable through a *converter* function. This
299301
takes two arguments: the first is a function, the second is the address of a C

Doc/howto/clinic.rst

Lines changed: 227 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ convert a function to work with it. Let's dive in!
127127
margin, with no line wider than 80 characters.
128128
(Argument Clinic will preserve indents inside the docstring.)
129129

130+
If the old docstring had a first line that looked like a function
131+
signature, throw that line away. (The docstring doesn't need it
132+
anymore--when you use ``help()`` on your builtin in the future,
133+
the first line will be built automatically based on the function's
134+
signature.)
135+
130136
Sample::
131137

132138
/*[clinic input]
@@ -196,6 +202,10 @@ convert a function to work with it. Let's dive in!
196202

197203
name_of_parameter: converter = default_value
198204

205+
Argument Clinic's support for "default values" is quite sophisticated;
206+
please see :ref:`the section below on default values <default_values>`
207+
for more information.
208+
199209
Add a blank line below the parameters.
200210

201211
What's a "converter"? It establishes both the type
@@ -513,16 +523,6 @@ The base function would now be named ``pickler_dumper()``,
513523
and the impl function would now be named ``pickler_dumper_impl()``.
514524

515525

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-
526526

527527
Converting functions using PyArg_UnpackTuple
528528
--------------------------------------------
@@ -654,35 +654,70 @@ the same converters.
654654
All arguments to Argument Clinic converters are keyword-only.
655655
All Argument Clinic converters accept the following arguments:
656656

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.
670-
671-
``required``
672-
If a parameter takes a default value, Argument Clinic infers that the
673-
parameter is optional. However, you may want a parameter to take a
674-
default value in C, but not behave in Python as if the parameter is
675-
optional. Passing in ``required=True`` to a converter tells Argument
676-
Clinic that this parameter is not optional, even if it has a default
677-
value.
678-
679-
(The need for ``required`` may be obviated by ``c_default``, which is
680-
newer but arguably a better solution.)
681-
682-
``annotation``
683-
The annotation value for this parameter. Not currently supported,
684-
because PEP 8 mandates that the Python library may not use
685-
annotations.
657+
``c_default``
658+
The default value for this parameter when defined in C.
659+
Specifically, this will be the initializer for the variable declared
660+
in the "parse function". See :ref:`the section on default values <default_values>`
661+
for how to use this.
662+
Specified as a string.
663+
664+
``annotation``
665+
The annotation value for this parameter. Not currently supported,
666+
because PEP 8 mandates that the Python library may not use
667+
annotations.
668+
669+
In addition, some converters accept additional arguments. Here is a list
670+
of these arguments, along with their meanings:
671+
672+
``bitwise``
673+
Only supported for unsigned integers. The native integer value of this
674+
Python argument will be written to the parameter without any range checking,
675+
even for negative values.
676+
677+
``converter``
678+
Only supported by the ``object`` converter. Specifies the name of a
679+
:ref:`C "converter function" <o_ampersand>`
680+
to use to convert this object to a native type.
681+
682+
``encoding``
683+
Only supported for strings. Specifies the encoding to use when converting
684+
this string from a Python str (Unicode) value into a C ``char *`` value.
685+
686+
``length``
687+
Only supported for strings. If true, requests that the length of the
688+
string be passed in to the impl function, just after the string parameter,
689+
in a parameter named ``<parameter_name>_length``.
690+
691+
``nullable``
692+
Only supported for strings. If true, this parameter may also be set to
693+
``None``, in which case the C parameter will be set to ``NULL``.
694+
695+
``subclass_of``
696+
Only supported for the ``object`` converter. Requires that the Python
697+
value be a subclass of a Python type, as expressed in C.
698+
699+
``types``
700+
Only supported for the ``object`` (and ``self``) converter. Specifies
701+
the C type that will be used to declare the variable. Default value is
702+
``"PyObject *"``.
703+
704+
``types``
705+
A string containing a list of Python types (and possibly pseudo-types);
706+
this restricts the allowable Python argument to values of these types.
707+
(This is not a general-purpose facility; as a rule it only supports
708+
specific lists of types as shown in the legacy converter table.)
709+
710+
``zeroes``
711+
Only supported for strings. If true, embedded NUL bytes (``'\\0'``) are
712+
permitted inside the value.
713+
714+
Please note, not every possible combination of arguments will work.
715+
Often these arguments are implemented internally by specific ``PyArg_ParseTuple``
716+
*format units*, with specific behavior. For example, currently you cannot
717+
call ``str`` and pass in ``zeroes=True`` without also specifying an ``encoding``;
718+
although it's perfectly reasonable to think this would work, these semantics don't
719+
map to any existing format unit. So Argument Clinic doesn't support it. (Or, at
720+
least, not yet.)
686721

687722
Below is a table showing the mapping of legacy converters into real
688723
Argument Clinic converters. On the left is the legacy converter,
@@ -720,9 +755,9 @@ on the right is the text you'd replace it with.
720755
``'u'`` ``Py_UNICODE``
721756
``'U'`` ``unicode``
722757
``'w*'`` ``Py_buffer(types='bytearray rwbuffer')``
723-
``'y#'`` ``str(type='bytes', length=True)``
758+
``'y#'`` ``str(types='bytes', length=True)``
724759
``'Y'`` ``PyByteArrayObject``
725-
``'y'`` ``str(type='bytes')``
760+
``'y'`` ``str(types='bytes')``
726761
``'y*'`` ``Py_buffer``
727762
``'Z#'`` ``Py_UNICODE(nullable=True, length=True)``
728763
``'z#'`` ``str(nullable=True, length=True)``
@@ -789,6 +824,90 @@ This restriction doesn't seem unreasonable; CPython itself always passes in stat
789824
hard-coded encoding strings for parameters whose format units start with ``e``.
790825

791826

827+
.. _default_values:
828+
829+
Parameter default values
830+
------------------------
831+
832+
Default values for parameters can be any of a number of values.
833+
At their simplest, they can be string, int, or float literals::
834+
835+
foo: str = "abc"
836+
bar: int = 123
837+
bat: float = 45.6
838+
839+
They can also use any of Python's built-in constants::
840+
841+
yep: bool = True
842+
nope: bool = False
843+
nada: object = None
844+
845+
There's also special support for a default value of ``NULL``, and
846+
for simple expressions, documented in the following sections.
847+
848+
849+
The ``NULL`` default value
850+
--------------------------
851+
852+
For string and object parameters, you can set them to ``None`` to indicate
853+
that there's no default. However, that means the C variable will be
854+
initialized to ``Py_None``. For convenience's sakes, there's a special
855+
value called ``NULL`` for just this reason: from Python's perspective it
856+
behaves like a default value of ``None``, but the C variable is initialized
857+
with ``NULL``.
858+
859+
Expressions specified as default values
860+
---------------------------------------
861+
862+
The default value for a parameter can be more than just a literal value.
863+
It can be an entire expression, using math operators and looking up attributes
864+
on objects. However, this support isn't exactly simple, because of some
865+
non-obvious semantics.
866+
867+
Consider the following example::
868+
869+
foo: Py_ssize_t = sys.maxsize - 1
870+
871+
``sys.maxsize`` can have different values on different platforms. Therefore
872+
Argument Clinic can't simply evaluate that expression locally and hard-code it
873+
in C. So it stores the default in such a way that it will get evaluated at
874+
runtime, when the user asks for the function's signature.
875+
876+
What namespace is available when the expression is evaluated? It's evaluated
877+
in the context of the module the builtin came from. So, if your module has an
878+
attribute called "``max_widgets``", you may simply use it::
879+
880+
foo: Py_ssize_t = max_widgets
881+
882+
If the symbol isn't found in the current module, it fails over to looking in
883+
``sys.modules``. That's how it can find ``sys.maxsize`` for example. (Since you
884+
don't know in advance what modules the user will load into their interpreter,
885+
it's best to restrict yourself to modules that are preloaded by Python itself.)
886+
887+
Evaluating default values only at runtime means Argument Clinic can't compute
888+
the correct equivalent C default value. So you need to tell it explicitly.
889+
When you use an expression, you must also specify the equivalent expression
890+
in C, using the ``c_default`` parameter to the converter::
891+
892+
foo: Py_ssize_t(c_default="PY_SSIZE_T_MAX - 1") = sys.maxsize - 1
893+
894+
Another complication: Argument Clinic can't know in advance whether or not the
895+
expression you supply is valid. It parses it to make sure it looks legal, but
896+
it can't *actually* know. You must be very careful when using expressions to
897+
specify values that are guaranteed to be valid at runtime!
898+
899+
Finally, because expressions must be representable as static C values, there
900+
are many restrictions on legal expressions. Here's a list of Python features
901+
you're not permitted to use:
902+
903+
* Function calls.
904+
* Inline if statements (``3 if foo else 5``).
905+
* Automatic sequence unpacking (``*[1, 2, 3]``).
906+
* List/set/dict comprehensions and generator expressions.
907+
* Tuple/list/set/dict literals.
908+
909+
910+
792911
Using a return converter
793912
------------------------
794913

@@ -1096,7 +1215,73 @@ any arguments.
10961215
You can still use a self converter, a return converter, and specify
10971216
a ``type`` argument to the object converter for ``METH_O``.
10981217

1099-
Using Argument Clinic in Python files
1218+
The #ifdef trick
1219+
----------------------------------------------
1220+
1221+
If you're converting a function that isn't available on all platforms,
1222+
there's a trick you can use to make life a little easier. The existing
1223+
code probably looks like this::
1224+
1225+
#ifdef HAVE_FUNCTIONNAME
1226+
static module_functionname(...)
1227+
{
1228+
...
1229+
}
1230+
#endif /* HAVE_FUNCTIONNAME */
1231+
1232+
And then in the ``PyMethodDef`` structure at the bottom you'll have::
1233+
1234+
#ifdef HAVE_FUNCTIONNAME
1235+
{'functionname', ... },
1236+
#endif /* HAVE_FUNCTIONNAME */
1237+
1238+
In this scenario, you should change the code to look like the following::
1239+
1240+
#ifdef HAVE_FUNCTIONNAME
1241+
/*[clinic input]
1242+
module.functionname
1243+
...
1244+
[clinic start generated code]*/
1245+
static module_functionname(...)
1246+
{
1247+
...
1248+
}
1249+
#endif /* HAVE_FUNCTIONNAME */
1250+
1251+
Run Argument Clinic on the code in this state, then refresh the file in
1252+
your editor. Now you'll have the generated code, including the #define
1253+
for the ``PyMethodDef``, like so::
1254+
1255+
#ifdef HAVE_FUNCTIONNAME
1256+
/*[clinic input]
1257+
...
1258+
[clinic start generated code]*/
1259+
...
1260+
#define MODULE_FUNCTIONNAME \
1261+
{'functionname', ... },
1262+
...
1263+
/*[clinic end generated code: checksum=...]*/
1264+
static module_functionname(...)
1265+
{
1266+
...
1267+
}
1268+
#endif /* HAVE_FUNCTIONNAME */
1269+
1270+
Change the #endif at the bottom as follows::
1271+
1272+
#else
1273+
#define MODULE_FUNCTIONNAME
1274+
#endif /* HAVE_FUNCTIONNAME */
1275+
1276+
Now you can remove the #ifdefs around the ``PyMethodDef`` structure
1277+
at the end, and replace those three lines with ``MODULE_FUNCTIONNAME``.
1278+
If the function is available, the macro turns into the ``PyMethodDef``
1279+
static value, including the trailing comma; if the function isn't
1280+
available, the macro turns into nothing. Perfect!
1281+
1282+
(This is the preferred approach for optional functions; in the future,
1283+
Argument Clinic may generate the entire ``PyMethodDef`` structure.)
1284+
11001285
-------------------------------------
11011286

11021287
It's actually possible to use Argument Clinic to preprocess Python files.

0 commit comments

Comments
 (0)