summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/multiarray/iterators.c16
-rw-r--r--numpy/core/tests/test_multiarray.py11
2 files changed, 21 insertions, 6 deletions
diff --git a/numpy/core/src/multiarray/iterators.c b/numpy/core/src/multiarray/iterators.c
index 640896f2a..576ea89b3 100644
--- a/numpy/core/src/multiarray/iterators.c
+++ b/numpy/core/src/multiarray/iterators.c
@@ -1063,14 +1063,16 @@ static PyMemberDef iter_members[] = {
T_OBJECT,
offsetof(PyArrayIterObject, ao),
READONLY, NULL},
- {"index",
- T_INT,
- offsetof(PyArrayIterObject, index),
- READONLY, NULL},
{NULL, 0, 0, 0, NULL},
};
static PyObject *
+iter_index_get(PyArrayIterObject *self)
+{
+ return PyArray_PyIntFromIntp(self->index);
+}
+
+static PyObject *
iter_coords_get(PyArrayIterObject *self)
{
int nd;
@@ -1096,10 +1098,12 @@ iter_coords_get(PyArrayIterObject *self)
}
static PyGetSetDef iter_getsets[] = {
+ {"index",
+ (getter)iter_index_get,
+ NULL, NULL, NULL},
{"coords",
(getter)iter_coords_get,
- NULL,
- NULL, NULL},
+ NULL, NULL, NULL},
{NULL, NULL, NULL, NULL, NULL},
};
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index 25dd76256..470a45337 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -5364,6 +5364,17 @@ class TestFlat:
assert_(abs(sys.getrefcount(ind) - rc_ind) < 50)
assert_(abs(sys.getrefcount(indtype) - rc_indtype) < 50)
+ def test_index_getset(self):
+ it = np.arange(10).reshape(2, 1, 5).flat
+ with pytest.raises(AttributeError):
+ it.index = 10
+
+ for _ in it:
+ pass
+ # Check the value of `.index` is updated correctly (see also gh-19153)
+ # If the type was incorrect, this would show up on big-endian machines
+ assert it.index == it.base.size
+
class TestResize: