]> granicus.if.org Git - python/commitdiff
bpo-29748: Added the slice index converter in Argument Clinic. (#549)
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 19 Mar 2017 17:37:40 +0000 (19:37 +0200)
committerGitHub <noreply@github.com>
Sun, 19 Mar 2017 17:37:40 +0000 (19:37 +0200)
Include/ceval.h
Misc/NEWS
Objects/listobject.c
Python/ceval.c
Tools/clinic/clinic.py

index 8760fe50b70d6ae057b434e6b931818adba4092b..25976bb37681ae7179f8e6f8522684a652e4c2fc 100644 (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
 
index f317527b9352fd7c98a12448d934fd38ff01c819..8509f52692d279e936739437094a3cbdf36fcd6e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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.
index 9c1c9d9689e81a6c03b74ee7302db9d48f65eb7c..9b42106e30b06455f216ef87746bbe547faf3c17 100644 (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;
 
index e682fc1afd0ed0ec73e8d15c4f3b2407ed459f9c..8ee58f5d30b529ff29522f02fcb9357db86dac02 100644 (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"
 
index 3d51c1dcada45eb4f94ee81f4ad5d57adfafa851..55faf1ea246aa0cd2f28ac28cb82f2009243397f 100755 (executable)
@@ -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