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

Skip to content

Commit 77561cc

Browse files
committed
Issue #20141: Improved Argument Clinic's support for the PyArg_Parse "O!"
format unit.
1 parent 16c5191 commit 77561cc

5 files changed

Lines changed: 4379 additions & 4388 deletions

File tree

Doc/howto/clinic.rst

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ on the right is the text you'd replace it with.
640640
``'K'`` ``unsigned_PY_LONG_LONG``
641641
``'L'`` ``PY_LONG_LONG``
642642
``'n'`` ``Py_ssize_t``
643-
``'O!'`` ``object(type='name_of_Python_type')``
643+
``'O!'`` ``object(subclass_of='&PySomething_Type')``
644644
``'O&'`` ``object(converter='name_of_c_function')``
645645
``'O'`` ``object``
646646
``'p'`` ``bool``
@@ -693,20 +693,22 @@ conversion functions, or types, or strings specifying an encoding.
693693
(But "legacy converters" don't support arguments. That's why we
694694
skipped them for your first function.) The argument you specified
695695
to the format unit is now an argument to the converter; this
696-
argument is either ``converter`` (for ``O&``), ``type`` (for ``O!``),
696+
argument is either ``converter`` (for ``O&``), ``subclass_of`` (for ``O!``),
697697
or ``encoding`` (for all the format units that start with ``e``).
698698

699-
Note that ``object()`` must explicitly support each Python type you specify
700-
for the ``type`` argument. Currently it only supports ``str``. It should be
701-
easy to add more, just edit ``Tools/clinic/clinic.py``, search for ``O!`` in
702-
the text, and add more entries to the dict mapping types to strings just above it.
699+
When using ``subclass_of``, you may also want to use the other
700+
custom argument for ``object()``: ``type``, which lets you set the type
701+
actually used for the parameter. For example, if you want to ensure
702+
that the object is a subclass of ``PyUnicode_Type``, you probably want
703+
to use the converter ``object(type='PyUnicodeObject *', subclass_of='&PyUnicode_Type')``.
703704

704-
Note also that this approach takes away some possible flexibility for the format
705-
units starting with ``e``. It used to be possible to decide at runtime what
705+
One possible problem with using Argument Clinic: it takes away some possible
706+
flexibility for the format units starting with ``e``. When writing a
707+
``PyArg_Parse`` call by hand, you could theoretically decide at runtime what
706708
encoding string to pass in to :c:func:`PyArg_ParseTuple`. But now this string must
707-
be hard-coded at compile-time. This limitation is deliberate; it made supporting
708-
this format unit much easier, and may allow for future compile-time optimizations.
709-
This restriction does not seem unreasonable; CPython itself always passes in static
709+
be hard-coded at Argument-Clinic-preprocessing-time. This limitation is deliberate;
710+
it made supporting this format unit much easier, and may allow for future optimizations.
711+
This restriction doesn't seem unreasonable; CPython itself always passes in static
710712
hard-coded encoding strings for parameters whose format units start with ``e``.
711713

712714

@@ -796,7 +798,8 @@ block, and ensure that its converter is an instance of
796798
``self_converter`` or a subclass thereof.
797799

798800
What's the point? This lets you automatically cast ``self``
799-
from ``PyObject *`` to a custom type.
801+
from ``PyObject *`` to a custom type, just like ``object()``
802+
does with its ``type`` parameter.
800803

801804
How do you specify the custom type you want to cast ``self`` to?
802805
If you only have one or two functions with the same type for ``self``,

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Library
2121
Tools/Demos
2222
-----------
2323

24+
- Issue #20141: Improved Argument Clinic's support for the PyArg_Parse "O!"
25+
format unit.
26+
2427
- Issue #20144: Argument Clinic now supports simple symbolic constants
2528
as parameter default values.
2629

Modules/unicodedata.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static Py_UCS4 getuchar(PyUnicodeObject *obj)
117117
118118
unicodedata.UCD.decimal
119119
120-
unichr: object(type='str')
120+
unichr: object(type='PyUnicodeObject *', subclass_of='&PyUnicode_Type')
121121
default: object=NULL
122122
/
123123
@@ -140,13 +140,13 @@ PyDoc_STRVAR(unicodedata_UCD_decimal__doc__,
140140
{"decimal", (PyCFunction)unicodedata_UCD_decimal, METH_VARARGS, unicodedata_UCD_decimal__doc__},
141141

142142
static PyObject *
143-
unicodedata_UCD_decimal_impl(PyObject *self, PyObject *unichr, PyObject *default_value);
143+
unicodedata_UCD_decimal_impl(PyObject *self, PyUnicodeObject *unichr, PyObject *default_value);
144144

145145
static PyObject *
146146
unicodedata_UCD_decimal(PyObject *self, PyObject *args)
147147
{
148148
PyObject *return_value = NULL;
149-
PyObject *unichr;
149+
PyUnicodeObject *unichr;
150150
PyObject *default_value = NULL;
151151

152152
if (!PyArg_ParseTuple(args,
@@ -160,8 +160,8 @@ unicodedata_UCD_decimal(PyObject *self, PyObject *args)
160160
}
161161

162162
static PyObject *
163-
unicodedata_UCD_decimal_impl(PyObject *self, PyObject *unichr, PyObject *default_value)
164-
/*[clinic checksum: 9576fa55f4ea0be82968af39dc9d0283e634beeb]*/
163+
unicodedata_UCD_decimal_impl(PyObject *self, PyUnicodeObject *unichr, PyObject *default_value)
164+
/*[clinic checksum: 73edde0e9cd5913ea174c4fa81504369761b7426]*/
165165
{
166166
PyUnicodeObject *v = (PyUnicodeObject *)unichr;
167167
int have_old = 0;

0 commit comments

Comments
 (0)