From 0332a2d8db6bd55b1d7e96c33ba4dd4f858d273d Mon Sep 17 00:00:00 2001 From: Julian Taylor Date: Thu, 23 Oct 2014 22:12:14 +0200 Subject: [PATCH] BUG: fix crash when using arange on datetime without dtype set --- numpy/core/src/multiarray/datetime.c | 5 +++-- numpy/core/tests/test_datetime.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/numpy/core/src/multiarray/datetime.c b/numpy/core/src/multiarray/datetime.c index 850c92b44975..cd52756b972c 100644 --- a/numpy/core/src/multiarray/datetime.c +++ b/numpy/core/src/multiarray/datetime.c @@ -3047,7 +3047,7 @@ cast_timedelta_to_timedelta(PyArray_DatetimeMetaData *src_meta, * Returns true if the object is something that is best considered * a Datetime, false otherwise. */ -static npy_bool +static NPY_GCC_NONNULL(1) npy_bool is_any_numpy_datetime(PyObject *obj) { return (PyArray_IsScalar(obj, Datetime) || @@ -3296,7 +3296,8 @@ datetime_arange(PyObject *start, PyObject *stop, PyObject *step, } } else { - if (is_any_numpy_datetime(start) || is_any_numpy_datetime(stop)) { + if ((start && is_any_numpy_datetime(start)) || + is_any_numpy_datetime(stop)) { type_nums[0] = NPY_DATETIME; } else { diff --git a/numpy/core/tests/test_datetime.py b/numpy/core/tests/test_datetime.py index bf0ba6807394..4e432f8850e6 100644 --- a/numpy/core/tests/test_datetime.py +++ b/numpy/core/tests/test_datetime.py @@ -1412,6 +1412,11 @@ def test_datetime_arange(self): np.datetime64('2012-02-03T14Z', 's'), np.timedelta64(5, 'Y')) + def test_datetime_arange_no_dtype(self): + d = np.array('2010-01-04', dtype="M8[D]") + assert_equal(np.arange(d, d + 1), d) + assert_raises(ValueError, np.arange, d) + def test_timedelta_arange(self): a = np.arange(3, 10, dtype='m8') assert_equal(a.dtype, np.dtype('m8')) @@ -1430,6 +1435,11 @@ def test_timedelta_arange(self): assert_raises(TypeError, np.arange, np.timedelta64(0, 'Y'), np.timedelta64(5, 'D')) + def test_timedelta_arange_no_dtype(self): + d = np.array(5, dtype="m8[D]") + assert_equal(np.arange(d, d + 1), d) + assert_raises(ValueError, np.arange, d) + def test_datetime_maximum_reduce(self): a = np.array(['2010-01-02', '1999-03-14', '1833-03'], dtype='M8[D]') assert_equal(np.maximum.reduce(a).dtype, np.dtype('M8[D]'))