@@ -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+
1121193. 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
1741868. 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!
455467Advanced 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
459493Renaming the C functions generated by Argument Clinic
460494-----------------------------------------------------
@@ -479,6 +513,29 @@ The base function would now be named ``pickler_dumper()``,
479513and 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+
482539Optional Groups
483540---------------
484541
@@ -570,8 +627,8 @@ To save time, and to minimize how much you need to learn
570627to achieve your first port to Argument Clinic, the walkthrough above tells
571628you to use "legacy converters". "Legacy converters" are a convenience,
572629designed 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
576633However, in the long term we probably want all our blocks to
577634use 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
591648In a nutshell, the syntax for Argument Clinic (non-legacy) converters
592649looks like a Python function call. However, if there are no explicit
@@ -597,12 +654,19 @@ the same converters.
597654All arguments to Argument Clinic converters are keyword-only.
598655All 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
681749it accepts, along with the default value for each parameter.
682750Just 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
685761Advanced converters
686762-------------------
@@ -745,15 +821,26 @@ encode the value you return like normal.
745821
746822Currently 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
753835None of these take parameters. For the first three, return -1 to indicate
754836error. For ``DecodeFSDefault ``, the return type is ``char * ``; return a NULL
755837pointer 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+
757844To see all the return converters Argument Clinic supports, along with
758845their parameters (if any),
759846just 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``,
9511031specifically the implementation of ``CReturnConverter `` and
9521032all 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
9551055Using Argument Clinic in Python files
9561056-------------------------------------
0 commit comments