diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2017-12-12 13:23:21 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-12 13:23:21 -0700 |
commit | 356b481bcf8783a4ecca6aaa7d7712e347c16101 (patch) | |
tree | d0bc8923f88a822941c12bb9a2c3df632912a383 /numpy | |
parent | 22d917776b80dc7cef683fc17f4ea15b1ce98fcc (diff) | |
parent | c28f1ae597dac17353ef0f3f2d466305cdbc9305 (diff) | |
download | python-numpy-356b481bcf8783a4ecca6aaa7d7712e347c16101.tar.gz python-numpy-356b481bcf8783a4ecca6aaa7d7712e347c16101.tar.bz2 python-numpy-356b481bcf8783a4ecca6aaa7d7712e347c16101.zip |
Merge pull request #10205 from eric-wieser/arange-fixes
BUG: Handle NaNs correctly in arange
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/ctors.c | 37 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 28 |
2 files changed, 46 insertions, 19 deletions
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 60f76bf5e..f4236f36d 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -2935,17 +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); - if (ivalue < NPY_MIN_INTP || ivalue > NPY_MAX_INTP) { + /* 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; } @@ -2962,9 +2970,9 @@ 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; } if (length <= 0) { @@ -3053,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); @@ -3064,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); @@ -3077,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 3bddfe2ae..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) |