Skip to content

Commit

Permalink
BUG: Fix incorrect return type in reduce without initial value
Browse files Browse the repository at this point in the history
The return type should not be integer of course (why doesn't C warn
about this?).

Closes gh-20921
  • Loading branch information
seberg authored and charris committed Feb 2, 2022
1 parent aa358cc commit 2d05278
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion numpy/core/src/umath/reduction.c
Expand Up @@ -68,7 +68,7 @@ count_axes(int ndim, const npy_bool *axis_flags)
* Returns -1 if an error occurred, and otherwise the reduce arrays size,
* which is the number of elements already initialized.
*/
NPY_NO_EXPORT int
static npy_intp
PyArray_CopyInitialReduceValues(
PyArrayObject *result, PyArrayObject *operand,
const npy_bool *axis_flags, const char *funcname,
Expand Down
12 changes: 12 additions & 0 deletions numpy/core/tests/test_ufunc.py
Expand Up @@ -14,6 +14,7 @@
assert_almost_equal, assert_array_almost_equal, assert_no_warnings,
assert_allclose, HAS_REFCOUNT, suppress_warnings
)
from numpy.testing._private.utils import requires_memory
from numpy.compat import pickle


Expand Down Expand Up @@ -1555,6 +1556,17 @@ def check_identityless_reduction(self, a):
[[0, 1, 1], [1, 1, 1]])
assert_equal(np.minimum.reduce(a, axis=()), a)

@requires_memory(6 * 1024**3)
def test_identityless_reduction_huge_array(self):
# Regression test for gh-20921 (copying identity incorrectly failed)
arr = np.zeros((2, 2**31), 'uint8')
arr[:, 0] = [1, 3]
arr[:, -1] = [4, 1]
res = np.maximum.reduce(arr, axis=0)
del arr
assert res[0] == 3
assert res[-1] == 4

def test_identityless_reduction_corder(self):
a = np.empty((2, 3, 4), order='C')
self.check_identityless_reduction(a)
Expand Down

0 comments on commit 2d05278

Please sign in to comment.