Skip to content

Commit 3fd7b70

Browse files
committed
BUG: numpy.einsum indexing arrays now accept numpy int type
See numpy#15961.
1 parent 651bb25 commit 3fd7b70

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

numpy/core/src/multiarray/multiarraymodule.c

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,7 +2437,6 @@ einsum_list_to_subscripts(PyObject *obj, char *subscripts, int subsize)
24372437
}
24382438
size = PySequence_Size(obj);
24392439

2440-
24412440
for (i = 0; i < size; ++i) {
24422441
item = PySequence_Fast_GET_ITEM(obj, i);
24432442
/* Ellipsis */
@@ -2459,46 +2458,48 @@ einsum_list_to_subscripts(PyObject *obj, char *subscripts, int subsize)
24592458
subscripts[subindex++] = '.';
24602459
ellipsis = 1;
24612460
}
2462-
/* Subscript */
2463-
else if (PyInt_Check(item) || PyLong_Check(item)) {
2464-
long s = PyInt_AsLong(item);
2465-
npy_bool bad_input = 0;
2461+
else {
2462+
long s = PyLong_AsLong(item);
2463+
/* Subscript */
2464+
if(!PyErr_Occurred()) {
2465+
npy_bool bad_input = 0;
24662466

2467-
if (subindex + 1 >= subsize) {
2468-
PyErr_SetString(PyExc_ValueError,
2469-
"subscripts list is too long");
2470-
Py_DECREF(obj);
2471-
return -1;
2472-
}
2467+
if (subindex + 1 >= subsize) {
2468+
PyErr_SetString(PyExc_ValueError,
2469+
"subscripts list is too long");
2470+
Py_DECREF(obj);
2471+
return -1;
2472+
}
24732473

2474-
if ( s < 0 ) {
2475-
bad_input = 1;
2476-
}
2477-
else if (s < 26) {
2478-
subscripts[subindex++] = 'A' + (char)s;
2479-
}
2480-
else if (s < 2*26) {
2481-
subscripts[subindex++] = 'a' + (char)s - 26;
2474+
if ( s < 0 ) {
2475+
bad_input = 1;
2476+
}
2477+
else if (s < 26) {
2478+
subscripts[subindex++] = 'A' + (char)s;
2479+
}
2480+
else if (s < 2*26) {
2481+
subscripts[subindex++] = 'a' + (char)s - 26;
2482+
}
2483+
else {
2484+
bad_input = 1;
2485+
}
2486+
2487+
if (bad_input) {
2488+
PyErr_SetString(PyExc_ValueError,
2489+
"subscript is not within the valid range [0, 52)");
2490+
Py_DECREF(obj);
2491+
return -1;
2492+
}
24822493
}
2494+
/* Invalid */
24832495
else {
2484-
bad_input = 1;
2485-
}
2486-
2487-
if (bad_input) {
24882496
PyErr_SetString(PyExc_ValueError,
2489-
"subscript is not within the valid range [0, 52)");
2497+
"each subscript must be either an integer "
2498+
"or an ellipsis");
24902499
Py_DECREF(obj);
24912500
return -1;
24922501
}
2493-
}
2494-
/* Invalid */
2495-
else {
2496-
PyErr_SetString(PyExc_ValueError,
2497-
"each subscript must be either an integer "
2498-
"or an ellipsis");
2499-
Py_DECREF(obj);
2500-
return -1;
2501-
}
2502+
}
25022503
}
25032504

25042505
Py_DECREF(obj);

0 commit comments

Comments
 (0)