1from petsc4py import PETSc 2import unittest 3import gc, weakref 4import warnings 5 6# -------------------------------------------------------------------- 7 8## gc.set_debug((gc.DEBUG_STATS | 9## gc.DEBUG_LEAK) & 10## ~gc.DEBUG_SAVEALL) 11 12# -------------------------------------------------------------------- 13 14class BaseTestGC(object): 15 16 def setUp(self): 17 self.obj = self.CLASS().create(comm=PETSc.COMM_SELF) 18 19 def tearDown(self): 20 wref = self.make_weakref() 21 self.assertTrue(wref() is self.obj) 22 self.obj = None 23 gc.collect() 24 self.assertTrue(wref() is None) 25 26 def make_weakref(self): 27 wref = weakref.ref(self.obj) 28 return wref 29 30 def testCycleInSelf(self): 31 self.obj.setAttr('myself', self.obj) 32 33 def testCycleInMethod(self): 34 self.obj.setAttr('mymeth', self.obj.view) 35 36 def testCycleInInstance(self): 37 class A: pass 38 a = A() 39 a.obj = self.obj 40 self.obj.setAttr('myinst', a) 41 42 def testCycleInAllWays(self): 43 self.testCycleInSelf() 44 self.testCycleInMethod() 45 self.testCycleInInstance() 46 47# -------------------------------------------------------------------- 48 49class TestGCVec(BaseTestGC, unittest.TestCase): 50 CLASS = PETSc.Vec 51 52class TestGCVecSubType(TestGCVec): 53 CLASS = type('_Vec', (PETSc.Vec,), {}) 54 55class TestGCMat(BaseTestGC, unittest.TestCase): 56 CLASS = PETSc.Mat 57 58class TestGCMatSubType(TestGCMat): 59 CLASS = type('_Mat', (PETSc.Mat,), {}) 60 61class TestGCPC(BaseTestGC, unittest.TestCase): 62 CLASS = PETSc.PC 63 64class TestGCPCSubType(TestGCPC): 65 CLASS = type('_PC', (PETSc.PC,), {}) 66 67class TestGCKSP(BaseTestGC, unittest.TestCase): 68 CLASS = PETSc.KSP 69 70class TestGCKSPSubType(TestGCKSP): 71 CLASS = type('_KSP', (PETSc.KSP,), {}) 72 73class TestGCSNES(BaseTestGC, unittest.TestCase): 74 CLASS = PETSc.SNES 75 def testCycleInAppCtx(self): 76 self.obj.setAppCtx(self.obj) 77 78class TestGCSNESSubType(TestGCSNES): 79 CLASS = type('_SNES', (PETSc.SNES,), {}) 80 81class TestGCTS(BaseTestGC, unittest.TestCase): 82 CLASS = PETSc.TS 83 def testCycleInAppCtx(self): 84 self.obj.setAppCtx(self.obj) 85 86class TestGCTSSubType(TestGCTS): 87 CLASS = type('_TS', (PETSc.TS,), {}) 88 def testCycleInAppCtx(self): 89 self.obj.setAppCtx(self.obj) 90 91# -------------------------------------------------------------------- 92 93if __name__ == '__main__': 94 unittest.main() 95