8000 Merge pull request #20237 from seberg/bug-void-nonzero · thomasjpfan/numpy@d85523a · GitHub
[go: up one dir, main page]

Skip to content

Commit d85523a

Browse files
authored
Merge pull request numpy#20237 from seberg/bug-void-nonzero
BUG: `VOID_nonzero` could sometimes mutate alignment flag
2 parents a37a51c + d1899f2 commit d85523a

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

numpy/core/src/multiarray/arraytypes.c.src

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2759,10 +2759,10 @@ VOID_nonzero (char *ip, PyArrayObject *ap)
27592759
dummy_fields.descr = new;
27602760
if ((new->alignment > 1) && !__ALIGNED(ip + offset,
27612761
new->alignment)) {
2762-
PyArray_CLEARFLAGS(ap, NPY_ARRAY_ALIGNED);
2762+
PyArray_CLEARFLAGS(dummy_arr, NPY_ARRAY_ALIGNED);
27632763
}
27642764
else {
2765-
PyArray_ENABLEFLAGS(ap, NPY_ARRAY_ALIGNED);
2765+
PyArray_ENABLEFLAGS(dummy_arr, NPY_ARRAY_ALIGNED);
27662766
}
27672767
if (new->f->nonzero(ip+offset, dummy_arr)) {
27682768
nonz = NPY_TRUE;

numpy/core/tests/test_numeric.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,18 @@ def __bool__(self):
14841484
a = np.array([[False], [TrueThenFalse()]])
14851485
assert_raises(RuntimeError, np.nonzero, a)
14861486

1487+
def test_nonzero_sideffects_structured_void(self):
1488+
# Checks that structured void does not mutate alignment flag of
1489+
# original array.
1490+
arr = np.zeros(5, dtype="i1,i8,i8") # `ones` may short-circuit
1491+
assert arr.flags.aligned # structs are considered "aligned"
1492+
assert not arr["f2"].flags.aligned
1493+
# make sure that nonzero/count_nonzero do not flip the flag:
1494+
np.nonzero(arr)
1495+
assert arr.flags.aligned
1496+
np.count_nonzero(arr)
1497+
assert arr.flags.aligned
1498+
14871499
def test_nonzero_exception_safe(self):
14881500
# gh-13930
14891501

0 commit comments

Comments
 (0)
0