File: test_gc.py

package info (click to toggle)
petsc4py 3.24.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,612 kB
  • sloc: python: 13,569; ansic: 1,768; makefile: 345; f90: 313; sh: 14
file content (125 lines) | stat: -rw-r--r-- 2,864 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from petsc4py import PETSc
import os
import unittest
import gc
import weakref

# --------------------------------------------------------------------

## gc.set_debug((gc.DEBUG_STATS |
##               gc.DEBUG_LEAK) &
##              ~gc.DEBUG_SAVEALL)

# --------------------------------------------------------------------

PYSABI = os.path.basename(PETSc.__file__).split(".")[1] == "abi3"


class BaseTestGC:
    def setUp(self):
        self.obj = self.CLASS().create(comm=PETSc.COMM_SELF)

    def tearDown(self):
        wref = self.make_weakref()
        self.assertTrue(wref() is self.obj)
        self.obj = None
        gc.collect()
        if PYSABI:
            # garbage collection is not supported
            # the weak reference is still alive
            self.assertTrue(wref() is not None)
            self.assertEqual(wref().getRefCount(), 1)
            wref().destroy()
        else:
            # the weak reference is gone
            self.assertTrue(wref() is None)
        del wref
        PETSc.garbage_cleanup()

    def make_weakref(self):
        return weakref.ref(self.obj)

    def testCycleInSelf(self):
        self.obj.setAttr('myself', self.obj)

    def testCycleInMethod(self):
        self.obj.setAttr('mymeth', self.obj.view)

    def testCycleInInstance(self):
        class A:
            pass

        a = A()
        a.obj = self.obj
        self.obj.setAttr('myinst', a)

    def testCycleInAllWays(self):
        self.testCycleInSelf()
        self.testCycleInMethod()
        self.testCycleInInstance()


# --------------------------------------------------------------------


class TestGCVec(BaseTestGC, unittest.TestCase):
    CLASS = PETSc.Vec


class TestGCVecSubType(TestGCVec):
    CLASS = type('_Vec', (PETSc.Vec,), {})


class TestGCMat(BaseTestGC, unittest.TestCase):
    CLASS = PETSc.Mat


class TestGCMatSubType(TestGCMat):
    CLASS = type('_Mat', (PETSc.Mat,), {})


class TestGCPC(BaseTestGC, unittest.TestCase):
    CLASS = PETSc.PC


class TestGCPCSubType(TestGCPC):
    CLASS = type('_PC', (PETSc.PC,), {})


class TestGCKSP(BaseTestGC, unittest.TestCase):
    CLASS = PETSc.KSP


class TestGCKSPSubType(TestGCKSP):
    CLASS = type('_KSP', (PETSc.KSP,), {})


class TestGCSNES(BaseTestGC, unittest.TestCase):
    CLASS = PETSc.SNES

    def testCycleInAppCtx(self):
        self.obj.setAppCtx(self.obj)


class TestGCSNESSubType(TestGCSNES):
    CLASS = type('_SNES', (PETSc.SNES,), {})


class TestGCTS(BaseTestGC, unittest.TestCase):
    CLASS = PETSc.TS

    def testCycleInAppCtx(self):
        self.obj.setAppCtx(self.obj)


class TestGCTSSubType(TestGCTS):
    CLASS = type('_TS', (PETSc.TS,), {})

    def testCycleInAppCtx(self):
        self.obj.setAppCtx(self.obj)


# --------------------------------------------------------------------

if __name__ == '__main__':
    unittest.main()