summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHan Shen <shenhan@google.com>2017-11-30 10:15:55 -0800
committerEric Wieser <wieser.eric@gmail.com>2017-12-11 20:23:21 -0800
commitf9aecaf5b1fc918cf6e898d1240336f7e2e8c743 (patch)
tree292c27853ed0de4d2ced2cb5c63805cc6d277827
parent8032cf4762008155fca610fb61092e6c9ecae98b (diff)
downloadnumpy-f9aecaf5b1fc918cf6e898d1240336f7e2e8c743.tar.gz
BUG: Fix corner cases when value is NaN.
The case is illustrated below: In "_safe_ceil_to_intp": ivalue = npy_ceil(value); <= ivalue is NaN when value is NaN. if (ivalue < NPY_MIN_INTP || ivalue > NPY_MAX_INTP) { return -1; <= this is never executed when ivalue is NaN. } *ret = (npy_intp)ivalue; <= this is undefined behavior when ivalue is NaN.
-rw-r--r--numpy/core/src/multiarray/ctors.c3
-rw-r--r--numpy/core/tests/test_multiarray.py4
2 files changed, 6 insertions, 1 deletions
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index 60f76bf5e..e1989bab8 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -2940,7 +2940,8 @@ static int _safe_ceil_to_intp(double value, npy_intp* ret)
double ivalue;
ivalue = npy_ceil(value);
- if (ivalue < NPY_MIN_INTP || ivalue > NPY_MAX_INTP) {
+ /* condition inverted to handle NaN */
+ if (!(NPY_MIN_INTP <= ivalue && ivalue <= NPY_MAX_INTP)) {
return -1;
}
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index 3bddfe2ae..f55bd0e2b 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -7270,6 +7270,10 @@ def test_npymath_real():
expected = npfun(z)
assert_allclose(got, expected)
+# Test when (stop - start) / step is NaN, ValueError is raised instead
+# of returning a zero-length array.
+def test_arange_nan():
+ assert_raises(ValueError, np.arange, 0, 1, np.nan)
if __name__ == "__main__":
run_module_suite()