summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2016-03-26 16:08:34 +0100
committerSebastian Berg <sebastian@sipsolutions.net>2016-03-26 18:35:13 +0100
commit4e86e89be1afc9b6e72e959bdc21567a31f5e1e4 (patch)
treed4f58a790232be876bd58165189622793dcaca60
parent5c24db244ec8a8d7e4ca2f823b0ba71b7d4dff3d (diff)
downloadnumpy-4e86e89be1afc9b6e72e959bdc21567a31f5e1e4.tar.gz
BUG: fix array too big error for wide dtypes.
The error was not being raised when arr.size * arr.dtype.itemsize was too large, but only when arr.size was too large alone. Closes gh-7451
-rw-r--r--numpy/core/src/multiarray/ctors.c10
-rw-r--r--numpy/core/tests/test_multiarray.py15
2 files changed, 21 insertions, 4 deletions
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index 79c2b16b1..5671f2251 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -929,8 +929,7 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
return NULL;
}
- /* Check dimensions */
- size = 1;
+ /* Check datatype element size */
sd = (size_t) descr->elsize;
if (sd == 0) {
if (!PyDataType_ISSTRING(descr)) {
@@ -950,6 +949,8 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
}
}
+ /* Check dimensions */
+ size = sd;
for (i = 0; i < nd; i++) {
npy_intp dim = dims[i];
@@ -976,7 +977,8 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
*/
if (npy_mul_with_overflow_intp(&size, size, dim)) {
PyErr_SetString(PyExc_ValueError,
- "array is too big.");
+ "array is too big; `arr.size * arr.dtype.itemsize` "
+ "is larger than the maximum possible size.");
Py_DECREF(descr);
return NULL;
}
@@ -1025,7 +1027,7 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
* the memory, but be careful with this...
*/
memcpy(fa->strides, strides, sizeof(npy_intp)*nd);
- sd *= size;
+ sd = size;
}
}
else {
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index 14a902b9c..94731f20c 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -730,6 +730,21 @@ class TestCreation(TestCase):
d = A([1,2,3])
assert_equal(len(np.array(d)), 3)
+ def test_array_too_big(self):
+ # Test that array creation succeeds for arrays addressable by intp
+ # on the byte level and fails for too large arrays.
+ buf = np.zeros(100)
+
+ max_bytes = np.iinfo(np.intp).max
+ for dtype in ["intp", "S20", "b"]:
+ dtype = np.dtype(dtype)
+ itemsize = dtype.itemsize
+
+ np.ndarray(buffer=buf, strides=(0,),
+ shape=(max_bytes//itemsize,), dtype=dtype)
+ assert_raises(ValueError, np.ndarray, buffer=buf, strides=(0,),
+ shape=(max_bytes//itemsize + 1,), dtype=dtype)
+
class TestStructured(TestCase):
def test_subarray_field_access(self):