summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2019-03-01 20:54:18 -0800
committerEric Wieser <wieser.eric@gmail.com>2019-03-02 11:18:32 -0800
commita4b388e65f845f18c61115f01905da2179a2af60 (patch)
tree8df52ad68d20d356c8a715323cf6e8d24350e81d
parent2f2dfa19839d69a20713b2fe05ca1ca35f6454a7 (diff)
downloadnumpy-a4b388e65f845f18c61115f01905da2179a2af60.tar.gz
BUG: Add missing error checking in conversion from integers to datetime types
This prevents SystemErrors on overflow, fixing gh-13062
-rw-r--r--numpy/core/src/multiarray/datetime.c9
-rw-r--r--numpy/core/tests/test_datetime.py6
2 files changed, 15 insertions, 0 deletions
diff --git a/numpy/core/src/multiarray/datetime.c b/numpy/core/src/multiarray/datetime.c
index 54d19d993..a33f643f1 100644
--- a/numpy/core/src/multiarray/datetime.c
+++ b/numpy/core/src/multiarray/datetime.c
@@ -2468,6 +2468,9 @@ convert_pyobject_to_datetime(PyArray_DatetimeMetaData *meta, PyObject *obj,
return -1;
}
*out = PyLong_AsLongLong(obj);
+ if (error_converting(*out)) {
+ return -1;
+ }
return 0;
}
/* Datetime scalar */
@@ -2666,6 +2669,9 @@ convert_pyobject_to_timedelta(PyArray_DatetimeMetaData *meta, PyObject *obj,
}
*out = PyLong_AsLongLong(obj);
+ if (error_converting(*out)) {
+ return -1;
+ }
return 0;
}
/* Timedelta scalar */
@@ -2853,6 +2859,9 @@ convert_pyobject_to_timedelta(PyArray_DatetimeMetaData *meta, PyObject *obj,
}
*out = PyLong_AsLongLong(obj);
+ if (error_converting(*out)) {
+ return -1;
+ }
return 0;
}
else {
diff --git a/numpy/core/tests/test_datetime.py b/numpy/core/tests/test_datetime.py
index 43d29f42d..8d480e7a3 100644
--- a/numpy/core/tests/test_datetime.py
+++ b/numpy/core/tests/test_datetime.py
@@ -1543,6 +1543,12 @@ class TestDateTime(object):
assert_equal(x[0].astype(np.int64), 322689600000000000)
+ # gh-13062
+ with pytest.raises(OverflowError):
+ np.datetime64(2**64, 'D')
+ with pytest.raises(OverflowError):
+ np.timedelta64(2**64, 'D')
+
def test_datetime_as_string(self):
# Check all the units with default string conversion
date = '1959-10-13'