@@ -197,21 +197,8 @@ unpack_scalar(PyObject *index, PyObject **result, npy_intp NPY_UNUSED(result_n))
197
197
/**
198
198
* Turn an index argument into a c-array of `PyObject *`s, one for each index.
199
199
*
200
- * When a scalar is passed, this is written directly to the buffer. When a
201
- * tuple is passed, the tuple elements are unpacked into the buffer.
202
- *
203
- * When some other sequence is passed, this implements the following section
204
- * from the advanced indexing docs to decide whether to unpack or just write
205
- * one element:
206
- *
207
- * > In order to remain backward compatible with a common usage in Numeric,
208
- * > basic slicing is also initiated if the selection object is any non-ndarray
209
- * > sequence (such as a list) containing slice objects, the Ellipsis object,
210
- * > or the newaxis object, but not for integer arrays or other embedded
211
- * > sequences.
212
- *
213
- * It might be worth deprecating this behaviour (gh-4434), in which case the
214
- * entire function should become a simple check of PyTuple_Check.
200
+ * When a tuple is passed, the tuple elements are unpacked into the buffer.
201
+ * Anything else is handled by unpack_scalar().
215
202
*
216
203
* @param index The index object, which may or may not be a tuple. This is
217
204
* a borrowed reference.
@@ -228,8 +215,11 @@ unpack_scalar(PyObject *index, PyObject **result, npy_intp NPY_UNUSED(result_n))
228
215
NPY_NO_EXPORT npy_intp
229
216
unpack_indices (PyObject * index , PyObject * * result , npy_intp result_n )
230
217
{
231
- npy_intp n , i ;
232
- npy_bool commit_to_unpack ;
218
+ npy_intp n ;
219
+
220
+ /* It is likely that the logic here can be simplified. See the discussion on
221
+ * https://github.com/numpy/numpy/pull/21029
222
+ */
233
223
234
224
/* Fast route for passing a tuple */
235
225
if (PyTuple_CheckExact (index )) {
@@ -262,95 +252,7 @@ unpack_indices(PyObject *index, PyObject **result, npy_intp result_n)
262
252
return n ;
263
253
}
264
254
265
- /*
266
- * At this point, we're left with a non-tuple, non-array, sequence:
267
- * typically, a list. We use some somewhat-arbitrary heuristics from here
268
- * onwards to decided whether to treat that list as a single index, or a
269
- * list of indices.
270
- */
271
-
272
- /* if len fails, treat like a scalar */
273
- n = PySequence_Size (index );
274
- if (n < 0 ) {
275
- PyErr_Clear ();
276
- return unpack_scalar (index , result , result_n );
277
- }
278
-
279
- /*
280
- * Backwards compatibility only takes effect for short sequences - otherwise
281
- * we treat it like any other scalar.
282
- *
283
- * Sequences < NPY_MAXDIMS with any slice objects
284
- * or newaxis, Ellipsis or other arrays or sequences
285
- * embedded, are considered equivalent to an indexing
286
- * tuple. (`a[[[1,2], [3,4]]] == a[[1,2], [3,4]]`)
287
- */
288
- if (n >= NPY_MAXDIMS ) {
289
- return unpack_scalar (index , result , result_n );
290
- }
291
-
292
- /* In case we change result_n elsewhere */
293
- assert (n <= result_n );
294
-
295
- /*
296
- * Some other type of short sequence - assume we should unpack it like a
297
- * tuple, and then decide whether that was actually necessary.
298
- */
299
- commit_to_unpack = 0 ;
300
- for (i = 0 ; i < n ; i ++ ) {
301
- PyObject * tmp_obj = result [i ] = PySequence_GetItem (index , i );
302
-
303
- if (commit_to_unpack ) {
304
- /* propagate errors */
305
- if (tmp_obj == NULL ) {
306
- goto fail ;
307
- }
308
- }
309
- else {
310
- /*
311
- * if getitem fails (unusual) before we've committed, then stop
312
- * unpacking
313
- */
314
- if (tmp_obj == NULL ) {
315
- PyErr_Clear ();
316
- break ;
317
- }
318
-
319
- /* decide if we should treat this sequence like a tuple */
320
- if (PyArray_Check (tmp_obj )
321
- || PySequence_Check (tmp_obj )
322
- || PySlice_Check (tmp_obj )
323
- || tmp_obj == Py_Ellipsis
324
- || tmp_obj == Py_None ) {
325
- if (DEPRECATE_FUTUREWARNING (
326
- "Using a non-tuple sequence for multidimensional "
327
- "indexing is deprecated; use `arr[tuple(seq)]` "
328
- "instead of `arr[seq]`. In the future this will be "
329
- "interpreted as an array index, `arr[np.array(seq)]`, "
330
- "which will result either in an error or a different "
331
- "result." ) < 0 ) {
332
- i ++ ; /* since loop update doesn't run */
333
- goto fail ;
334
- }
335
- commit_to_unpack = 1 ;
336
- }
337
- }
338
- }
339
-
340
- /* unpacking was the right thing to do, and we already did it */
341
- if (commit_to_unpack ) {
342
- return n ;
343
- }
344
- /* got to the end, never found an indication that we should have unpacked */
345
- else {
346
- /* we partially filled result, so empty it first */
347
- multi_DECREF (result , i );
348
- return unpack_scalar (index , result , result_n );
349
- }
350
-
351
- fail :
352
- multi_DECREF (result , i );
353
- return -1 ;
255
+ return unpack_scalar (index , result , result_n );
354
256
}
355
257
356
258
/**
0 commit comments