Skip to content

Commit 1515155

Browse files
committed
ENH: DeprecationWarning for non-int slice indices.
1 parent 5de9999 commit 1515155

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

numpy/core/src/multiarray/iterators.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,32 @@ slice_coerce_index(PyObject *o, npy_intp *v)
229229
return 1;
230230
}
231231

232-
/* This is basically PySlice_GetIndicesEx, but with our coercion
233-
* of indices to integers (plus, that function is new in Python 2.3) */
232+
/*
233+
* Issue a DeprecationWarning for slice parameters that do not pass a
234+
* PyIndex_Check, returning -1 if an error occurs.
235+
*
236+
* N.B. This function, like slice_GetIndices, will be obsolete once
237+
* non-integer slice parameters becomes an error rather than a warning.
238+
*/
239+
static NPY_INLINE int
240+
_validate_slice_parameter(PyObject *o)
241+
{
242+
if (!PyIndex_Check_Or_Unsupported(o)) {
243+
if (DEPRECATE("non-integer slice parameter. In a future numpy "
244+
"release, this will raise an error.") < 0) {
245+
return -1;
246+
}
247+
}
248+
return 0;
249+
}
250+
251+
/*
252+
* This is basically PySlice_GetIndicesEx, but with our coercion
253+
* of indices to integers (plus, that function is new in Python 2.3)
254+
*
255+
* N.B. The coercion to integers is deprecated; once the DeprecationWarning
256+
* is changed to an error, it would seem that this is obsolete.
257+
*/
234258
NPY_NO_EXPORT int
235259
slice_GetIndices(PySliceObject *r, npy_intp length,
236260
npy_intp *start, npy_intp *stop, npy_intp *step,
@@ -242,6 +266,9 @@ slice_GetIndices(PySliceObject *r, npy_intp length,
242266
*step = 1;
243267
}
244268
else {
269+
if (_validate_slice_parameter(r->step) < 0) {
270+
return -1;
271+
}
245272
if (!slice_coerce_index(r->step, step)) {
246273
return -1;
247274
}
@@ -257,6 +284,9 @@ slice_GetIndices(PySliceObject *r, npy_intp length,
257284
*start = *step < 0 ? length-1 : 0;
258285
}
259286
else {
287+
if (_validate_slice_parameter(r->start) < 0) {
288+
return -1;
289+
}
260290
if (!slice_coerce_index(r->start, start)) {
261291
return -1;
262292
}
@@ -275,6 +305,9 @@ slice_GetIndices(PySliceObject *r, npy_intp length,
275305
*stop = defstop;
276306
}
277307
else {
308+
if (_validate_slice_parameter(r->stop) < 0) {
309+
return -1;
310+
}
278311
if (!slice_coerce_index(r->stop, stop)) {
279312
return -1;
280313
}

0 commit comments

Comments
 (0)