Skip to content

Commit 375ca7c

Browse files
committed
Ensure view holds a reference to original array
1 parent af908e7 commit 375ca7c

4 files changed

Lines changed: 20 additions & 13 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ else()
5050
include(FetchContent)
5151
FetchContent_Declare(blosc2
5252
GIT_REPOSITORY https://github.com/Blosc/c-blosc2
53-
GIT_TAG ab7a0cc3b5ca3cf09c5ddaeb66ba1100c1986f23 # fix view
53+
GIT_TAG ae52f60b0c7caeac68d461f1ae5b0b2d57060e6a # v2.21.2
5454
)
5555
FetchContent_MakeAvailable(blosc2)
5656
include_directories("${blosc2_SOURCE_DIR}/include")

src/blosc2/blosc2_ext.pyx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,9 +2357,10 @@ cdef class slice_flatter:
23572357
cdef class NDArray:
23582358
cdef b2nd_array_t* array
23592359

2360-
def __init__(self, array):
2360+
def __init__(self, array, base=None):
23612361
self._dtype = None
23622362
self.array = <b2nd_array_t *> PyCapsule_GetPointer(array, <char *> "b2nd_array_t*")
2363+
self.base = base # add reference to base if NDArray is a view
23632364

23642365
@property
23652366
def shape(self) -> tuple[int]:
@@ -2997,8 +2998,7 @@ def expand_dims(arr1: NDArray, axis_mask: list[bool], final_dims: int) -> blosc2
29972998
mask_[i] = axis_mask[i]
29982999
_check_rc(b2nd_expand_dims(arr1.array, &view, mask_, final_dims),"Error while expanding the arrays")
29993000

3000-
# Increase the reference count of the original array
3001-
Py_INCREF(arr1)
3002-
3001+
# create view with reference to arr1 to hold onto
3002+
new_base = arr1 if arr1.base is None else arr1.base
30033003
return blosc2.NDArray(_schunk=PyCapsule_New(view.sc, <char *> "blosc2_schunk*", NULL),
3004-
_array=PyCapsule_New(view, <char *> "b2nd_array_t*", NULL))
3004+
_array=PyCapsule_New(view, <char *> "b2nd_array_t*", NULL), _base=new_base)

src/blosc2/ndarray.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,10 +1170,12 @@ def __next__(self):
11701170
class NDArray(blosc2_ext.NDArray, Operand):
11711171
def __init__(self, **kwargs):
11721172
self._schunk = SChunk(_schunk=kwargs["_schunk"], _is_view=True) # SChunk Python instance
1173+
self.base = SChunk(_schunk=kwargs["_schunk"], _is_view=True) # SChunk Python instance
11731174
self._keep_last_read = False
11741175
# Where to store the last read data
11751176
self._last_read = {}
1176-
super().__init__(kwargs["_array"])
1177+
base = kwargs.pop("_base", None)
1178+
super().__init__(kwargs["_array"], base=base)
11771179
# Accessor to fields
11781180
self._fields = {}
11791181
if self.dtype.fields:

tests/ndarray/test_resize.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,21 @@ def test_resize(shape, new_shape, chunks, blocks, fill_value):
4242
)
4343
def test_expand_dims(shape, axis, chunks, blocks, fill_value):
4444
a = blosc2.full(shape, fill_value=fill_value, chunks=chunks, blocks=blocks)
45-
45+
npa = a[:]
4646
b = blosc2.expand_dims(a, axis=axis)
47-
npa = np.expand_dims(a[:], axis)
48-
assert npa.shape == b.shape
49-
np.testing.assert_array_equal(npa, b[:])
47+
npb = np.expand_dims(npa, axis)
48+
assert npb.shape == b.shape
49+
np.testing.assert_array_equal(npb, b[:])
5050

5151
# Repeated expansion
5252
axis = (axis,) if isinstance(axis, int) else axis
5353
axis = axis[0] if (len(axis) + b.ndim) > blosc2.MAX_DIM else axis
5454
b = blosc2.expand_dims(b, axis=axis)
55+
npb = np.expand_dims(npb, axis)
56+
assert npb.shape == b.shape
57+
np.testing.assert_array_equal(npb, b[:])
58+
59+
# Check that handling of views is correct
60+
a = blosc2.expand_dims(a, axis=axis) # could lose ref to original array and thus dealloc data
5561
npa = np.expand_dims(npa, axis)
56-
assert npa.shape == b.shape
57-
np.testing.assert_array_equal(npa, b[:])
62+
assert a[()].shape == npa[()].shape # getitem fails if deallocate has happened

0 commit comments

Comments
 (0)