Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Include/ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,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.
Expand Down
6 changes: 3 additions & 3 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
12 changes: 12 additions & 0 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down