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_nodel(): class C(object): pass with pytest.raises(ReferenceError): # Need to delete after using if in with-block context with assert_deallocated(C) as 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_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_circular(): class C(object): def __init__(self): self._circular = self with pytest.raises(ReferenceError): # Circular reference, no automatic garbage collection with assert_deallocated(C) as c: del c
def test_assert_deallocated_circular2(): class C: def __init__(self): self._circular = self with pytest.raises(ReferenceError): # Still circular reference, no automatic garbage collection with assert_deallocated(C): pass
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
def test_assert_deallocated_nodel(): class C: pass with pytest.raises(ReferenceError): # Need to delete after using if in with-block context # Note: assert_deallocated(C) needs to be assigned for the test # to function correctly. It is assigned to c, but c itself is # not referenced in the body of the with, it is only there for # the refcount. with assert_deallocated(C) as c: pass