[Python-checkins] cpython (merge 3.5 -> 3.6): Issue #27867: Function PySlice_GetIndicesEx() is replaced with a macro if

serhiy.storchaka python-checkins at python.org
Wed Jan 25 06:29:16 EST 2017


https://hg.python.org/cpython/rev/b4457fe7fdb8
changeset:   106313:b4457fe7fdb8
branch:      3.6
parent:      106309:25fc68e22eee
parent:      106312:96f5327f7253
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Wed Jan 25 13:25:52 2017 +0200
summary:
  Issue #27867: Function PySlice_GetIndicesEx() is replaced with a macro if
Py_LIMITED_API is not set or set to the value between 0x03050400
and 0x03060000 (not including) or 0x03060100 or higher.

files:
  Include/sliceobject.h |  16 +++++-
  Misc/NEWS             |   4 +
  Objects/sliceobject.c |  78 +++++++++++++++++++++---------
  PC/python3.def        |   2 +
  4 files changed, 74 insertions(+), 26 deletions(-)


diff --git a/Include/sliceobject.h b/Include/sliceobject.h
--- a/Include/sliceobject.h
+++ b/Include/sliceobject.h
@@ -41,8 +41,20 @@
 PyAPI_FUNC(int) PySlice_GetIndices(PyObject *r, Py_ssize_t length,
                                   Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step);
 PyAPI_FUNC(int) PySlice_GetIndicesEx(PyObject *r, Py_ssize_t length,
-				    Py_ssize_t *start, Py_ssize_t *stop,
-				    Py_ssize_t *step, Py_ssize_t *slicelength);
+                                     Py_ssize_t *start, Py_ssize_t *stop,
+                                     Py_ssize_t *step, Py_ssize_t *slicelength);
+
+#if !defined(Py_LIMITED_API) || (Py_LIMITED_API+0 >= 0x03050400 && Py_LIMITED_API+0 < 0x03060000) || Py_LIMITED_API+0 >= 0x03060100
+#define PySlice_GetIndicesEx(slice, length, start, stop, step, slicelen) (  \
+    PySlice_Unpack((slice), (start), (stop), (step)) < 0 ? -1 :             \
+    ((*slicelen = PySlice_AdjustIndices((length), (start), (stop), *(step))), \
+     0))
+PyAPI_FUNC(int) PySlice_Unpack(PyObject *slice,
+                               Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step);
+PyAPI_FUNC(Py_ssize_t) PySlice_AdjustIndices(Py_ssize_t length,
+                                             Py_ssize_t *start, Py_ssize_t *stop,
+                                             Py_ssize_t step);
+#endif
 
 #ifdef __cplusplus
 }
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -143,6 +143,10 @@
 C API
 -----
 
+- Issue #27867: Function PySlice_GetIndicesEx() is replaced with a macro if
+  Py_LIMITED_API is not set or set to the value between 0x03050400
+  and 0x03060000 (not including) or 0x03060100 or higher.
+
 - Issue #29083: Fixed the declaration of some public API functions.
   PyArg_VaParse() and PyArg_VaParseTupleAndKeywords() were not available in
   limited API.  PyArg_ValidateKeywordArguments(), PyArg_UnpackTuple() and
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -191,15 +191,12 @@
 }
 
 int
-PySlice_GetIndicesEx(PyObject *_r, Py_ssize_t length,
-                     Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
-                     Py_ssize_t *slicelength)
+PySlice_Unpack(PyObject *_r,
+               Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
 {
     PySliceObject *r = (PySliceObject*)_r;
     /* this is harder to get right than you might think */
 
-    Py_ssize_t defstart, defstop;
-
     if (r->step == Py_None) {
         *step = 1;
     }
@@ -219,42 +216,75 @@
             *step = -PY_SSIZE_T_MAX;
     }
 
-    defstart = *step < 0 ? length-1 : 0;
-    defstop = *step < 0 ? -1 : length;
-
     if (r->start == Py_None) {
-        *start = defstart;
+        *start = *step < 0 ? PY_SSIZE_T_MAX-1 : 0;;
     }
     else {
         if (!_PyEval_SliceIndex(r->start, start)) return -1;
-        if (*start < 0) *start += length;
-        if (*start < 0) *start = (*step < 0) ? -1 : 0;
-        if (*start >= length)
-            *start = (*step < 0) ? length - 1 : length;
     }
 
     if (r->stop == Py_None) {
-        *stop = defstop;
+        *stop = *step < 0 ? -PY_SSIZE_T_MAX : PY_SSIZE_T_MAX;
     }
     else {
         if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
-        if (*stop < 0) *stop += length;
-        if (*stop < 0) *stop = (*step < 0) ? -1 : 0;
-        if (*stop >= length)
-            *stop = (*step < 0) ? length - 1 : length;
     }
 
-    if ((*step < 0 && *stop >= *start)
-        || (*step > 0 && *start >= *stop)) {
-        *slicelength = 0;
+    return 0;
+}
+
+Py_ssize_t
+PySlice_AdjustIndices(Py_ssize_t length,
+                      Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step)
+{
+    /* this is harder to get right than you might think */
+
+    assert(step != 0);
+    assert(step >= -PY_SSIZE_T_MAX);
+
+    if (*start < 0) {
+        *start += length;
+        if (*start < 0) {
+            *start = (step < 0) ? -1 : 0;
+        }
     }
-    else if (*step < 0) {
-        *slicelength = (*stop-*start+1)/(*step)+1;
+    else if (*start >= length) {
+        *start = (step < 0) ? length - 1 : length;
+    }
+
+    if (*stop < 0) {
+        *stop += length;
+        if (*stop < 0) {
+            *stop = (step < 0) ? -1 : 0;
+        }
+    }
+    else  if (*stop >= length) {
+        *stop = (step < 0) ? length - 1 : length;
+    }
+
+    if (step < 0) {
+        if (*stop < *start) {
+            return (*start - *stop - 1) / (-step) + 1;
+        }
     }
     else {
-        *slicelength = (*stop-*start-1)/(*step)+1;
+        if (*start < *stop) {
+            return (*stop - *start - 1) / step + 1;
+        }
     }
+    return 0;
+}
 
+#undef PySlice_GetIndicesEx
+
+int
+PySlice_GetIndicesEx(PyObject *_r, Py_ssize_t length,
+                     Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
+                     Py_ssize_t *slicelength)
+{
+    if (PySlice_Unpack(_r, start, stop, step) < 0)
+        return -1;
+    *slicelength = PySlice_AdjustIndices(length, start, stop, *step);
     return 0;
 }
 
diff --git a/PC/python3.def b/PC/python3.def
--- a/PC/python3.def
+++ b/PC/python3.def
@@ -531,10 +531,12 @@
   PySet_Pop=python36.PySet_Pop
   PySet_Size=python36.PySet_Size
   PySet_Type=python36.PySet_Type DATA
+  PySlice_AdjustIndices=python36.PySlice_AdjustIndices
   PySlice_GetIndices=python36.PySlice_GetIndices
   PySlice_GetIndicesEx=python36.PySlice_GetIndicesEx
   PySlice_New=python36.PySlice_New
   PySlice_Type=python36.PySlice_Type DATA
+  PySlice_Unpack=python36.PySlice_Unpack
   PySortWrapper_Type=python36.PySortWrapper_Type DATA
   PyState_AddModule=python36.PyState_AddModule
   PyState_FindModule=python36.PyState_FindModule

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list