8000 BUG: fix array too big error for wide dtypes. · numpy/numpy@4e86e89 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4e86e89

Browse files
committed
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
1 parent 5c24db2 commit 4e86e89

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

numpy/core/src/multiarray/ctors.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -929,8 +929,7 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
929929
return NULL;
930930
}
931931

932-
/* Check dimensions */
933-
size = 1;
932+
/* Check datatype element size */
934933
sd = (size_t) descr->elsize;
935934
if (sd == 0) {
936935
if (!PyDataType_ISSTRING(descr)) {
@@ -950,6 +949,8 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
950949
}
951950
}
952951

952+
/* Check dimensions */
953+
size = sd;
953954
for (i = 0; i < nd; i++) {
954955
npy_intp dim = dims[i];
955956

@@ -976,7 +977,8 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
976977
*/
977978
if (npy_mul_with_overflow_intp(&size, size, dim)) {
978979
PyErr_SetString(PyExc_ValueError,
979-
"array is too big.");
980+
"array is too big; `arr.size * arr.dtype.itemsize` "
981+
"is larger than the maximum possible size.");
980982
Py_DECREF(descr);
981983
return NULL;
982984
}
@@ -1025,7 +1027,7 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
10251027
* the memory, but be careful with this...
10261028
*/
10271029
memcpy(fa->strides, strides, sizeof(npy_intp)*nd);
1028-
sd *= size;
1030+
sd = size;
10291031
}
10301032
}
10311033
else {

numpy/core/tests/test_multiarray.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,21 @@ def __len__(self):
730730
d = A([1,2,3])
731731
assert_equal(len(np.array(d)), 3)
732732

733+
def test_array_too_big(self):
734+
# Test that array creation succeeds for arrays addressable by intp
735+
# on the byte level and fails for too large arrays.
736+
buf = np.zeros(100)
737+
738+
max_bytes = np.iinfo(np.intp).max
739+
for dtype in ["intp", "S20", "b"]:
740+
dtype = np.dtype(dtype)
741+
itemsize = dtype.itemsize
742+
743+
np.ndarray(buffer=buf, strides=(0,),
744+
shape=(max_bytes//itemsize,), dtype=dtype)
745+
assert_raises(ValueError, np.ndarray, buffer=buf, strides=(0,),
746+
shape=(max_bytes//itemsize + 1,), dtype=dtype)
747+
733748

734749
class TestStructured(TestCase):
735750
def test_subarray_field_access(self):

0 commit comments

Comments
 (0)
0