Skip to content

Commit 4cd7cb4

Browse files
committed
Raise an exception when dtype exceeds size limit
1 parent 95a9dc6 commit 4cd7cb4

2 files changed

Lines changed: 16 additions & 13 deletions

File tree

src/blosc2/blosc2_ext.pyx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ cdef extern from "blosc2.h":
7474
BLOSC2_MAX_OVERHEAD
7575
BLOSC2_MAX_BUFFERSIZE
7676
BLOSC2_MAXBLOCKSIZE
77+
BLOSC2_MAXTYPESIZE
7778
BLOSC_MAX_TYPESIZE
7879
BLOSC_MIN_BUFFERSIZE
7980

@@ -536,7 +537,7 @@ ctypedef struct udf_udata:
536537
int64_t chunks_in_array[B2ND_MAX_DIM]
537538
int64_t blocks_in_chunk[B2ND_MAX_DIM]
538539

539-
MAX_TYPESIZE = BLOSC_MAX_TYPESIZE
540+
MAX_TYPESIZE = BLOSC2_MAXTYPESIZE
540541
MAX_BUFFERSIZE = BLOSC2_MAX_BUFFERSIZE
541542
MAX_BLOCKSIZE = BLOSC2_MAXBLOCKSIZE
542543
MAX_OVERHEAD = BLOSC2_MAX_OVERHEAD

src/blosc2/ndarray.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2776,6 +2776,11 @@ def _check_shape(shape):
27762776
raise TypeError("shape should be a tuple or a list!")
27772777
return shape
27782778

2779+
def _check_dtype(dtype):
2780+
dtype = np.dtype(dtype)
2781+
if dtype.itemsize > blosc2.MAX_TYPESIZE:
2782+
raise ValueError(f"dtype itemsize {dtype.itemsize} is too large (>{blosc2.MAX_TYPESIZE})!")
2783+
return dtype
27792784

27802785
def empty(shape: int | tuple | list, dtype: np.dtype | str | None = np.float64, **kwargs: Any) -> NDArray:
27812786
"""Create an empty array.
@@ -2822,7 +2827,7 @@ def empty(shape: int | tuple | list, dtype: np.dtype | str | None = np.float64,
28222827
>>> array.dtype
28232828
dtype('int32')
28242829
"""
2825-
dtype = np.dtype(dtype)
2830+
dtype = _check_dtype(dtype)
28262831
shape = _check_shape(shape)
28272832
kwargs = _check_ndarray_kwargs(**kwargs)
28282833
chunks = kwargs.pop("chunks", None)
@@ -2856,7 +2861,7 @@ def uninit(shape: int | tuple | list, dtype: np.dtype | str = np.float64, **kwar
28562861
>>> array.dtype
28572862
dtype('float64')
28582863
"""
2859-
dtype = np.dtype(dtype)
2864+
dtype = _check_dtype(dtype)
28602865
shape = _check_shape(shape)
28612866
kwargs = _check_ndarray_kwargs(**kwargs)
28622867
chunks = kwargs.pop("chunks", None)
@@ -2890,7 +2895,7 @@ def nans(shape: int | tuple | list, dtype: np.dtype | str = np.float64, **kwargs
28902895
>>> array.dtype
28912896
dtype('float64')
28922897
"""
2893-
dtype = np.dtype(dtype)
2898+
dtype = _check_dtype(dtype)
28942899
shape = _check_shape(shape)
28952900
kwargs = _check_ndarray_kwargs(**kwargs)
28962901
chunks = kwargs.pop("chunks", None)
@@ -2930,7 +2935,7 @@ def zeros(shape: int | tuple | list, dtype: np.dtype | str = np.float64, **kwarg
29302935
>>> array.dtype
29312936
dtype('float64')
29322937
"""
2933-
dtype = np.dtype(dtype)
2938+
dtype = _check_dtype(dtype)
29342939
shape = _check_shape(shape)
29352940
kwargs = _check_ndarray_kwargs(**kwargs)
29362941
chunks = kwargs.pop("chunks", None)
@@ -2990,6 +2995,7 @@ def full(
29902995
dtype = np.dtype(type(fill_value))
29912996
else:
29922997
dtype = np.dtype(dtype)
2998+
dtype = _check_dtype(dtype)
29932999
shape = _check_shape(shape)
29943000
kwargs = _check_ndarray_kwargs(**kwargs)
29953001
chunks = kwargs.pop("chunks", None)
@@ -3101,8 +3107,7 @@ def arange_fill(inputs, output, offset):
31013107
# Check that the shape is consistent with the start, stop and step values
31023108
if math.prod(shape) != int((stop - start) / step):
31033109
raise ValueError("The shape is not consistent with the start, stop and step values")
3104-
# Check dtype
3105-
dtype = np.dtype(dtype)
3110+
dtype = _check_dtype(dtype)
31063111

31073112
if is_inside_new_expr():
31083113
# We already have the dtype and shape, so return immediately
@@ -3170,8 +3175,7 @@ def linspace_fill(inputs, output, offset):
31703175

31713176
if not shape:
31723177
shape = (num,)
3173-
# Check dtype
3174-
dtype = np.dtype(dtype)
3178+
dtype = _check_dtype(dtype)
31753179

31763180
if is_inside_new_expr():
31773181
# We already have the dtype and shape, so return immediately
@@ -3230,8 +3234,7 @@ def fill_eye(inputs, output: np.array, offset: tuple):
32303234
if M is None:
32313235
M = N
32323236
shape = (N, M)
3233-
# Check dtype
3234-
dtype = np.dtype(dtype)
3237+
dtype = _check_dtype(dtype)
32353238

32363239
if is_inside_new_expr():
32373240
# We already have the dtype and shape, so return immediately
@@ -3284,8 +3287,7 @@ def iter_fill(inputs, output, offset):
32843287
(iterable,) = inputs
32853288
output[:] = np.fromiter(iterable, dtype=output.dtype, count=nout).reshape(output.shape)
32863289

3287-
# Check dtype
3288-
dtype = np.dtype(dtype)
3290+
dtype = _check_dtype(dtype)
32893291

32903292
if is_inside_new_expr():
32913293
# We already have the dtype and shape, so return immediately

0 commit comments

Comments
 (0)