diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2021-11-03 18:24:39 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-03 18:24:39 -0600 |
commit | acbbf1a66179f8c83c75022ddbdbf1d15c35cdaa (patch) | |
tree | 192e7b325f51d0aa0ec34f6df0433ac5a9ca49d5 | |
parent | 5fe9833b45772a36e4c324c1ff59cc2926dae1be (diff) | |
parent | b1869922b4e9d45d6ec2a97c892deaf8317c552f (diff) | |
download | numpy-acbbf1a66179f8c83c75022ddbdbf1d15c35cdaa.tar.gz |
Merge pull request #20294 from charris/backport-20237
BUG: ``VOID_nonzero`` could sometimes mutate alignment flag
-rw-r--r-- | numpy/core/src/multiarray/arraytypes.c.src | 4 | ||||
-rw-r--r-- | numpy/core/tests/test_numeric.py | 12 |
2 files changed, 14 insertions, 2 deletions
diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src index b3ea7544d..7f545d6cf 100644 --- a/numpy/core/src/multiarray/arraytypes.c.src +++ b/numpy/core/src/multiarray/arraytypes.c.src @@ -2759,10 +2759,10 @@ VOID_nonzero (char *ip, PyArrayObject *ap) dummy_fields.descr = new; if ((new->alignment > 1) && !__ALIGNED(ip + offset, new->alignment)) { - PyArray_CLEARFLAGS(ap, NPY_ARRAY_ALIGNED); + PyArray_CLEARFLAGS(dummy_arr, NPY_ARRAY_ALIGNED); } else { - PyArray_ENABLEFLAGS(ap, NPY_ARRAY_ALIGNED); + PyArray_ENABLEFLAGS(dummy_arr, NPY_ARRAY_ALIGNED); } if (new->f->nonzero(ip+offset, dummy_arr)) { nonz = NPY_TRUE; diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index fe310058a..af311066a 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -1497,6 +1497,18 @@ class TestNonzero: a = np.array([[False], [TrueThenFalse()]]) assert_raises(RuntimeError, np.nonzero, a) + def test_nonzero_sideffects_structured_void(self): + # Checks that structured void does not mutate alignment flag of + # original array. + arr = np.zeros(5, dtype="i1,i8,i8") # `ones` may short-circuit + assert arr.flags.aligned # structs are considered "aligned" + assert not arr["f2"].flags.aligned + # make sure that nonzero/count_nonzero do not flip the flag: + np.nonzero(arr) + assert arr.flags.aligned + np.count_nonzero(arr) + assert arr.flags.aligned + def test_nonzero_exception_safe(self): # gh-13930 |