summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2017-12-11 20:32:04 -0800
committerEric Wieser <wieser.eric@gmail.com>2017-12-11 20:32:04 -0800
commitc28f1ae597dac17353ef0f3f2d466305cdbc9305 (patch)
treef36137e2cd9a0005c5e9a2d08c8eb3cd03727059 /numpy
parent655ba9a968ba4f1419693c978b175fac8baee06e (diff)
downloadnumpy-c28f1ae597dac17353ef0f3f2d466305cdbc9305.tar.gz
MAINT: Produce a different error message when length is NaN
Also, improve tests.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/multiarray/ctors.c33
-rw-r--r--numpy/core/tests/test_multiarray.py32
2 files changed, 43 insertions, 22 deletions
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index 319e17a46..f4236f36d 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -2935,18 +2935,25 @@ PyArray_Empty(int nd, npy_intp *dims, PyArray_Descr *type, int is_f_order)
* Return 0 on success, -1 on failure. In case of failure, set a PyExc_Overflow
* exception
*/
-static int _safe_ceil_to_intp(double value, npy_intp* ret)
+static npy_intp
+_arange_safe_ceil_to_intp(double value)
{
double ivalue;
ivalue = npy_ceil(value);
/* condition inverted to handle NaN */
+ if (npy_isnan(ivalue)) {
+ PyErr_SetString(PyExc_ValueError,
+ "arange: cannot compute length");
+ return -1;
+ }
if (!(NPY_MIN_INTP <= ivalue && ivalue <= NPY_MAX_INTP)) {
+ PyErr_SetString(PyExc_OverflowError,
+ "arange: overflow while computing length");
return -1;
}
- *ret = (npy_intp)ivalue;
- return 0;
+ return (npy_intp)ivalue;
}
@@ -2963,9 +2970,8 @@ PyArray_Arange(double start, double stop, double step, int type_num)
int ret;
NPY_BEGIN_THREADS_DEF;
- if (_safe_ceil_to_intp((stop - start)/step, &length)) {
- PyErr_SetString(PyExc_OverflowError,
- "arange: overflow while computing length");
+ length = _arange_safe_ceil_to_intp((stop - start)/step);
+ if (error_converting(length)) {
return NULL;
}
@@ -3055,10 +3061,9 @@ _calc_length(PyObject *start, PyObject *stop, PyObject *step, PyObject **next, i
Py_DECREF(val);
return -1;
}
- if (_safe_ceil_to_intp(value, &len)) {
+ len = _arange_safe_ceil_to_intp(value);
+ if (error_converting(len)) {
Py_DECREF(val);
- PyErr_SetString(PyExc_OverflowError,
- "arange: overflow while computing length");
return -1;
}
value = PyComplex_ImagAsDouble(val);
@@ -3066,9 +3071,8 @@ _calc_length(PyObject *start, PyObject *stop, PyObject *step, PyObject **next, i
if (error_converting(value)) {
return -1;
}
- if (_safe_ceil_to_intp(value, &tmp)) {
- PyErr_SetString(PyExc_OverflowError,
- "arange: overflow while computing length");
+ tmp = _arange_safe_ceil_to_intp(value);
+ if (error_converting(tmp)) {
return -1;
}
len = PyArray_MIN(len, tmp);
@@ -3079,9 +3083,8 @@ _calc_length(PyObject *start, PyObject *stop, PyObject *step, PyObject **next, i
if (error_converting(value)) {
return -1;
}
- if (_safe_ceil_to_intp(value, &len)) {
- PyErr_SetString(PyExc_OverflowError,
- "arange: overflow while computing length");
+ len = _arange_safe_ceil_to_intp(value);
+ if (error_converting(len)) {
return -1;
}
}
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index f55bd0e2b..fdd34b6c0 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -1643,7 +1643,7 @@ class TestMethods(object):
arr = np.array([0, datetime.now(), 1], dtype=object)
for kind in ['q', 'm', 'h']:
assert_raises(TypeError, arr.sort, kind=kind)
- #gh-3879
+ #gh-3879
class Raiser(object):
def raises_anything(*args, **kwargs):
raise TypeError("SOMETHING ERRORED")
@@ -7196,8 +7196,30 @@ class TestWritebackIfCopy(TestCase):
assert_(not arr_wb.ctypes.data == 0)
arr_wb[:] = 100
assert_equal(arr, -100)
-
-
+
+
+class TestArange(object):
+ def test_infinite(self):
+ assert_raises_regex(
+ ValueError, "size exceeded",
+ np.arange, 0, np.inf
+ )
+
+ def test_nan_step(self):
+ assert_raises_regex(
+ ValueError, "cannot compute length",
+ np.arange, 0, 1, np.nan
+ )
+
+ def test_zero_step(self):
+ assert_raises(ZeroDivisionError, np.arange, 0, 10, 0)
+ assert_raises(ZeroDivisionError, np.arange, 0.0, 10.0, 0.0)
+
+ # empty range
+ assert_raises(ZeroDivisionError, np.arange, 0, 0, 0)
+ assert_raises(ZeroDivisionError, np.arange, 0.0, 0.0, 0.0)
+
+
def test_orderconverter_with_nonASCII_unicode_ordering():
# gh-7475
a = np.arange(5)
@@ -7270,10 +7292,6 @@ 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()