Skip to content

Commit 80ec836

Browse files
bpo-29748: Added the slice index converter in Argument Clinic. (#549)
1 parent a5af6e1 commit 80ec836

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

Include/ceval.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ PyAPI_FUNC(Py_ssize_t) _PyEval_RequestCodeExtraIndex(freefunc);
223223

224224
#ifndef Py_LIMITED_API
225225
PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
226+
PyAPI_FUNC(int) _PyEval_SliceIndexOrNone(PyObject *, Py_ssize_t *);
226227
PyAPI_FUNC(void) _PyEval_SignalAsyncExc(void);
227228
#endif
228229

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,8 @@ Build
937937
Tools/Demos
938938
-----------
939939

940+
- bpo-29748: Added the slice index converter in Argument Clinic.
941+
940942
- bpo-24037: Argument Clinic now uses the converter `bool(accept={int})` rather
941943
than `int` for semantical booleans. This avoids repeating the default
942944
value for Python and C and will help in converting to `bool` in future.

Objects/listobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,8 +2210,8 @@ PyList_AsTuple(PyObject *v)
22102210
list.index
22112211
22122212
value: object
2213-
start: object(converter="_PyEval_SliceIndex", type="Py_ssize_t") = 0
2214-
stop: object(converter="_PyEval_SliceIndex", type="Py_ssize_t", c_default="PY_SSIZE_T_MAX") = sys.maxsize
2213+
start: slice_index(accept={int}) = 0
2214+
stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
22152215
/
22162216
22172217
Return first index of value.
@@ -2222,7 +2222,7 @@ Raises ValueError if the value is not present.
22222222
static PyObject *
22232223
list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
22242224
Py_ssize_t stop)
2225-
/*[clinic end generated code: output=ec51b88787e4e481 input=70b7247e398a6999]*/
2225+
/*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
22262226
{
22272227
Py_ssize_t i;
22282228

Python/ceval.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4917,6 +4917,13 @@ _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
49174917
return 1;
49184918
}
49194919

4920+
int
4921+
_PyEval_SliceIndexOrNone(PyObject *v, Py_ssize_t *pi)
4922+
{
4923+
return v == Py_None || _PyEval_SliceIndex(v, pi);
4924+
}
4925+
4926+
49204927
#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
49214928
"BaseException is not allowed"
49224929

Tools/clinic/clinic.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,6 +2659,18 @@ class Py_ssize_t_converter(CConverter):
26592659
c_ignored_default = "0"
26602660

26612661

2662+
class slice_index_converter(CConverter):
2663+
type = 'Py_ssize_t'
2664+
2665+
def converter_init(self, *, accept={int, NoneType}):
2666+
if accept == {int}:
2667+
self.converter = '_PyEval_SliceIndex'
2668+
elif accept == {int, NoneType}:
2669+
self.converter = '_PyEval_SliceIndexOrNone'
2670+
else:
2671+
fail("slice_index_converter: illegal 'accept' argument " + repr(accept))
2672+
2673+
26622674
class float_converter(CConverter):
26632675
type = 'float'
26642676
default_type = float

0 commit comments

Comments
 (0)