def test_assert_deallocated(): # Ordinary use class C(object): def __init__(self, arg0, arg1, name='myname'): self.name = name for gc_current in (True, False): with gc_state(gc_current): # We are deleting from with-block context, so that's OK with assert_deallocated(C, 0, 2, 'another name') as c: assert_equal(c.name, 'another name') del c # Or not using the thing in with-block context, also OK with assert_deallocated(C, 0, 2, name='third name'): pass assert_equal(gc.isenabled(), gc_current)
def test_assert_deallocated_circular2(): class C(object): def __init__(self): self._circular = self # Still circular reference, no automatic garbage collection with assert_deallocated(C): pass
def test_assert_deallocated_nodel(): class C(object): pass # Need to delete after using if in with-block context with assert_deallocated(C) as c: pass
def test_circular_refs(self): # Test interp1d can be automatically garbage collected x = np.linspace(0, 1) y = np.linspace(0, 1) # Confirm interp can be released from memory after use with assert_deallocated(interp1d, x, y) as interp: new_y = interp([0.1, 0.2]) del interp
def test_linearoperator_deallocation(): # Check that the linear operators used by the Arpack wrappers are # deallocatable by reference counting -- they are big objects, so # Python's cyclic GC may not collect them fast enough before # running out of memory if eigs/eigsh are called in a tight loop. M_d = np.eye(10) M_s = csc_matrix(M_d) M_o = aslinearoperator(M_d) with assert_deallocated(lambda: arpack.SpLuInv(M_s)): pass with assert_deallocated(lambda: arpack.LuInv(M_d)): pass with assert_deallocated(lambda: arpack.IterInv(M_s)): pass with assert_deallocated(lambda: arpack.IterOpInv(M_o, None, 0.3)): pass with assert_deallocated(lambda: arpack.IterOpInv(M_o, M_o, 0.3)): pass