@@ -1129,10 +1129,19 @@ array_ass_sub_simple(PyArrayObject *self, PyObject *ind, PyObject *op)
1129
1129
return ret ;
1130
1130
}
1131
1131
1132
+ /* Check if ind is a tuple and if it has as many elements as arr has axes. */
1133
+ static NPY_INLINE int
1134
+ _is_full_index (PyObject * ind , PyArrayObject * arr )
1135
+ {
1136
+ return PyTuple_Check (ind ) && (PyTuple_GET_SIZE (ind ) == PyArray_NDIM (arr ));
1137
+ }
1132
1138
1133
- /* return -1 if tuple-object seq is not a tuple of integers.
1134
- otherwise fill vals with converted integers
1135
- */
1139
+ /*
1140
+ * Returns 0 if tuple-object seq is not a tuple of integers.
1141
+ * If the return value is positive, vals will be filled with the elements
1142
+ * from the tuple.
1143
+ * Returns -1 on error.
1144
+ */
1136
1145
static int
1137
1146
_tuple_of_integers (PyObject * seq , npy_intp * vals , int maxvals )
1138
1147
{
@@ -1144,15 +1153,21 @@ _tuple_of_integers(PyObject *seq, npy_intp *vals, int maxvals)
1144
1153
obj = PyTuple_GET_ITEM (seq , i );
1145
1154
if ((PyArray_Check (obj ) && PyArray_NDIM ((PyArrayObject * )obj ) > 0 )
1146
1155
|| PyList_Check (obj )) {
1147
- return -1 ;
1156
+ return 0 ;
1148
1157
}
1149
1158
temp = PyArray_PyIntAsIntp (obj );
1150
1159
if (error_converting (temp )) {
1151
- return -1 ;
1160
+ return 0 ;
1161
+ }
1162
+ if (!PyIndex_Check_Or_Unsupported (obj )) {
1163
+ if (DEPRECATE ("non-integer scalar index. In a future numpy "
1164
+ "release, this will raise an error." ) < 0 ) {
1165
+ return -1 ;
1166
+ }
1152
1167
}
1153
1168
vals [i ] = temp ;
1154
1169
}
1155
- return 0 ;
1170
+ return 1 ;
1156
1171
}
1157
1172
1158
1173
@@ -1257,22 +1272,26 @@ array_ass_sub(PyArrayObject *self, PyObject *ind, PyObject *op)
1257
1272
}
1258
1273
1259
1274
/* Integer-tuple */
1260
- if (PyTuple_Check (ind ) &&
1261
- (PyTuple_GET_SIZE (ind ) == PyArray_NDIM (self )) &&
1262
- (_tuple_of_integers (ind , vals , PyArray_NDIM (self )) >= 0 )) {
1263
- int idim , ndim = PyArray_NDIM (self );
1264
- npy_intp * shape = PyArray_DIMS (self );
1265
- npy_intp * strides = PyArray_STRIDES (self );
1266
- char * item = PyArray_DATA (self );
1267
-
1268
- for (idim = 0 ; idim < ndim ; idim ++ ) {
1269
- npy_intp v = vals [idim ];
1270
- if (check_and_adjust_index (& v , shape [idim ], idim ) < 0 ) {
1271
- return -1 ;
1275
+ if (_is_full_index (ind , self )) {
1276
+ ret = _tuple_of_integers (ind , vals , PyArray_NDIM (self ));
1277
+ /* In case an exception occurred (e.g. in PyErr_WarnEx) */
1278
+ if (ret < 0 ) {
1279
+ return -1 ;
1280
+ }
1281
+ else if (ret > 0 ) {
1282
+ int idim , ndim = PyArray_NDIM (self );
1283
+ npy_intp * shape = PyArray_DIMS (self );
1284
+ npy_intp * strides = PyArray_STRIDES (self );
1285
+ char * item = PyArray_DATA (self );
1286
+ for (idim = 0 ; idim < ndim ; idim ++ ) {
1287
+ npy_intp v = vals [idim ];
1288
+ if (check_and_adjust_index (& v , shape [idim ], idim ) < 0 ) {
1289
+ return -1 ;
1290
+ }
1291
+ item += v * strides [idim ];
1272
1292
}
1273
- item += v * strides [ idim ] ;
1293
+ return PyArray_DESCR ( self ) -> f -> setitem ( op , item , self ) ;
1274
1294
}
1275
- return PyArray_DESCR (self )-> f -> setitem (op , item , self );
1276
1295
}
1277
1296
PyErr_Clear ();
1278
1297
@@ -1350,6 +1369,7 @@ array_subscript_nice(PyArrayObject *self, PyObject *op)
1350
1369
{
1351
1370
1352
1371
PyArrayObject * mp ;
1372
+ int ret ;
1353
1373
npy_intp vals [NPY_MAXDIMS ];
1354
1374
1355
1375
if (PyInt_Check (op ) || PyArray_IsScalar (op , Integer ) ||
@@ -1364,27 +1384,39 @@ array_subscript_nice(PyArrayObject *self, PyObject *op)
1364
1384
return array_item_nice (self , (Py_ssize_t ) value );
1365
1385
}
1366
1386
}
1367
- /* optimization for a tuple of integers */
1368
- if (PyArray_NDIM (self ) > 1 &&
1369
- PyTuple_Check (op ) &&
1370
- (PyTuple_GET_SIZE (op ) == PyArray_NDIM (self )) &&
1371
- (_tuple_of_integers (op , vals , PyArray_NDIM (self )) >= 0 )) {
1372
- int idim , ndim = PyArray_NDIM (self );
1373
- npy_intp * shape = PyArray_DIMS (self );
1374
- npy_intp * strides = PyArray_STRIDES (self );
1375
- char * item = PyArray_DATA (self );
1376
-
1377
- for (idim = 0 ; idim < ndim ; idim ++ ) {
1378
- npy_intp v = vals [idim ];
1379
- if (check_and_adjust_index (& v , shape [idim ], idim ) < 0 ) {
1380
- return NULL ;
1387
+ /*
1388
+ * Optimization for a tuple of integers that is the same size as the
1389
+ * array's dimension.
1390
+ */
1391
+ if (PyArray_NDIM (self ) > 1 && _is_full_index (op , self )) {
1392
+ ret = _tuple_of_integers (op , vals , PyArray_NDIM (self ));
1393
+ /* In case an exception occurred (e.g. in PyErr_WarnEx) */
1394
+ if (ret < 0 ) {
1395
+ return NULL ;
1396
+ }
1397
+ else if (ret > 0 ) {
1398
+ int idim , ndim = PyArray_NDIM (self );
1399
+ npy_intp * shape = PyArray_DIMS (self );
1400
+ npy_intp * strides = PyArray_STRIDES (self );
1401
+ char * item = PyArray_DATA (self );
1402
+ for (idim = 0 ; idim < ndim ; idim ++ ) {
1403
+ npy_intp v = vals [idim ];
1404
+ if (check_and_adjust_index (& v , shape [idim ], idim ) < 0 ) {
1405
+ return NULL ;
1406
+ }
1407
+ item += v * strides [idim ];
1381
1408
}
1382
- item += v * strides [ idim ] ;
1409
+ return PyArray_Scalar ( item , PyArray_DESCR ( self ), ( PyObject * ) self ) ;
1383
1410
}
1384
- return PyArray_Scalar (item , PyArray_DESCR (self ), (PyObject * )self );
1385
1411
}
1386
1412
PyErr_Clear ();
1387
-
1413
+ if ((PyNumber_Check (op ) || PyArray_IsScalar (op , Number )) &&
1414
+ !PyIndex_Check_Or_Unsupported (op )) {
1415
+ if (DEPRECATE ("non-integer scalar index. In a future numpy "
1416
+ "release, this will raise an error." ) < 0 ) {
1417
+ return NULL ;
1418
+ }
1419
+ }
1388
1420
mp = (PyArrayObject * )array_subscript (self , op );
1389
1421
/*
1390
1422
* mp could be a scalar if op is not an Int, Scalar, Long or other Index
0 commit comments