Skip to content

Commit

Permalink
BUG: Fix arr.flat.index for large or big-endian machines
Browse files Browse the repository at this point in the history
The type read when exposing was previously int, but has to be
intp.  this would only be visible for >2**31 elements, but is
also visible on big-endian machines.

Closes gh-19153
  • Loading branch information
seberg authored and charris committed Jun 25, 2021
1 parent 34c10e8 commit dc5ec98
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
16 changes: 10 additions & 6 deletions numpy/core/src/multiarray/iterators.c
Expand Up @@ -1063,13 +1063,15 @@ 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)
{
Expand All @@ -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},
};

Expand Down
11 changes: 11 additions & 0 deletions numpy/core/tests/test_multiarray.py
Expand Up @@ -5364,6 +5364,17 @@ def test_refcount(self):
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:

Expand Down

0 comments on commit dc5ec98

Please sign in to comment.