@@ -42,16 +42,36 @@ def test_resize(shape, new_shape, chunks, blocks, fill_value):
4242)
4343def 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
63+
64+ # Now check that garbage collecting works and there will be no memory leaks for views
65+ import sys
66+
67+ arr = np .arange (4 )
68+ bloscarr_ = blosc2 .asarray (arr )
69+ assert sys .getrefcount (arr ) == sys .getrefcount (bloscarr_ ) == 2
70+
71+ view = np .expand_dims (arr , 0 )
72+ bloscview = blosc2 .expand_dims (bloscarr_ , 0 )
73+ assert sys .getrefcount (arr ) == sys .getrefcount (bloscarr_ ) == 3
74+
75+ del view
76+ del bloscview
77+ assert sys .getrefcount (arr ) == sys .getrefcount (bloscarr_ ) == 2
0 commit comments