Skip to content

NDArray.expand_dims does not correctly increase the reference count #478

@FrancescAlted

Description

@FrancescAlted

As a consequence, the underlying NDArray object may disappear, and the new view will not be able to access the underlying data because it has been freed already. Here it is a simple code snippet reproducing the issue:

In [1]: import blosc2

In [2]: a = blosc2.arange(10)

In [3]: a = blosc2.expand_dims(a)

In [4]: a[0]
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[4], line 1
----> 1 a[0]

File ~/blosc/python-blosc2/src/blosc2/ndarray.py:1782, in NDArray.__getitem__(self, key)
   1780 # Create the array to store the result
   1781 arr = np.empty(shape, dtype=self.dtype)
-> 1782 nparr = super().get_slice_numpy(arr, (start, stop))
   1783 if step != (1,) * self.ndim:  # TODO: optimise to work like __setitem__ for non-unit steps
   1784     # have to make step refer to sliced dims (which will be less if ints present)
   1785     slice_ = tuple(slice(None, None, st) for st, m in zip(step, nm_, strict=True) if m)

File ~/blosc/python-blosc2/src/blosc2/blosc2_ext.pyx:2426, in blosc2.blosc2_ext.NDArray.get_slice_numpy()

File ~/blosc/python-blosc2/src/blosc2/blosc2_ext.pyx:2248, in blosc2.blosc2_ext._check_rc()

RuntimeError: Error while getting the buffer

The solution imply re-thinking views, and how to use them from C/Python.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions