bpo-29748: Added the slice index converter in Argument Clinic. (#549)

This commit is contained in:
Serhiy Storchaka 2017-03-19 19:37:40 +02:00 committed by GitHub
parent a5af6e1af7
commit 80ec8364f1
5 changed files with 25 additions and 3 deletions

View File

@ -223,6 +223,7 @@ PyAPI_FUNC(Py_ssize_t) _PyEval_RequestCodeExtraIndex(freefunc);
#ifndef Py_LIMITED_API
PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
PyAPI_FUNC(int) _PyEval_SliceIndexOrNone(PyObject *, Py_ssize_t *);
PyAPI_FUNC(void) _PyEval_SignalAsyncExc(void);
#endif

View File

@ -937,6 +937,8 @@ Build
Tools/Demos
-----------
- bpo-29748: Added the slice index converter in Argument Clinic.
- bpo-24037: Argument Clinic now uses the converter `bool(accept={int})` rather
than `int` for semantical booleans. This avoids repeating the default
value for Python and C and will help in converting to `bool` in future.

View File

@ -2210,8 +2210,8 @@ PyList_AsTuple(PyObject *v)
list.index
value: object
start: object(converter="_PyEval_SliceIndex", type="Py_ssize_t") = 0
stop: object(converter="_PyEval_SliceIndex", type="Py_ssize_t", c_default="PY_SSIZE_T_MAX") = sys.maxsize
start: slice_index(accept={int}) = 0
stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
/
Return first index of value.
@ -2222,7 +2222,7 @@ Raises ValueError if the value is not present.
static PyObject *
list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
Py_ssize_t stop)
/*[clinic end generated code: output=ec51b88787e4e481 input=70b7247e398a6999]*/
/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
{
Py_ssize_t i;

View File

@ -4917,6 +4917,13 @@ _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
return 1;
}
int
_PyEval_SliceIndexOrNone(PyObject *v, Py_ssize_t *pi)
{
return v == Py_None || _PyEval_SliceIndex(v, pi);
}
#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
"BaseException is not allowed"

View File

@ -2659,6 +2659,18 @@ class Py_ssize_t_converter(CConverter):
c_ignored_default = "0"
class slice_index_converter(CConverter):
type = 'Py_ssize_t'
def converter_init(self, *, accept={int, NoneType}):
if accept == {int}:
self.converter = '_PyEval_SliceIndex'
elif accept == {int, NoneType}:
self.converter = '_PyEval_SliceIndexOrNone'
else:
fail("slice_index_converter: illegal 'accept' argument " + repr(accept))
class float_converter(CConverter):
type = 'float'
default_type = float